@@ -57,13 +57,13 @@ public override void Initialize(AnalysisContext context)
57
57
CheckCopyability ( oc , op . Value , ArgumentRule ) ;
58
58
} , OperationKind . Argument ) ;
59
59
60
- // csc.RegisterOperationAction(oc =>
61
- // {
62
- // var op = (IReturnOperation)oc.Operation;
63
- // if (op.ReturnedValue == null) return;
64
- // CheckCopyability(oc, op.ReturnedValue, ReturnRule);
65
- // }, OperationKind.Return,
66
- // OperationKind.YieldReturn);
60
+ csc . RegisterOperationAction ( oc=>
61
+ {
62
+ var op = ( IReturnOperation ) oc . Operation ;
63
+ if ( op . ReturnedValue == null ) return ;
64
+ CheckCopyability ( oc , op . ReturnedValue , ReturnRule ) ;
65
+ } , OperationKind . Return ,
66
+ OperationKind . YieldReturn ) ;
67
67
68
68
csc . RegisterOperationAction ( oc=>
69
69
{
@@ -83,10 +83,10 @@ public override void Initialize(AnalysisContext context)
83
83
op == ( ( IForEachLoopOperation ) op . Parent ) . Collection &&
84
84
op . Conversion . IsIdentity )
85
85
{
86
- return ;
86
+ return ;
87
87
}
88
88
89
- oc . ReportDiagnostic ( Diagnostic . Create ( ConversionRule , v . Syntax . GetLocation ( ) , t . Name ) ) ;
89
+ oc . ReportDiagnostic ( Error ( v . Syntax , ConversionRule , t . Name ) ) ;
90
90
} , OperationKind . Conversion ) ;
91
91
92
92
csc . RegisterOperationAction ( oc=>
@@ -101,24 +101,12 @@ public override void Initialize(AnalysisContext context)
101
101
}
102
102
} , OperationKind . ArrayInitializer ) ;
103
103
104
- csc . RegisterOperationAction ( oc=>
105
- {
106
- var op = ( ICollectionElementInitializerOperation ) oc . Operation ;
107
-
108
- if ( ! HasNonCopyableParameter ( op . AddMethod ) ) return ;
109
-
110
- foreach ( var a in op . Arguments )
111
- {
112
- CheckCopyability ( oc , a , InitializerRule ) ;
113
- }
114
- } , OperationKind . CollectionElementInitializer ) ;
115
-
116
104
csc . RegisterOperationAction ( oc=>
117
105
{
118
106
var op = ( IDeclarationPatternOperation ) oc . Operation ;
119
107
var t = ( ( ILocalSymbol ) op . DeclaredSymbol ) . Type ;
120
108
if ( ! t . IsNonCopyable ( ) ) return ;
121
- oc . ReportDiagnostic ( Diagnostic . Create ( PatternRule , op . Syntax . GetLocation ( ) , t . Name ) ) ;
109
+ oc . ReportDiagnostic ( Error ( op . Syntax , PatternRule , t . Name ) ) ;
122
110
} , OperationKind . DeclarationPattern ) ;
123
111
124
112
csc . RegisterOperationAction ( oc=>
@@ -152,13 +140,24 @@ public override void Initialize(AnalysisContext context)
152
140
153
141
} , OperationKind . Invocation ) ;
154
142
143
+ csc . RegisterOperationAction ( oc=> {
144
+ var op = ( IDynamicInvocationOperation ) oc . Operation ;
145
+
146
+ foreach ( var arg in op . Arguments ) {
147
+ if ( ! arg . Type . IsNonCopyable ( ) ) continue ;
148
+
149
+ oc . ReportDiagnostic ( Error ( arg . Syntax , GenericConstraintRule ) ) ;
150
+ }
151
+
152
+ } , OperationKind . DynamicInvocation ) ;
153
+
155
154
csc . RegisterOperationAction ( oc=>
156
155
{
157
156
// delagate creation
158
157
var op = ( IMemberReferenceOperation ) oc . Operation ;
159
158
if ( op . Instance == null ) return ;
160
159
if ( ! op . Instance . Type . IsNonCopyable ( ) ) return ;
161
- oc . ReportDiagnostic ( Diagnostic . Create ( DelegateRule , op . Instance . Syntax . GetLocation ( ) , op . Instance . Type . Name ) ) ;
160
+ oc . ReportDiagnostic ( Error ( op . Instance . Syntax , DelegateRule , op . Instance . Type . Name ) ) ;
162
161
} , OperationKind . MethodReference ) ;
163
162
164
163
csc . RegisterSymbolAction ( sac=>
@@ -168,7 +167,7 @@ public override void Initialize(AnalysisContext context)
168
167
if ( ! f . Type . IsNonCopyable ( ) ) return ;
169
168
if ( f . ContainingType . IsReferenceType ) return ;
170
169
if ( f . ContainingType . IsNonCopyable ( ) ) return ;
171
- sac . ReportDiagnostic ( Diagnostic . Create ( FieldDeclarationRule , f . DeclaringSyntaxReferences [ 0 ] . GetSyntax ( ) . GetLocation ( ) , f . Type . Name ) ) ;
170
+ sac . ReportDiagnostic ( Error ( f . DeclaringSyntaxReferences [ 0 ] . GetSyntax ( ) , FieldDeclarationRule , f . Type . Name ) ) ;
172
171
} , SymbolKind . Field ) ;
173
172
} ) ;
174
173
@@ -192,7 +191,7 @@ private static void CheckGenericConstraints(in OperationAnalysisContext oc, IInv
192
191
var a = arguments [ i ] ;
193
192
194
193
if ( a . IsNonCopyable ( ) && ! p . IsNonCopyable ( ) )
195
- oc . ReportDiagnostic ( Diagnostic . Create ( rule , op . Syntax . GetLocation ( ) , a . Name ) ) ;
194
+ oc . ReportDiagnostic ( Error ( op . Syntax , rule , a . Name ) ) ;
196
195
}
197
196
}
198
197
}
@@ -206,10 +205,15 @@ private static void CheckInstanceReadonly(in OperationAnalysisContext oc, IOpera
206
205
207
206
if ( IsInstanceReadonly ( instance ) )
208
207
{
209
- oc . ReportDiagnostic ( Diagnostic . Create ( rule , instance . Syntax . GetLocation ( ) , t . Name ) ) ;
208
+ oc . ReportDiagnostic ( Error ( instance . Syntax , rule , t . Name ) ) ;
210
209
}
211
210
}
212
211
212
+ private static Diagnostic Error ( SyntaxNode at , DiagnosticDescriptor rule , string name = null )
213
+ => name is null
214
+ ? Diagnostic . Create ( rule , at . GetLocation ( ) )
215
+ : Diagnostic . Create ( rule , at . GetLocation ( ) , name ) ;
216
+
213
217
private static bool IsInstanceReadonly ( IOperation instance )
214
218
{
215
219
bool isReadOnly = false ;
@@ -246,7 +250,7 @@ private static void CheckCopyability(in OperationAnalysisContext oc, IOperation
246
250
var t = v . Type ;
247
251
if ( ! t . IsNonCopyable ( ) ) return ;
248
252
if ( v . CanCopy ( ) ) return ;
249
- oc . ReportDiagnostic ( Diagnostic . Create ( rule , v . Syntax . GetLocation ( ) , t . Name ) ) ;
253
+ oc . ReportDiagnostic ( Error ( v . Syntax , rule , t . Name ) ) ;
250
254
}
251
255
}
252
256
}