Skip to content

Fix CA2264/CA1871 code fixes for expression-bodied members - #56776

Open
Hanif6731 wants to merge 2 commits into
dotnet:mainfrom
Hanif6731:fix/ca2264-expression-bodied-member
Open

Hanif6731 wants to merge 2 commits into
dotnet:mainfrom
Hanif6731:fix/ca2264-expression-bodied-member

Conversation

@Hanif6731

Copy link
Copy Markdown

Fixes #56775

The ThrowIfNull fixer assumed the call's parent is a statement. When the call is the whole expression body of a member or local function, the parent is an ArrowExpressionClauseSyntax:

Shape CA2264 before CA1871 before After
void M(int x) => ArgumentNullException.ThrowIfNull(x); (and ctor, finalizer, set/init accessor, indexer set, local function) void M(int x) ; (CS0501) fixer throws InvalidCastException block body: empty for CA2264, the HasValue check for CA1871
C# lambda y => ArgumentNullException.ThrowIfNull(y) fixer throws ArgumentNullException same path no fix offered (the diagnostic is still reported)
VB Sub(y) ArgumentNullException.ThrowIfNull(y) fixer throws NullReferenceException same path no fix offered (the diagnostic is still reported)

The issue says local functions were left unchanged. The tests show they hit the same ; defect as members, so this PR fixes them too.

Change

  • DoNotPassNonNullableValueToArgumentNullExceptionThrowIfNull.Fixer.cs (shared): adds an abstract CanRewrite(statement) check. Both RegisterCodeFixesAsync and the fix-all ApplyFix call it, so a fix-all pass, such as dotnet format, also skips the shapes it cannot rewrite. Statement removal moves into a virtual RemoveStatement that keeps the current behaviour.
  • CSharpDoNotPassNonNullableValueToArgumentNullExceptionThrowIfNullFixer.cs: the C# fix can rewrite an ExpressionStatementSyntax or an ArrowExpressionClauseSyntax. For an arrow clause, it replaces the declaration through SyntaxGenerator.WithStatements, which gives an empty block (CA2264) or a block that holds the if check (CA1871).
  • BasicDoNotPassNonNullableValueToArgumentNullExceptionThrowIfNullFixer.vb: the VB fix is not offered when the statement is the body of a SingleLineLambdaExpressionSyntax.

Operators, conversion operators, and property or indexer getters are not covered. ThrowIfNull returns void, so it cannot be the expression body of a member that returns a value.

Side effect

In C#, the fix is no longer offered when the call is inside a for statement's initializer or incrementor list. There, the call's parent is the ForStatementSyntax, and the old fix removed the entire loop.

Tests

All tests are in DoNotPassStructToArgumentNullExceptionThrowIfNullTests.cs. Each one is a code-fix test, and each one failed before the fix:

  • NotNullable_ExpressionBodiedMethod_Diagnostic: the repro from the issue. The method becomes an empty block.
  • NotNullable_ExpressionBodiedConstructor_Diagnostic
  • NotNullable_ExpressionBodiedDestructor_Diagnostic
  • NotNullable_ExpressionBodiedAccessor_Diagnostic (set, init; C# 9)
  • NotNullable_ExpressionBodiedIndexerAccessor_Diagnostic
  • NotNullable_ExpressionBodiedLocalFunction_Diagnostic
  • NotNullable_ExpressionBodiedMethod_TriviaIsPreserved_Diagnostic: the leading comment and the blank line before the next member are kept. The comment after the removed call is dropped, the same as TriviaIsNotPreserved_Diagnostic.
  • Nullable_ExpressionBodiedMethod_Diagnostic: CA1871 produces a block that holds the HasValue check.
  • NotNullable_ExpressionBodiedLambda_Diagnostic: the diagnostic is reported and no fix is offered.
  • NotNullable_FixAllSkipsExpressionBodiedLambda_Diagnostic: fix-all fixes the expression-bodied method, leaves the lambda alone, and its diagnostic remains.
  • Vb_NotNullable_SingleLineLambda_Diagnostic: the diagnostic is reported and no fix is offered.

Microsoft.CodeAnalysis.NetAnalyzers.UnitTests: 18,006 passed, 25 skipped, 0 failed. The filtered class has 138 tests, all passing. The analyzer solution builds with 0 warnings. The generated .md and .sarif.template files are unchanged.

Not in scope (same fixer, found while probing)

  • C# embedded statement: with if (c) ArgumentNullException.ThrowIfNull(x);, the fixer throws ArgumentNullException (parameter statement).
  • VB single-line If: with If c Then ArgumentNullException.ThrowIfNull(x), the fix produces If c Then End Sub.

I can follow up on these separately if that would help.

The fixer treated the parent of the `ThrowIfNull` call as a statement. When
the call is the whole expression body of a member or local function, that
parent is the arrow clause: removing it left `void M(int x) ;` (CS0501), and
the CA1871 fix threw an InvalidCastException. The declaration now gets a block
body instead: empty for CA2264, holding the `HasValue` check for CA1871.

The call as the body of an expression-bodied lambda (C#) or a single-line
`Sub` lambda (VB) has no statement to rewrite, and the fix threw there. It is
no longer offered for those shapes, in a single fix or a fix-all pass.

Fixes dotnet#56775
@Hanif6731
Hanif6731 requested a review from a team as a code owner October 24, 2026 15:46
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
2 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@Hanif6731

Copy link
Copy Markdown
Author

@dotnet-policy-service agree

@Hanif6731

Copy link
Copy Markdown
Author

Hi @Evangelink, a gentle ping when you have a moment. This fixes the linked CA2264 issue that was assigned to you. Checks are green. Happy to update the branch from main or change anything you'd like.

@Evangelink Evangelink left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

This review was generated with AI assistance.

No actionable findings after reviewing the expression-bodied C# and VB code-fix paths, fix-all behavior, and regression tests. The PR's linked issue scenario is addressed, and the current PR checks pass.

Findings: 0 correctness bugs, 0 regression-coverage gaps, 0 other issues.

@Evangelink
Evangelink enabled auto-merge (squash) November 19, 2026 17:20

This branch has not been deployed

No deployments
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.

CA2264 code fix produces uncompilable code for an expression-bodied member

2 participants