CA1862 warning is annoying should the compiler just convert it instead #7711
Replies: 3 comments 1 reply
-
CAxxxx analyzers belong to roslyn-analyzers repo. It's not a part of the language. |
Beta Was this translation helpful? Give feedback.
-
You misunderstood. This is not an analyzer issue, as I can disable it. I want the language improved so
is handled elegantly and efficiently. This would also mean that the analyzer would no longer be needed. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the information. At least I tried. As an end user ToLower() sounds like it is a language feature, so it should be able to be improved and not have the extra memory usage. I would have thought the language team would agree that 'if (request.UserEmail.Equals("[email protected]", StringComparison.CurrentCultureIgnoreCase))' is not very elegant. Very frustrating, I will just disable the analyzer and close this issue. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The CA1862 warning about using string.Equals for case insensitive is annoying, makes the code less readable and flawed as it breaks my EF Core projections (where I am making the database use case insensitivity inside the SQL that EF Core generates).
Since you can detect the case insensitivity in the compiler, can you please convert the code in the IL? That way we have the efficient code without losing the elegance.
This means the code is more elegant to read (as Mads says is his aim in dotnet conf 2023 and elsewhere).
For example I think this is more elegant and readable than:
if (request.UserEmail.ToLower() == "[email protected]"))
than
if (request.UserEmail.Equals("[email protected]", StringComparison.CurrentCultureIgnoreCase))
and have a real dislike for this warning. The code change also breaks my EF Core projections, but I may be doing something wrong there, I am not sure.
Note: The CA!862 warning was introduced in dotnet 8.0 (see https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-8/).
Here is what it says:
CA1862, added in dotnet/roslyn-analyzers#6662, looks for places where code is performing a case-insensitive comparison (which is fine) but doing so by first lower/uppercasing an input string and then comparing that (which is far from fine).
It’s much more efficient to just use a StringComparison. dotnet/runtime#89539 fixes a few such cases.
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions