Skip to content

fix(analyzers): exclude operators from CA1822 false positive#54517

Open
EduardF1 wants to merge 1 commit into
dotnet:mainfrom
EduardF1:fix/ca1822-operator-false-positive
Open

fix(analyzers): exclude operators from CA1822 false positive#54517
EduardF1 wants to merge 1 commit into
dotnet:mainfrom
EduardF1:fix/ca1822-operator-false-positive

Conversation

@EduardF1
Copy link
Copy Markdown

Summary

Excludes user-defined operator methods from the CA1822 ("Make member static") diagnostic, fixing a false positive introduced by C# 14 compound assignment operators.

Problem

CA1822 is triggered on C# 14 user-defined compound assignment operators such as:

class C
{
    public void operator +=(int a) { }
}

These operators are required to be instance methods in C# - they cannot legally be made static. Suggesting they be made static (CA1822) is a false positive and confusing to developers.

Traditional operator overloads (operator +, etc.) are already static and are excluded by the existing methodSymbol.IsStatic check.

Fix

In ShouldAnalyze(), add an early return for MethodKind.UserDefinedOperator and MethodKind.Conversion.

Fixes #51644

User-defined operators are already required to be static in C#,
so CA1822's suggestion to make them static is redundant and confusing.

C# 14 introduced compound assignment operators (e.g. operator +=) that
are specifically required to be instance methods and cannot be made
static. Flagging these with CA1822 is a false positive.

The fix excludes methods with MethodKind.UserDefinedOperator and
MethodKind.Conversion from the CA1822 analysis in ShouldAnalyze().

Fixes dotnet#51644

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@EduardF1 EduardF1 requested a review from a team as a code owner May 29, 2026 23:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CA1822 suggests making user-defined compound operators static

1 participant