The SerializeField attribute is redundant on public fields

Rule ND3205 API Usage / Unity

SerializeField makes a field display in the Inspector and be saved — which declaring the field public already does. Hence the attribute is redundant on public fields.

How to fix

Remove the SerializeField attribute on matched public fields.

CQLinq source

This rule is written in CQLinq, C# LINQ over the NDepend code model. It ships with every new NDepend project: you can read it, adapt it, or use it as a starting point for your own custom rules.

// <Name>The SerializeField attribute is redundant on public fields</Name>
// <Id>ND3205:TheSerializeFieldAttributeIsRedundantOnPublicFields</Id>

warnif count > 0
from f in Application.Fields where
  f.IsPubliclyVisible &&
  f.HasAttribute("UnityEngine.SerializeFieldAttribute".AllowNoMatch())
select new { 
  f, 
  Debt = 3.ToMinutes().ToDebt(),
  Severity = Severity.Medium
}

//<Expl>
// The *public {0}* shouldn't be tagged with *SerializeFieldAttribute*, 
// this causes redundancy.
//</Expl>

//<Desc>
// *SerializeField* makes a field display in the Inspector and be saved —
// which declaring the field public already does. Hence the attribute is
// redundant on public fields.
//</Desc>

//<HowToFix>
// Remove the *SerializeField* attribute on matched public fields.
//</HowToFix>

API Usage / Unity: all 9 items