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

CA1862: Recommend case insensitive comparison overloads with patterns stored across statements #6689

Open
carlossanlop opened this issue Jun 15, 2023 · 3 comments

Comments

@carlossanlop
Copy link
Member

carlossanlop commented Jun 15, 2023

Follow up for CA1862 after merging this PR: #6662

This case was suggested in the following comment: #6662 (comment)

Should we consider also detecting simple patterns across statements, such as below?

var loweredStr = str.ToLower();
if (loweredStr.Contains(anotherStr))

We would need to check that we have exactly one reference to the intermediate temporary variable.

If yes, we already added another analyzer which implements a simple heuristic for detecting above patterns: https://github.com/dotnet/roslyn-analyzers/blob/main/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/PreferStringContainsOverIndexOfAnalyzer.cs. If not for v1, but feel it may be a worthwhile enhancement.

Also, per @Youssef1313 's suggestion here:

For CompareTo, I think we should only report when the case changing method is called on both strings (or a constant known to be lower-case at compile-time). In this case, I think the codefix will produce the correct result and we can add it back.

@mavasani
Copy link
Contributor

@carlossanlop Is this issue addressed with #6662?

@carlossanlop
Copy link
Member Author

No. This was opened as a follow up from #6662 from feedback you gave me there.

@mavasani
Copy link
Contributor

Oh sorry about that..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants