Skip to content

Fix CA1805 for unnecessary initialization of fields - #38410

Merged
stephentoub merged 1 commit into
dotnet:masterfrom
stephentoub:fixca1805
Jun 26, 2020
Merged

Fix CA1805 for unnecessary initialization of fields#38410
stephentoub merged 1 commit into
dotnet:masterfrom
stephentoub:fixca1805

Conversation

@stephentoub

@stephentoub stephentoub commented Jun 25, 2020

Copy link
Copy Markdown
Member

This has three primary benefits:

  1. In some cases, in particular for static fields where other static fields have non-default initialization, the C# compiler can't elide the default initialization, resulting in more IL.
  2. It highlights places where the field is otherwise unused and can be deleted, but where the presence of the initialization was silencing the C# compiler's warnings about the unused field.
  3. It increases consistency and decreases maintenance / code size / etc.

cc: @marek-safar, @bartonjs

@danmoseley

Copy link
Copy Markdown
Contributor

Are you going to enable the rule so we don't go backwards (just waiting on that Roslyn fix?)

Did the rule have a fixer?

@stephentoub

stephentoub commented Jun 25, 2020

Copy link
Copy Markdown
Member Author

Are you going to enable the rule so we don't go backwards

Eventually. There are a few cases I reverted because they require more follow-up, e.g. #38401, generally where removing the initialization then caused C# to warn that the field was unused, and while I deleted a few, I didn't go through all of them.

Did the rule have a fixer?

Yes, I wrote a fixer for it and used that to do this:
https://github.com/dotnet/roslyn-analyzers/blob/master/src/NetAnalyzers/CSharp/Microsoft.CodeQuality.Analyzers/QualityGuidelines/CSharpDoNotInitializeUnnecessarily.Fixer.cs

Comment thread src/libraries/System.Private.Xml/src/System/Xml/Xsl/Xslt/OutputScopeManager.cs Outdated
@stephentoub
stephentoub force-pushed the fixca1805 branch 2 times, most recently from b9e6b0c to a404806 Compare June 25, 2020 21:57
@bartonjs

Copy link
Copy Markdown
Member

Wow, that was a lot of "Load Diff" clicking.

@danmoseley danmoseley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good except I don't follow HeaderCollection.cs

I did not review inside Github :)

Comment thread src/libraries/System.Net.Mail/src/System/Net/Mime/HeaderCollection.cs Outdated
This has three primary benefits:
1. In some cases, in particular for static fields where other static fields have non-default initialization, the C# compiler can't elide the default initialization, resulting in more IL.
2. It highlights places where the field is otherwise unused and can be deleted, but where the presence of the initialization was silencing the C# compiler's warnings about the unused field.
3. It increases consistency and decreases maintenance / code size / etc.
@stephentoub
stephentoub merged commit ed78fad into dotnet:master Jun 26, 2020
@stephentoub
stephentoub deleted the fixca1805 branch June 26, 2020 11:52
@stephentoub stephentoub mentioned this pull request Jun 26, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants