Report MA0068 on the parameter and property placements of NotNullIfNotNull

Merged
ME
2mo ago
+284-20

What

MA0068 (invalid parameter name for nullable attribute) only inspected GetReturnTypeAttributes(), so it only validated the [return: NotNullIfNotNull("...")] placement. NotNullIfNotNullAttribute is declared with AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, and the same typo was silently ignored on an out/ref parameter and on a property:

public class Sample
{
    // no diagnostic before this change, although "doesNotExist" is not a parameter
    public void M(string? a, [NotNullIfNotNull("doesNotExist")] out string? b) => b = a;

    [return: NotNullIfNotNull("doesNotExist")] // MA0068
    public string? N(string? a) => a;
}

How

  • A SymbolKind.Property symbol action is registered next to the existing SymbolKind.Method one, and the attribute loop is shared by the four placements: the return type attributes of a method, the attributes of each of its parameters, the attributes of a property, and the attributes of each parameter of an indexer.
  • The parameters are analyzed from their declaring method or indexer rather than being registered directly: SymbolKind.Parameter is not a symbol kind the Roslyn analyzer driver raises symbol actions for (it only raises them for namespaces, types, methods, fields, properties and events), so RegisterSymbolAction(..., SymbolKind.Parameter) would never run.
  • The candidate names come from the declaring symbol: the parameters of the method for a method; for a property, the parameters of the setter when there is one — the parameters of the indexer plus the implicit value parameter, which is how the compiler binds the name for the set accessor — and the parameters of the indexer otherwise. The extension parameter keeps being a candidate in both cases, under the existing CSHARP14_OR_GREATER guard.
  • The parameters of the accessors of an indexer are skipped (AssociatedSymbol is IPropertySymbol). An attribute written on an indexer parameter surfaces both on the parameter of the indexer and on the parameter of the accessor, which reported the attribute twice; this was confirmed with a test before adding the guard. The return type attributes of an accessor are still analyzed, so [return: NotNullIfNotNull(...)] on a get accessor is reported.

Notes for the reviewer

  • A property with no setter and no parameters has no candidate name, so any name is reported. [NotNullIfNotNull("value")] is therefore compliant on { get; set; } and reported on a get-only property.
  • The sample of docs/Rules/MA0068.md was also incorrect: its compliant case was commented as "a" is **not** a parameter of the method, and both methods were named A. It now shows the parameter, property and indexer placements, and states the value/indexer rule.

Tests

9 cases added to NullableAttributeUsageAnalyzerTests (out parameter valid and invalid, property invalid, value on a settable property and on a get-only one, indexer name valid and invalid, indexer parameter attribute, accessor [return:]), plus 3 extension block cases for the property and parameter placements.

dotnet build succeeds with no warning, the MA0068 tests pass on every Roslyn version (11 on 4.8 and 4.14, 16 on 5.0, 5.6 and 5.9), and the full 5.9 suite passes (4606 tests). dotnet run --project src/DocumentationGenerator exits 0 with no further change.