You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
KTSU0004 code fix deletes the else branch of the null check it replaces #64
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 wholeIfStatementSyntax, including its else clause, with Ensure.NotNull(x);.
(ManualNullCheckCodeFixProvider.cs#L96-L104)
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.
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.
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.
I picked this up in an automated run and put it back because it is bigger than one fix-and-test cycle:
The test project doesn't exist yet. The acceptance criteria call for a Microsoft.CodeAnalysis.CSharp.CodeFix.Testing project. test/ has only Sdk.Examples.Tests, which is kept out of Sdk.sln and run by a dedicated CI job. A new unit-test project needs its own csproj, a decision about whether it joins Sdk.sln, and CI wiring. KTSU0006 code fix can insert the new PackageReference into a TFM-conditional ItemGroup, so the diagnostic never clears #66 wants the same project, so it should be set up once, deliberately, and not as a side effect of this fix.
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.
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.
Activity
matt-edmondson commentedon Sep 29, 2026
Triage
elsebody, custom message or wrong parameter name goes unnoticed. The fix silently changes behaviour in every repository that uses the SDK.Sdk.Analyzers, specificallyManualNullCheckAnalyzerandManualNullCheckCodeFixProvider. Whoever owns the analyzers should take it.Microsoft.CodeAnalysis.CSharp.CodeFix.Testing). It makes sense to set that project up once and use it for both.else, a message argument, or aparamNamethat doesn't match the checked expression. That is the smallest safe change. Then build the argument withParseExpressionand add the code-fix test.Generated by Claude Code
matt-edmondson commentedon Nov 2, 2026
Triage
I picked this up in an automated run and put it back because it is bigger than one fix-and-test cycle:
Microsoft.CodeAnalysis.CSharp.CodeFix.Testingproject.test/has onlySdk.Examples.Tests, which is kept out ofSdk.slnand run by a dedicated CI job. A new unit-test project needs its own csproj, a decision about whether it joinsSdk.sln, and CI wiring. KTSU0006 code fix can insert the new PackageReference into a TFM-conditional ItemGroup, so the diagnostic never clears #66 wants the same project, so it should be set up once, deliberately, and not as a side effect of this fix.InternalsVisibleTo Include="Sdk.Examples.Tests"toSdk.Analyzers.csprojand aProjectReferenceto the analyzers fromSdk.Examples.Tests. Wiring analyzer tests now would collide with it.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 whenifStatement.Else is not null, when theArgumentNullExceptionhas a message argument, or when itsparamNamedoesn't match the checked expression. In the code fix, build the argument withSyntaxFactory.ParseExpression.Unassigned; no branch was pushed.
Generated by Claude Code
matt-edmondson commentedon Nov 12, 2026
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.csandCodeFixTests.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 inCodeFixTests. #66 waits on the same PR.Generated by Claude Code