Skip to content

Fix the MA0166 message saying "available tokens" - #1513

Merged
meziantou merged 1 commit into
mainfrom
feature/ma0166-message-wording-95819e
Sep 12, 2026
Merged

meziantou merged 1 commit into
mainfrom
feature/ma0166-message-wording-95819e

Conversation

@meziantou

Copy link
Copy Markdown
Owner

What

MA0166 (Forward the TimeProvider to methods that take one) formats the list of TimeProvider expressions available in the scope into its message, but called them tokens:

MA0166: Use an overload with a TimeProvider, available tokens: this.timeProvider

The file was derived from UseAnOverloadThatHasCancellationTokenAnalyzer and the wording was never updated. It is confusing next to MA0032/MA0040, where the same wording really is about CancellationToken.

The message is now:

MA0166: Use an overload with a TimeProvider, available time providers: this.timeProvider

Also in this change

The same copy-paste left several identifiers talking about cancellation tokens; they are renamed to match what they actually hold:

  • availableCancellationTokens → availableTimeProviders
  • CreateProperties(string[] cancellationTokens, …) → timeProviders
  • IsArgumentImplicitlyDeclared(…, INamedTypeSymbol cancellationTokenSymbol, …) → timeProviderSymbol (the argument passed at the call site is already TimeProviderSymbol)

The comment above the (int)symbol.SpecialType is >= 1 and <= 45 fast path is expanded to say what the range means. Checking the Roslyn packages confirms the enum grew past 45 (System_Runtime_CompilerServices_InlineArrayAttribute and System_Runtime_CompilerServices_AsyncHelpers exist in 5.9 but not in 4.8), so the bound covers every special type of the oldest supported Roslyn version and the newer ones are simply not skipped — a missed fast path, not a correctness issue. The bound itself is left unchanged, as widening it is a behaviour change beyond this fix.

No behaviour other than the message text changes: the diagnostic properties consumed by the code fix (ParameterIndexKey, ParameterNameKey, PathsKey) are untouched.

Tests

No test asserted on the MA0166 message, so ReportsTheAvailableTimeProvidersInTheMessage is added to pin the wording, mirroring the equivalent MA0040 test.

  • UseAnOverloadThatHasTimeProviderAnalyzerTests: 18/18 passing on roslyn4.8, 4.14, 5.0, 5.6 and 5.9
  • dotnet run --project src/DocumentationGenerator: exit code 0, no markdown change (the docs surface rule titles, not message formats)

MA0166 reports the TimeProvider expressions available in the scope, but its
message called them "tokens", which was copied from
UseAnOverloadThatHasCancellationTokenAnalyzer along with the rest of the file.
It read as "Use an overload with a TimeProvider, available tokens: timeProvider",
which is confusing next to MA0032/MA0040, where the same wording really is about
CancellationToken.

Rename the identifiers that came from the same copy-paste and were still talking
about cancellation tokens, and explain the SpecialType range used to skip the
types that cannot contain a TimeProvider.

No test asserted on the message, so add one to pin the wording.
@meziantou
meziantou merged commit ff7fcc5 into main Sep 12, 2026
13 checks passed
@meziantou
meziantou deleted the feature/ma0166-message-wording-95819e branch September 12, 2026 20:18
This was referenced Sep 12, 2026
This was referenced Sep 28, 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