Skip to content

KTSU0004 code fix deletes the else branch of the null check it replaces #64

Open

Description

@matt-edmondson

What's wrong

ManualNullCheckAnalyzer.AnalyzeIfStatement reports any if (x == null | x is null) throw new ArgumentNullException(...). It only inspects ifStatement.Statement and never looks at ifStatement.Else.
(ManualNullCheckAnalyzer.cs#L59-L89)

ManualNullCheckCodeFixProvider then replaces the whole IfStatementSyntax, including its else clause, with Ensure.NotNull(x);.
(ManualNullCheckCodeFixProvider.cs#L96-L104)

Failure scenario

if (value is null)
    throw new ArgumentNullException(nameof(value));
else
    _items.Add(value);

KTSU0004 is an error, so people apply the fix, often through Fix All. The result is:

Ensure.NotNull(value);

_items.Add(value) is gone. The fixed code still compiles, so nobody notices the lost behaviour.

Two related losses go through the same path:

  • Custom message dropped. new ArgumentNullException(nameof(x), "msg") becomes Ensure.NotNull(x).
  • Parameter name changed. When the checked expression is not the named parameter, the fix names the wrong thing. For example, if (opts.Logger == null) throw new ArgumentNullException(nameof(opts)) becomes Ensure.NotNull(opts.Logger), and the fix builds that argument as an IdentifierName containing a dot.

Suggested fix

  • Analyzer: skip the if when ifStatement.Else is not null. Alternatively, keep reporting it and have the fix emit Ensure.NotNull(x); followed by the else body.
  • Consider not reporting when the ArgumentNullException passes a message, or when its paramName does not match the checked expression.
  • Build the argument with SyntaxFactory.ParseExpression instead of IdentifierName.

Acceptance criteria

  • A code-fix unit test with an if/else shows that the else body is preserved, or that no diagnostic is reported.
  • There is no analyzer or code-fix unit test project today; test/ only has the end-to-end AnalyzerTriggerTests. Add a test based on Microsoft.CodeAnalysis.CSharp.CodeFix.Testing for this fix.

Activity

  1. added
    bug
    on Sep 29, 2026
  2. matt-edmondson commented on Sep 29, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    • Category: Bug
    • Priority: High. KTSU0004 is error severity, so people apply this fix, often through Fix All. The fixed code still compiles, which means the deleted else body, custom message or wrong parameter name goes unnoticed. The fix silently changes behaviour in every repository that uses the SDK.
    • Area / suggested assignment: Sdk.Analyzers, specifically ManualNullCheckAnalyzer and ManualNullCheckCodeFixProvider. Whoever owns the analyzers should take it.
    • Duplicates / in progress: No duplicate found, and no open PR covers it. KTSU0006 code fix can insert the new PackageReference into a TFM-conditional ItemGroup, so the diagnostic never clears #66 (the KTSU0006 code fix) is in the same area, and both issues ask for the first code-fix unit-test project (Microsoft.CodeAnalysis.CSharp.CodeFix.Testing). It makes sense to set that project up once and use it for both.
    • Next step: Stop reporting the diagnostic when there is an else, a message argument, or a paramName that doesn't match the checked expression. That is the smallest safe change. Then build the argument with ParseExpression and add the code-fix test.

    Generated by Claude Code

  3. matt-edmondson commented on Nov 2, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    I picked this up in an automated run and put it back because it is bigger than one fix-and-test cycle:

    Suggested order: merge #88 first, then add the code-fix test project (shared with #66). After that, the analyzer change is small. In ManualNullCheckAnalyzer.AnalyzeIfStatement, return early when ifStatement.Else is not null, when the ArgumentNullException has a message argument, or when its paramName doesn't match the checked expression. In the code fix, build the argument with SyntaxFactory.ParseExpression.

    Unassigned; no branch was pushed.


    Generated by Claude Code

  4. removed their assignment
    on Nov 2, 2026
  5. matt-edmondson commented on Nov 12, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    Still not actionable. It now depends on PR #114, not #88. #114 adds the code-fix test harness this issue needs (test/Sdk.Examples.Tests/Infrastructure/CodeFixHarness.cs and CodeFixTests.cs). Starting the KTSU0004 fix before #114 merges would set up a second harness or collide with that one. Once #114 merges, the fix is the small analyzer change described above, plus one if/else case in CodeFixTests. #66 waits on the same PR.


    Generated by Claude Code

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

    analyzersbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions