Repository navigation
Conversation
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
|
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. |
Author
|
@dotnet-policy-service agree |
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
approved these changes
Nov 19, 2026
Evangelink
left a comment
Member
There was a problem hiding this comment.
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
enabled auto-merge (squash)
November 19, 2026 17:20
This branch has not been deployed
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.
Fixes #56775
The
ThrowIfNullfixer 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 anArrowExpressionClauseSyntax:void M(int x) => ArgumentNullException.ThrowIfNull(x);(and ctor, finalizer,set/initaccessor, indexerset, local function)void M(int x) ;(CS0501)InvalidCastExceptionHasValuecheck for CA1871y => ArgumentNullException.ThrowIfNull(y)ArgumentNullExceptionSub(y) ArgumentNullException.ThrowIfNull(y)NullReferenceExceptionThe 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 abstractCanRewrite(statement)check. BothRegisterCodeFixesAsyncand the fix-allApplyFixcall it, so a fix-all pass, such asdotnet format, also skips the shapes it cannot rewrite. Statement removal moves into a virtualRemoveStatementthat keeps the current behaviour.CSharpDoNotPassNonNullableValueToArgumentNullExceptionThrowIfNullFixer.cs: the C# fix can rewrite anExpressionStatementSyntaxor anArrowExpressionClauseSyntax. For an arrow clause, it replaces the declaration throughSyntaxGenerator.WithStatements, which gives an empty block (CA2264) or a block that holds theifcheck (CA1871).BasicDoNotPassNonNullableValueToArgumentNullExceptionThrowIfNullFixer.vb: the VB fix is not offered when the statement is the body of aSingleLineLambdaExpressionSyntax.Operators, conversion operators, and property or indexer getters are not covered.
ThrowIfNullreturnsvoid, 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
forstatement's initializer or incrementor list. There, the call's parent is theForStatementSyntax, 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_DiagnosticNotNullable_ExpressionBodiedDestructor_DiagnosticNotNullable_ExpressionBodiedAccessor_Diagnostic(set,init; C# 9)NotNullable_ExpressionBodiedIndexerAccessor_DiagnosticNotNullable_ExpressionBodiedLocalFunction_DiagnosticNotNullable_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 asTriviaIsNotPreserved_Diagnostic.Nullable_ExpressionBodiedMethod_Diagnostic: CA1871 produces a block that holds theHasValuecheck.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.mdand.sarif.templatefiles are unchanged.Not in scope (same fixer, found while probing)
if (c) ArgumentNullException.ThrowIfNull(x);, the fixer throwsArgumentNullException(parameterstatement).If: withIf c Then ArgumentNullException.ThrowIfNull(x), the fix producesIf c Then End Sub.I can follow up on these separately if that would help.