Roslyn-analyzers: CA1820 does not suggest code fix for string.Equals

Created on 8 Dec 2020  路  11Comments  路  Source: dotnet/roslyn-analyzers

Analyzer

Diagnostic ID: CA1820: Test for empty strings using string lengths

Analyzer source

SDK: Built-in CA analyzers in .NET 5 SDK or later

Version: SDK 5.0.100

Describe the bug

A CA1820 rule violation does not suggest a code fix when using the string.Equals method.

Steps To Reproduce

s.Equals(string.Empty)

Expected behavior

Code fix is offered:

s.Length == 0

Actual behavior

No code fix is offered

Category-Performance Enhancement Needs-Fixer Verified help wanted

Most helpful comment

I would like to take a crack at this

All 11 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

paulomorgado picture paulomorgado  路  3Comments

paulomorgado picture paulomorgado  路  3Comments

paulomorgado picture paulomorgado  路  3Comments

Grauenwolf picture Grauenwolf  路  3Comments

x3ntrix picture x3ntrix  路  3Comments