Do not suggest instance members in static contexts in MA0166/MA0167 - #1623
Merged
Merged
Conversation
LookupAvailableSymbols only recognized static methods as static contexts, so the instance fields and properties of the type were suggested as the TimeProvider to forward in static properties, static constructors, static operators, static event accessors, field and property initializers, and constructor initializers. The members of a containing type were also suggested in a nested type. Primary constructor parameters are now handled explicitly: they can be used in instance members, instance initializers and the arguments of the base type, but not in static members or in constructor initializers. MA0084 uses the same logic to decide whether a local hides a primary constructor parameter. Fixes #793 (comment)
CSharpExtensions.GetDeclaredSymbol and GetSymbolInfo accept a nullable SemanticModel.
This was referenced Oct 3, 2026
Open
This was referenced Oct 5, 2026
Open
Open
Open
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to this comment on #793, which reports a false positive in MA0166.
Problem
MA0166 reports when a
TimeProvideris available in scope. To find what's available,LookupAvailableSymbolscallsSemanticModel.LookupSymbols, which also returns instance members that can't be used at that point. The code that filtered them out (IsInStaticContext) only recognized static methods. As a result, MA0166 reported things likeavailable time providers: _timeProvider, and the code fix would have produced code that doesn't compile, in:: this(...)/: base(...)constructor initializersChanges
OperationExtensions: replaceIsInStaticContextwith aGetStaticContexthelper that handles the contexts above.CS9105).LocalVariablesShouldNotHideSymbolsAnalyzer) usedIsInStaticContext, so it now usesCanUsePrimaryConstructorParameters. Side effect: it no longer reports a local in a static property, a static field initializer or a constructor initializer as hiding a primary constructor parameter.docs/Rules/MA0166.md: explain why the rule exists. Overloads without aTimeProvideruseTimeProvider.System, so forwarding an injected provider keeps the code testable with a fake clock. This addresses the "outdated" point in the comment.Tests
UseAnOverloadThatHasTimeProviderAnalyzerTestscases for each context above, plus the primary-constructor cases (available and not available).LocalVariablesShouldNotHideSymbolsAnalyzerTestscases for static contexts and instance field initializers.JsonSourceGenerationOptionsAnalyzerTestson Roslyn 4.14 with network socket errors. That class passed on a re-run.