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

Warn against comparing the result of string.ToUpper / string.ToLower. #156

Open
brian-reichle opened this issue Jun 30, 2021 · 2 comments
Open
Milestone

Comments

@brian-reichle
Copy link
Contributor

brian-reichle commented Jun 30, 2021

If the result of string.ToUpper or string.ToLower is simply used in a comparison, then that's a pretty good indication that a case-insensitive comparison should be used instead.

foo.ToUpper() == "FOO"
"foo" == foo.ToLower(CultureInfo.InvariantCulture)
string.Equals(foo.ToUpper(CultureInfo.InvarintCulture), "FOO")
foo.ToUpper().CompareTo("FOO")
string.Compare(foo.ToUpper(), "FOO", StringComparison.Ordinal)

Should become:

foo.Equals("FOO", StringComparison.CurrentCultureIgnoreCase)
"foo".Equals(foo, StringComparison.InvariantCultureIgnoreCase)
string.Equals(foo, "FOO", StringComparison.InvarintCultureIgnoreCase)
string.Compare(foo, "FOO", StringComparison.CurrentCultureIgnoreCase)
string.Compare(foo "FOO", StringComparison.OrdinalIgnoreCase)

Should probably report things like the following as errors on the basis that they well never match:

foo.ToUpper(CultureInfo.InvariantCulture) == "Foo"
foo.ToLower(CultureInfo.InvariantCulture) == "Foo"
@yaakov-h
Copy link
Member

Should the bottom section be one of those "replace with false" code-fixes? 😆

@brian-reichle
Copy link
Contributor Author

brian-reichle commented Jun 30, 2021

That's what I was thinking, or rather "replace with false and simplify".

@brian-reichle brian-reichle added this to the 3.7.1 milestone Jan 18, 2022
@yaakov-h yaakov-h modified the milestones: 3.7.1, 3.7.2 Nov 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants