SonarSource / sonar-dotnet

Code analyzer for C# and VB.NET projects
https://redirect.sonarsource.com/plugins/csharp.html
GNU Lesser General Public License v3.0
773 stars 226 forks source link

New Rule Idea: avoid using string operations based on StringComparison.InvariantCulture #2959

Open muiriswoulfe opened 4 years ago

muiriswoulfe commented 4 years ago

Description

Consider adding a rule to replace usages of StringComparison.InvariantCulture and StringComparison.InvariantCultureIgnoreCase with StringComparison.Ordinal and StringComparison.OrdinalIgnoreCase respectively. This aligns with the advice provided at that https://docs.microsoft.com/dotnet/standard/base-types/best-practices-strings:

Do not use string operations based on StringComparison.InvariantCulture in most cases. One of the few exceptions is when you are persisting linguistically meaningful but culturally agnostic data.

The same rule should also cover replacing usages of StringComparer.InvariantCulture and StringComparer.InvariantCultureIgnoreCase with StringComparer.Ordinal and StringComparer.OrdinalIgnoreCase respectively.

Using the InvariantCulture versions in almost all cases results in redundant operations that just add performance overhead.

andrei-epure-sonarsource commented 4 years ago

Hi, Muiris, good to e-see you 😃 . And great to see you're using our products 😄

Thanks for the rule suggestion!

cc @nicolas-harraudeau-sonarsource