Skip to content

CA1862 false positive with the same variable on both sides of the comparison #7074

Open
@xiety

Description

Analyzer

Diagnostic ID: CA1862: Use the 'StringComparison' method overloads to perform case-insensitive string comparisons

Analyzer source

SDK: Built-in CA analyzers in .NET 8 SDK

Version: SDK 8.0.100

Describe the bug

CA1862 should not be shown when the same variable is on both sides of the comparison.

Steps To Reproduce

if (name.ToLower() == name)
    Console.WriteLine("all letters are lowercase");

And the suggested fix changes the behavior completely:

// always true now
if (name.Equals(name, StringComparison.CurrentCultureIgnoreCase))
    Console.WriteLine("all letters are lowercase");

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-Microsoft.CodeAnalysis.NetAnalyzersBugThe product is not behaving according to its current intended designFalse_PositiveA diagnostic is reported for non-problematic casehelp wantedThe issue is up-for-grabs, and can be claimed by commenting

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions