Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CA1805 for unnecessary initialization of fields #38410

Merged
merged 1 commit into from
Jun 26, 2020

Conversation

stephentoub
Copy link
Member

@stephentoub stephentoub commented Jun 25, 2020

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
Member

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
Copy link
Member Author

stephentoub commented Jun 25, 2020

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

@stephentoub stephentoub force-pushed the fixca1805 branch 2 times, most recently from b9e6b0c to a404806 Compare June 25, 2020 21:57
@bartonjs
Copy link
Member

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

Copy link
Member

@danmoseley danmoseley left a comment

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 :)

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