-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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 suggestion changing semantics of the comparison. #89740
Comments
Tagging subscribers to this area: @dotnet/area-system-globalization Issue Details
using System;
// These are both lower case Greek Sigmas
string a = "σ";
string b = "ς";
// Prints True
Console.WriteLine(a.Equals(b, StringComparison.InvariantCultureIgnoreCase));
string lowerA = a.ToLowerInvariant();
string lowerB = b.ToLowerInvariant();
// Prints False
Console.WriteLine(lowerA == lowerB); Originally posted by @MichalStrehovsky in #89630 (comment)
|
Created the issue in runtime in order to discuss the appropriate fix for this issue @carlossanlop @tarekgh PTAL |
Using I prefer to keep the analyzer and we add something to the analyzer message clarify exceptional cases can hold different results. At least get users to think about it. |
Yep, this is true - but for the code that I pointed out, this a behavior of the CoreCLR implementation of reflection (that is written in C++) that NativeAOT reflection (written in C#) is simply matching. "Fixing" it on NativeAOT would introduce a behavioral difference between the reflection stacks. Whenever this analyzer kicks in, people need to be aware it can change behavior. It is very subtle. |
That is what I suggested to address that |
It is to be preferred to use What I have seen in related issues, e.g.
is a literal or rote translation of existing code to the equivalent But programmers have an extra choice when using The problem with an issue like #78606 is that the word "ordinal" doesn't appear once. So we need to get If the programmer is comparing strings case-insensitively because the strings are identifiers, then For all the following types of strings,
So when would you use a
From experience (C# and .NET since 2002) I can say that comparing "linguistically relevant" strings case-insensitively is something I've never done, whereas comparing identifiers (etc.) using The trouble with the table that seems to be at the heart of these new or revised analyzers, is that it the right-hand column is only correct if the string being operated on is cultural / linguistic. This is a dangerous assumption to make, I feel. The analyzers in this space should be weighted towards recommending |
@carlossanlop were you planning to fix this for 8.0? |
Fixed by dotnet/roslyn-analyzers#6948 |
CA1862: Prefer using 'StringComparer'/'StringComparison' to perform case-insensitive string comparisons
suggestion changing semantics of the comparisonWarning:
Prefer using 'string.Equals(string, StringComparison)' to perform a case-insensitive comparison
Originally posted by @MichalStrehovsky in #89630 (comment):
This is changing semantics of the comparison. Do we really have an analyzer that suggests people blindly do this?
The text was updated successfully, but these errors were encountered: