Add tests for code paths not covered by the test suite

Merged
ME
2mo ago
+538-2

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.