Diagnostic ID: CA1820: Test for empty strings using string lengths
SDK: Built-in CA analyzers in .NET 5 SDK or later
Version: SDK 5.0.100
A CA1820 rule violation does not suggest a code fix when using the string.Equals method.
s.Equals(string.Empty)
Code fix is offered:
s.Length == 0
No code fix is offered
Currently the code-fix is only offered for == and !=, we could improve the code-fix to also handle .Equals.
We would use a different code fix: s.Length == 0 as we can assume s is not null.
Please target https://github.com/dotnet/roslyn-analyzers/tree/release/6.0.1xx-preview1 branch as this would be a new feature.
@mavasani Just to be completely clear, s.Equals(string.Empty) is already identified as a CA1820 violation but no code fix is offered.
@xtqqczze Ah, thanks. So, this is just a bug fix to an existing code fix? If so, targeting master should also be fine.
@mavasani I think it is a bad experience to report the violation without offering a fix, especially considering this fix is unambiguous. I am not familiar with repository policy, so would you consider this a bug fix, or not ?
@xtqqczze Yes, it would be fine to be considered as a bug fix that targets the master branch. Please feel free to work on it and submit a PR.
I have not worked on code analyzer development before, so I cannot commit to submitting a PR, at least in the short term.
It might not be the simplest to start with but if you want to give it a try, I am happy to support you.
I would like to take a crack at this
@hrkeni It would be great if you could. I am happy to help with tests.
Most helpful comment
I would like to take a crack at this