Fix MA0166 on types that derive from TimeProvider and on non-readable members

Merged
ME
2mo ago
+98-4

What

MA0166 (Use an overload with a TimeProvider argument) did not recognize a variable whose type derives from TimeProvider as a provider, and enumerated its members instead, finding the inherited static TimeProvider.System:

class Test
{
    void A(MyTimeProvider foo)
    {
        Task.Delay(TimeSpan.Zero);
    }

    class MyTimeProvider : TimeProvider { }
}

Expected fix: Task.Delay(TimeSpan.Zero, foo). Actual: Task.Delay(TimeSpan.Zero, foo.System), which does not compile (CS0176).

Like MA0040, the member paths also included members that cannot be read through an instance: static fields and properties, write-only properties, properties whose getter is not accessible from the call site, and indexers.

Fix

  • A variable or member whose type is or derives from TimeProvider is a provider (IsOrInheritsFrom instead of IsEqualTo).
  • When collecting the members of the variables in scope, static members, indexers, and properties without a getter are skipped, and the accessibility check of a property checks its getter.

Tests

Added to UseAnOverloadThatHasTimeProviderAnalyzerTests (all failed before the fix):

  • WhenAvailable_DerivedTimeProviderParameter (code fix passes foo)
  • WhenAvailable_NestedPropOfDerivedType (code fix passes foo.A)
  • NotAvailable_NonUsableMember theory (static property, static field, write-only property, private getter, indexer)

The test class passes on roslyn5.9 (28/28) and roslyn4.8 (28/28).