@@ -100,13 +100,126 @@ public async void NotWarningIfEventIsFiredWithInvokeMethod()
100100 public class MyClass
101101 {
102102 public event System.EventHandler MyEvent;
103-
104103 public void Execute()
105104 {
106105 MyEvent?.Invoke(this, System.EventArgs.Empty);
107106 }
108107 }" ;
108+ await VerifyCSharpHasNoDiagnosticsAsync ( test ) ;
109+ }
110+
111+ [ Fact ]
112+ public async void RaiseDiagnosticEvenWhenVerifiedForNullAndNotReturnedOrThrown ( )
113+ {
114+ const string test = @"
115+ public class MyClass
116+ {
117+ public static void Execute(System.Action action)
118+ {
119+ if (action == null)
120+ {
121+ var a = 1;
122+ }
123+ action();
124+ }
125+ }" ;
126+ var expected = new DiagnosticResult
127+ {
128+ Id = DiagnosticId . UseInvokeMethodToFireEvent . ToDiagnosticId ( ) ,
129+ Message = string . Format ( UseInvokeMethodToFireEventAnalyzer . MessageFormat . ToString ( ) , "action" ) ,
130+ Severity = DiagnosticSeverity . Warning ,
131+ Locations = new [ ] { new DiagnosticResultLocation ( "Test0.cs" , 10 , 25 ) }
132+ } ;
133+ await VerifyCSharpDiagnosticAsync ( test , expected ) ;
134+ }
135+
136+ [ Fact ]
137+ public async void RaiseDiagnosticEvenWhenVerifiedForNullAndNotReturnedOrThrownWithBlocklessIf ( )
138+ {
139+ const string test = @"
140+ public class MyClass
141+ {
142+ public static void Execute(System.Action action)
143+ {
144+ if (action == null)
145+ System.Console.WriteLine();
146+ action();
147+ }
148+ }" ;
149+ var expected = new DiagnosticResult
150+ {
151+ Id = DiagnosticId . UseInvokeMethodToFireEvent . ToDiagnosticId ( ) ,
152+ Message = string . Format ( UseInvokeMethodToFireEventAnalyzer . MessageFormat . ToString ( ) , "action" ) ,
153+ Severity = DiagnosticSeverity . Warning ,
154+ Locations = new [ ] { new DiagnosticResultLocation ( "Test0.cs" , 8 , 25 ) }
155+ } ;
156+ await VerifyCSharpDiagnosticAsync ( test , expected ) ;
157+ }
158+
159+ [ Fact ]
160+ public async void RaiseDiagnosticIfNullCheckIsAfterInvocation ( )
161+ {
162+ const string test = @"
163+ public class MyClass
164+ {
165+ public static void Execute(System.Action action)
166+ {
167+ action();
168+ if (action == null) throw new Exception();
169+ }
170+ }" ;
171+ var expected = new DiagnosticResult
172+ {
173+ Id = DiagnosticId . UseInvokeMethodToFireEvent . ToDiagnosticId ( ) ,
174+ Message = string . Format ( UseInvokeMethodToFireEventAnalyzer . MessageFormat . ToString ( ) , "action" ) ,
175+ Severity = DiagnosticSeverity . Warning ,
176+ Locations = new [ ] { new DiagnosticResultLocation ( "Test0.cs" , 6 , 25 ) }
177+ } ;
178+ await VerifyCSharpDiagnosticAsync ( test , expected ) ;
179+ }
109180
181+ [ Fact ]
182+ public async void IgnoreIfAlreadyVerifiedForNullWithThrow ( )
183+ {
184+ const string test = @"
185+ public class MyClass
186+ {
187+ public static void Execute(System.Action action)
188+ {
189+ if (action == null) throw new Exception();
190+ action();
191+ }
192+ }" ;
193+ await VerifyCSharpHasNoDiagnosticsAsync ( test ) ;
194+ }
195+
196+ [ Fact ]
197+ public async void IgnoreIfAlreadyVerifiedForNullInverted ( )
198+ {
199+ const string test = @"
200+ public class MyClass
201+ {
202+ public static void Execute(System.Action action)
203+ {
204+ if (null == action) throw new Exception();
205+ action();
206+ }
207+ }" ;
208+ await VerifyCSharpHasNoDiagnosticsAsync ( test ) ;
209+ }
210+
211+ [ Fact ]
212+ public async void IgnoreIfAlreadyVerifiedForNullWithReturn ( )
213+ {
214+ const string test = @"
215+ public class MyClass
216+ {
217+ public static void Execute(System.Action action)
218+ {
219+ if (action == null) return;
220+ action();
221+ }
222+ }" ;
110223 await VerifyCSharpHasNoDiagnosticsAsync ( test ) ;
111224 }
112225
@@ -200,15 +313,14 @@ public async void ReportOnParametersWhenReturnTypeIsAReferenceType()
200313 var test = @"
201314public static TReturn Method<T, TReturn>(System.Func<T, TReturn> getter) where T : System.Attribute where TReturn : class
202315{
203- if (getter == null) return default(TReturn);
204316 return getter(default(T));
205317}" . WrapInCSharpClass ( ) ;
206318 var expected = new DiagnosticResult
207319 {
208320 Id = DiagnosticId . UseInvokeMethodToFireEvent . ToDiagnosticId ( ) ,
209321 Message = string . Format ( UseInvokeMethodToFireEventAnalyzer . MessageFormat . ToString ( ) , "getter" ) ,
210322 Severity = DiagnosticSeverity . Warning ,
211- Locations = new [ ] { new DiagnosticResultLocation ( "Test0.cs" , 12 , 12 ) }
323+ Locations = new [ ] { new DiagnosticResultLocation ( "Test0.cs" , 11 , 12 ) }
212324 } ;
213325 await VerifyCSharpDiagnosticAsync ( test , expected ) ;
214326 }
0 commit comments