Skip to content

Add tests for code paths not covered by the test suite - #1527

Merged
meziantou merged 1 commit into
mainfrom
feature/code-coverage-improvements-7c937a
Sep 15, 2026
Merged

meziantou merged 1 commit into
mainfrom
feature/code-coverage-improvements-7c937a

Conversation

@meziantou

Copy link
Copy Markdown
Owner

This PR only adds tests. No analyzer or code fixer code changes. I found the gaps by running the Roslyn 5.9 test suite with code coverage, and the new tests cover about 150 more lines and 130 more branches.

Tests added

  • MA0015 / MA0043:
    • A test that ArgumentNullException.ThrowIfNull(arg, "arg") is fixed to use nameof.
    • Valid and invalid argument names for InvalidEnumArgumentException.
  • MA0194: fixer tests for:
    • Other terms between the checks that can be merged.
    • Checks that switch to another value.
    • An expression nested in a parenthesized logical expression.
    • Constant patterns joined with && that can't be merged next to ones that can.
    • Parenthesized values.
    • Events.
  • MA0192: (value & Flag) == 0 for every enum underlying type, using the highest bit (reported) and a combined flag (not reported).
  • MA0005:
    • 0L and 0u sizes, and multi-dimensional arrays.
    • Compiler-generated params arrays for constructors and indexers.
  • MA0003:
    • The minimum_method_parameters option, including an invalid value.
    • A single params element.
    • The ReferenceEquals and Parse / TryParse exclusions.
    • SyntaxNode.With* methods with several parameters.
  • MA0151: indexers, the ! operator, parenthesized expressions and blank expressions.
  • MA0011: string.Format and StringBuilder.AppendFormat with an explicit object[].
  • MA0076:
    • nuint, StringBuilder and Version.
    • The WPF FontStretch / Brush and NuGet SemanticVersion types, as stubs that implement IFormattable so the exclusion is what prevents the diagnostic.
  • MA0110: match timeouts that overflow the tick arithmetic, and timeouts computed with an instance method.
  • MA0193: a type declared in source that implements IFloatingPoint<T> without explicit interface syntax.

MinimumNumberOfParameters_2 passed without reading the option, because its argument (new object()) is never reported. It now passes a boolean literal, so the option is what prevents the diagnostic.

Notes for reviewers

  • Bug not fixed here: MA0011 does not report collection expressions such as string.Format("", ["test", -1]). The argument is wrapped in an implicit conversion to ReadOnlySpan<object?>, so the ICollectionExpressionOperation checks in CultureSensitiveFormattingContext never match. The fix would add new diagnostics, so it belongs in its own PR, and this PR has no tests for it.
  • Code no test can reach:
    • The MA0003 exclusions for MethodBase.Invoke, FieldInfo / PropertyInfo.GetValue and SetValue never run, because their first parameter is named obj and is filtered out earlier.
    • The MA0003 Volatile.Read exclusion never runs, because its only parameter is ref and can't take a literal.
    • MA0042 checks IVariableDeclarationOperation.Initializer, which is only set for VB.

Testing

  • dotnet build: 0 errors, 0 warnings.
  • dotnet test on all Roslyn versions (4.8, 4.14, 5.0, 5.6, 5.9): 23,625 tests passed.

Cover the branches found with code coverage in MA0003, MA0005, MA0011,
MA0015, MA0043, MA0076, MA0110, MA0151, MA0192, MA0193 and MA0194.

MinimumNumberOfParameters_2 did not exercise the option, as its argument
is never reported, so it now uses a boolean literal.
@meziantou
meziantou enabled auto-merge (squash) September 15, 2026 15:28
@meziantou
meziantou merged commit a44471e into main Sep 15, 2026
13 checks passed
@meziantou
meziantou deleted the feature/code-coverage-improvements-7c937a branch September 15, 2026 15:29
This was referenced Sep 18, 2026
This was referenced Sep 26, 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.

1 participant