Fix MA0166 on types that derive from TimeProvider and on non-readable members
Merged
ME
2mo ago+98+98+01234567890123456789-4-4-0123456789
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
TimeProvideris a provider (IsOrInheritsFrominstead ofIsEqualTo). - 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 passesfoo)WhenAvailable_NestedPropOfDerivedType(code fix passesfoo.A)NotAvailable_NonUsableMembertheory (static property, static field, write-only property, private getter, indexer)
The test class passes on roslyn5.9 (28/28) and roslyn4.8 (28/28).