11using System . Collections . Generic ;
22using System . Collections . Immutable ;
3+ using System . Threading ;
34using Microsoft . CodeAnalysis ;
5+ using Microsoft . CodeAnalysis . CSharp ;
46using Microsoft . CodeAnalysis . CSharp . Syntax ;
57using Microsoft . CodeAnalysis . Diagnostics ;
68using Microsoft . CodeAnalysis . Operations ;
@@ -41,7 +43,7 @@ private static void AnalyzeVariableDeclaration(OperationAnalysisContext context)
4143 if ( containingType == null )
4244 return ;
4345
44- foreach ( var member in GetSymbols ( containingType , localSymbol . Name ) )
46+ foreach ( var member in GetSymbols ( containingType , localSymbol . Name , context . CancellationToken ) )
4547 {
4648 if ( ! semanticModel . IsAccessible ( operation . Syntax . SpanStart , member ) )
4749 continue ;
@@ -57,6 +59,12 @@ private static void AnalyzeVariableDeclaration(OperationAnalysisContext context)
5759 ReportDiagnostic ( "property" ) ;
5860 return ;
5961 }
62+
63+ if ( member is IParameterSymbol && ! operation . IsInStaticContext ( context . CancellationToken ) )
64+ {
65+ ReportDiagnostic ( "parameter" ) ;
66+ return ;
67+ }
6068 }
6169
6270 void ReportDiagnostic ( string type )
@@ -72,8 +80,32 @@ void ReportDiagnostic(string type)
7280 }
7381 }
7482
75- private static IEnumerable < ISymbol > GetSymbols ( INamedTypeSymbol ? type , string name )
83+ private static IEnumerable < ISymbol > GetSymbols ( INamedTypeSymbol ? type , string name , CancellationToken cancellationToken )
7684 {
85+ #if CSHARP12_OR_GREATER
86+ if ( type ? . InstanceConstructors is not null )
87+ {
88+ foreach ( var constructor in type . InstanceConstructors )
89+ {
90+ if ( constructor . Parameters . Length == 0 )
91+ continue ;
92+
93+ foreach ( var syntaxRef in constructor . DeclaringSyntaxReferences )
94+ {
95+ var syntax = syntaxRef . GetSyntax ( cancellationToken ) ;
96+ if ( syntax . IsKind ( SyntaxKind . ClassDeclaration ) || syntax . IsKind ( SyntaxKind . StructDeclaration ) )
97+ {
98+ var typeDeclaration = ( TypeDeclarationSyntax ) syntax ;
99+ foreach ( var param in constructor . Parameters )
100+ {
101+ yield return param ;
102+ }
103+ }
104+ }
105+ }
106+ }
107+ #endif
108+
77109 while ( type != null )
78110 {
79111 var members = type . GetMembers ( name ) ;
0 commit comments