Skip to content

CA1862 should not be triggered when an explicit CultureInfo is provided. #7672

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

Open
m-gallesio opened this issue May 26, 2025 · 0 comments
Open

Comments

@m-gallesio
Copy link

Analyzer

Diagnostic ID: CA1862: Use the 'StringComparison' method overloads to perform case-insensitive string comparisons

Describe the improvement

The analyzer is triggered by the following code:

return LoadAllEnabled().Where(item => item.Text.ToLower(CurrentCulture).Contains(text)).ToList();

I think the very fact I am passing a CultureInfo explicitly to ToLower should be enough to tell that the actual culture is potentially meaningful for the comparison.

Moreover, the in-editor description of the rule feels verbose and very lacking in self-confidence ("keep in mind this might cause subtle changes in behavior, so make sure to conduct thorough testing") for an analysis with an automatic code fix.

Additional context

Internally Contains leverages IndexOf, which internally uses CultureInfo.CompareInfo:

https://github.com/dotnet/runtime/blob/2d5f1f0165e826ac01745df9f3a390905e282ed3/src/libraries/System.Private.CoreLib/src/System/String.Searching.cs#L55

There could be value in exposing variants of IndexOf, Contains and similar methods which accept a CultureInfo or a CompareInfo object.

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

No branches or pull requests

1 participant