dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.04k stars 4.03k forks source link

Some uses of StringComparer.OrdinalIgnoreCase instead of CaseInsensitiveComparison.Comparer #26034

Open gafter opened 6 years ago

gafter commented 6 years ago

If you need a StringComparer that performs case-mapping, you have two choices:

  1. StringComparer.OrdinalIgnoreCase
  2. CaseInsensitiveComparison.Comparer

These two case-mapping techniques differ in how they are implemented, and therefore in how they behave.

The Roslyn code base is inconsistent in how it uses these two. The most common error is using the former when the latter would be more appropriate. Here two examples of probably incorrect uses in the compiler:

We should probably survey the rest of Roslyn for other incorrect uses.

CyrusNajmabadi commented 6 years ago

Consider having a central location that exposes teh right comparers to use, with teh right names to help pick which one you want. For example: StringComparers.FileSystemNameComparer. that would go a long way toward helping people make the right choices.

AdamSpeight2008 commented 6 years ago

A StringComparers.IdentifierNameComparer would also help. @gafter Il can go through them if you like?

AdamSpeight2008 commented 6 years ago

Gist of All StringComparer In Roslyn

AdamSpeight2008 commented 6 years ago

@gafter We should also consider auditing the use of CaseInsensitiveComparison.ToLower