Open bretehlert opened 5 years ago
If the class uses code contracts and there happens to be an invariant on Something
, then this is necessary as contracts gets confused by read-only auto properties.
Having said that, it's probably a good rule for the projects lucky enough to have dropped code contracts.
This sounds like something another Analyzer should have taken care of already, though I can't find a corresponding FxCopAnalyzers rule or Roslyn-inbuilt rule.
Resharper has one, but we don't use that in the build (and I try to avoid it in general): https://www.jetbrains.com/help/resharper/AutoPropertyCanBeMadeGetOnly.Local.html
I don't think this is realistically achievable with current Api exposed to analyzers.
I still see this kind of code from experienced developers sometimes:
I guess they are not aware of the read-only auto-properties that were introduced in C# 6. The property Something does not need the
private set
and removing the private set is better as it demonstrates the immutability of the property and potentially the class.Add a rule to warn against the private set when the only call to the property setter is in the class constructor.