Skip to content

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

@carlossanlop

Description

@carlossanlop

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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions