FYI @stephentoub
_Originally posted by @mavasani in https://github.com/dotnet/roslyn-analyzers/pull/3800/review_comment/create_
See https://github.com/dotnet/roslyn-analyzers/pull/3800/files#diff-b410e83f0b75d15a2208362bbad3bd02R24
Seems this is by design as per https://github.com/dotnet/roslyn-analyzers/pull/3800#discussion_r448003929. Feel free to close it if appropriate.
I'm inclined to say this is by design, though it's always annoying when addressing one warning introduces another. In this particular case, the field is being explicitly initialized to its default value. By removing the = default, we remove unnecessary code and reduce the generated IL, a win win (the compiler emits IL for the = default here because there's another static field that's explicitly initialized to a non-default value). Of course when the = default is removed, the compiler then complains that the field isn't initialized and will keep its default value, even though that's exactly what it was getting before. As I noted in the link, I think the best fix for that particular case (and others like it) is to just make it a property; the warnings will go away, and the resulting IL and asm should be even better.
@stephentoub I think it would be good to add your explanation to documentation page for CA1805, when it is created. Can we update the diagnostic message or description in some way to give this hint: _I think the best fix for that particular case (and others like it) is to just make it a property; the warnings will go away, and the resulting IL and asm should be even better._
Sure. Re-opening to track updating the message.
We can also consider offering a secondary code fix that converts the field into a read only property.
Most helpful comment
I'm inclined to say this is by design, though it's always annoying when addressing one warning introduces another. In this particular case, the field is being explicitly initialized to its default value. By removing the
= default, we remove unnecessary code and reduce the generated IL, a win win (the compiler emits IL for the= defaulthere because there's another static field that's explicitly initialized to a non-default value). Of course when the= defaultis removed, the compiler then complains that the field isn't initialized and will keep its default value, even though that's exactly what it was getting before. As I noted in the link, I think the best fix for that particular case (and others like it) is to just make it a property; the warnings will go away, and the resulting IL and asm should be even better.