fix(analyzers): exclude operators from CA1822 false positive#54517
Open
EduardF1 wants to merge 1 commit into
Open
fix(analyzers): exclude operators from CA1822 false positive#54517EduardF1 wants to merge 1 commit into
EduardF1 wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
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 alreadystaticand are excluded by the existingmethodSymbol.IsStaticcheck.Fix
In
ShouldAnalyze(), add an early return forMethodKind.UserDefinedOperatorandMethodKind.Conversion.Fixes #51644