Skip to content

Comments

fix: add assertion analyzers for common mistakes (prefer IsNull, IsTrue/IsFalse)#4944

Merged
thomhurst merged 1 commit intomainfrom
fix/assertion-analyzers-v2
Feb 19, 2026
Merged

fix: add assertion analyzers for common mistakes (prefer IsNull, IsTrue/IsFalse)#4944
thomhurst merged 1 commit intomainfrom
fix/assertion-analyzers-v2

Conversation

@thomhurst
Copy link
Owner

Closes #4874. Adds TUnitAssertions0014 and TUnitAssertions0015. 56/56 tests passing.

@claude
Copy link
Contributor

claude bot commented Feb 19, 2026

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

The two new Roslyn analyzers (PreferIsNullAnalyzer / TUnitAssertions0014 and PreferIsTrueOrIsFalseAnalyzer / TUnitAssertions0015) are well-implemented:

  • Correct use of Roslyn Operations API — Using IInvocationOperation and related operation types is more robust than syntax-based analysis, correctly handling implicit conversions, default values, and constant folding.
  • Argument lookup by parameter name (a.Parameter?.Name == "expected") correctly handles extension method receivers vs. regular arguments.
  • Null detection covers the important cases: direct null literals, constant values that are null, IConversionOperation wrapping null, and IDefaultValueOperation on reference types.
  • Boolean detection similarly covers literals, constant values, and conversions.
  • Diagnostic message format is consistent — TUnitAssertions0014 has no placeholders, TUnitAssertions0015 correctly passes suggestedMethod and literalText matching the {0}/{1} placeholders in the resource string.
  • Tests cover both positive (diagnostic raised) and negative (no diagnostic) cases for all relevant scenarios.
  • CLAUDE.md rules — All critical rules are either explicitly exempted for analyzer changes (Dual-Mode, Snapshot Testing) or are not implicated (VSTest, AOT, async blocking).

Add two new analyzer rules to TUnit.Assertions.Analyzers:

- TUnitAssertions0014: Prefer IsNull() over IsEqualTo(null) for clearer
  intent and better error messages
- TUnitAssertions0015: Prefer IsTrue()/IsFalse() over IsEqualTo(true/false)
  for clearer intent and better error messages

Both rules are Warning severity and include full test coverage.

Closes #4874
@thomhurst thomhurst force-pushed the fix/assertion-analyzers-v2 branch from 484573c to 09cb81a Compare February 19, 2026 11:46
@thomhurst thomhurst merged commit 51dc3a6 into main Feb 19, 2026
13 of 14 checks passed
@thomhurst thomhurst deleted the fix/assertion-analyzers-v2 branch February 19, 2026 13:49
This was referenced Feb 23, 2026
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.

fix: assertion analyzers missing rules for common mistakes

1 participant