Skip to content
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

Prefer string.Equals over string.Compare(string, string) == 0 or != 0 #34514

Closed
Tracked by #79053
AArnott opened this issue Apr 3, 2020 · 7 comments · Fixed by #82438
Closed
Tracked by #79053

Prefer string.Equals over string.Compare(string, string) == 0 or != 0 #34514

AArnott opened this issue Apr 3, 2020 · 7 comments · Fixed by #82438
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime code-analyzer Marks an issue that suggests a Roslyn analyzer code-fixer Marks an issue that suggests a Roslyn code fixer
Milestone

Comments

@AArnott
Copy link
Contributor

AArnott commented Apr 3, 2020

As a carryover from native code habits, I see folks use string.Compare(a, b) == 0 or != 0 a lot where string.Equals(a, b) is more semantically meaningful.

But more importantly, Compare is for sorting strings whereas Equals is for determining equality. In particular cultures two strings may have sort equivalence without being equal. So in some cases it may even make a security difference (when StringComparison isn't specified).

Category: Performance

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Runtime untriaged New issue has not been triaged by the area owner labels Apr 3, 2020
@AArnott
Copy link
Contributor Author

AArnott commented Apr 3, 2020

@terrajobst to consider for inclusion in the Roslyn Analyzers project.

@AArnott AArnott added code-analyzer Marks an issue that suggests a Roslyn analyzer code-fixer Marks an issue that suggests a Roslyn code fixer labels Apr 3, 2020
@danmoseley
Copy link
Member

cc @GrabYourPitchforks who I think has commented on this before.

@GrabYourPitchforks
Copy link
Member

string.Equals(...) and string.Compare(...) == 0 are semantically equivalent as long as the same StringComparison is used.

The thing that messes people up is that if you don't specify an explicit comparison, string.Equals defaults to Ordinal, while string.Compare defaults to CurrentCulture.

IMO this particular issue should be performance rather than correctness. Let existing issue dotnet/roslyn-analyzers#2581 (comment) be the "correctness / security" issue.

@AArnott AArnott closed this as completed Apr 4, 2020
@GrabYourPitchforks
Copy link
Member

Shouldn't we keep this issue open? There is some benefit to having devs prefer string.Equals(...) over the equivalent string.Compare(...) == 0 (or != 0).

@AArnott
Copy link
Contributor Author

AArnott commented Apr 5, 2020

Oh, I misunderstood. I thought you were suggesting we close it in favor of the other issue. But now I see that you simply suggested I revise the category.

@AArnott AArnott reopened this Apr 5, 2020
@joperezr joperezr removed the untriaged New issue has not been triaged by the area owner label Jul 7, 2020
@joperezr joperezr added this to the Future milestone Jul 7, 2020
@stephentoub stephentoub modified the milestones: Future, 8.0.0 Nov 17, 2022
@buyaa-n buyaa-n added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Dec 20, 2022
@EgorBo
Copy link
Member

EgorBo commented Feb 21, 2023

There are a few hits in dotnet/runtime itself:

image

cc @stephentoub

@stephentoub
Copy link
Member

stephentoub commented Feb 21, 2023

We have https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2251. This issue should have been closed with that.

Regarding "There are a few hits in dotnet/runtime itself", those are all for string.CompareOrdinal rather than string.Compare. We should open an issue in dotnet/roslyn-analyzers to update CA2251 to also look for that method, at least the two-argument one... string.Equals doesn't provide an equivalent overload for the five-argument one.
dotnet/roslyn-analyzers#6498

@ghost ghost added in-pr There is an active PR which will close this issue when it is merged and removed in-pr There is an active PR which will close this issue when it is merged labels Feb 21, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Mar 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime code-analyzer Marks an issue that suggests a Roslyn analyzer code-fixer Marks an issue that suggests a Roslyn code fixer
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants