-
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
[Analyzer] string.ToLower() == otherString.ToLower() #78607
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Should the invariant variants be also changed? |
The intent is all variations of this that make sense. That includes ToLower/UpperInvariant. |
Tagging subscribers to this area: @dotnet/area-system-runtime Issue DetailsThe right way to compare two strings in a case-insensitive manner is via a StringComparer/Comparison that specifies case-insensitivity. But there's are many cases where code is using ToLower() on each string and then comparing those for equality, e.g.
We should write an analyzer that flags such uses and recommends fixing them to use Equals with the appropriate comparison.
|
Tagging subscribers to this area: @dotnet/area-system-globalization Issue DetailsThe right way to compare two strings in a case-insensitive manner is via a StringComparer/Comparison that specifies case-insensitivity. But there's are many cases where code is using ToLower() on each string and then comparing those for equality, e.g.
We should write an analyzer that flags such uses and recommends fixing them to use Equals with the appropriate comparison. This analyzer should cover Such analyzers will help avoid the allocations caused by ToLower/ToUpper calls. Performance rules Category
|
CC @Youssef1313 |
We should identify any of the equality cases with a string-case transformation
And replace them with the equivalent call to This should be merged into the same diagnostic ID as #78606 (if possible). |
|
I think it will be safe to do it if |
I'll work on this one next. It is closely related to my other one that's almost done: #78606 |
@tarekgh - For this analyzer, should I continue using the same rules I used in my previous PR?:
I ask because Jeremy's approval comment mentioned a non-existent comparison:
But there's also |
You will use |
I submitted a PR to fix this: dotnet/roslyn-analyzers#6720 @tarekgh question: If I have this comparison: a.ToLower() == a.ToLowerInvariant()` Am I right to assume that the substitution should be this?: a.Equals(b, StringComparison.CurrentCultureIgnorecase) My assumption is that |
@carlossanlop in such case we shouldn't provide a fix but only diagnostic telling this is can be optimized by using string comparison with ignore casing feature. We cannot know the intention of the caller if want to have the current culture or invariant casing at that time. |
The right way to compare two strings in a case-insensitive manner is via a StringComparer/Comparison that specifies case-insensitivity. But there's are many cases where code is using ToLower() on each string and then comparing those for equality, e.g.
We should write an analyzer that flags such uses and recommends fixing them to use Equals with the appropriate comparison.
This analyzer should cover
ToUpper
too even if the usage is low for that.Such analyzers will help avoid the allocations caused by ToLower/ToUpper calls.
Performance rules Category
Severity = suggestion
The text was updated successfully, but these errors were encountered: