dotnet / roslynator

Roslynator is a set of code analysis tools for C#, powered by Roslyn.
https://josefpihrt.github.io/docs/roslynator
Other
3.04k stars 251 forks source link

RCS1155: use StringComparison false positive #641

Open PhilPJL opened 4 years ago

PhilPJL commented 4 years ago

Product and Version Used: Roslynator 2.3.0 Visual Studio 16.4.4

Steps to Reproduce:

Consider this Linq code running against a SQL database with a case sensitive collation.

This is attempting to do a case insensitive search

KitJournals.Where (k => k.UserId.ToUpper() == "osullirx".ToUpper()).Dump();

and results in this SQL

SELECT 
    ... 
    FROM [dbo].[KitJournal] AS [Extent1]
    WHERE ((UPPER([Extent1].[UserId])) = (UPPER(N'osullirx'))) 
      OR ((UPPER([Extent1].[UserId]) IS NULL) AND (UPPER(N'osullirx') IS NULL))

Accepting RCS1155 suggestion changes the code to:

KitJournals.Where (k => k.UserId.Equals("osullirx", StringComparison.OrdinalIgnoreCase)).Dump();

which results in this SQL which does a case sensitive search, the opposite of what was expected

SELECT 
    ...
    FROM [dbo].[KitJournal] AS [Extent1]
    WHERE N'osullirx' = [Extent1].[UserId]
josefpihrt commented 4 years ago

Possible duplicate of #440