@@ -276,7 +276,7 @@ public class TypeName
276276{
277277 public void Test()
278278 {
279- Action action1 = /*a*/()/*b*/ => { };
279+ Action action1 = /*a*/() => /*b*/{ };
280280 Action action2 = /*a*//*b*/(/*c*/)/*d*/ => { };
281281 Action<int> action3 = /*a*//*b*//*c*//*d*/i/*e*//*f*/ => { };
282282 Action<List<int>> action4 = i => { };
@@ -710,5 +710,135 @@ static void Main(string[] args)
710710
711711 await VerifyCSharpFixAsync ( testCode , expected , testCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
712712 }
713+
714+ [ Fact ]
715+ [ WorkItem ( 2902 , "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2902" ) ]
716+ public async Task VerifyThatCodeFixDoesNotCrashOnDelegateReturnAsync ( )
717+ {
718+ var testCode = @"using System;
719+ public class TestClass
720+ {
721+ public static EventHandler TestMethod1()
722+ {
723+ return delegate
724+ {
725+ };
726+ }
727+
728+ public static EventHandler TestMethod2() => delegate
729+ {
730+ };
731+
732+ public static EventHandler TestProperty1
733+ {
734+ get
735+ {
736+ return delegate
737+ {
738+ };
739+ }
740+ }
741+
742+ public static EventHandler TestProperty2 => delegate
743+ {
744+ };
745+ }" ;
746+
747+ var fixedCode = @"using System;
748+ public class TestClass
749+ {
750+ public static EventHandler TestMethod1()
751+ {
752+ return (sender, e) =>
753+ {
754+ };
755+ }
756+
757+ public static EventHandler TestMethod2() => (sender, e) =>
758+ {
759+ };
760+
761+ public static EventHandler TestProperty1
762+ {
763+ get
764+ {
765+ return (sender, e) =>
766+ {
767+ };
768+ }
769+ }
770+
771+ public static EventHandler TestProperty2 => (sender, e) =>
772+ {
773+ };
774+ }" ;
775+
776+ DiagnosticResult [ ] expected =
777+ {
778+ Diagnostic ( ) . WithLocation ( 6 , 16 ) ,
779+ Diagnostic ( ) . WithLocation ( 11 , 49 ) ,
780+ Diagnostic ( ) . WithLocation ( 19 , 20 ) ,
781+ Diagnostic ( ) . WithLocation ( 25 , 49 ) ,
782+ } ;
783+
784+ await VerifyCSharpFixAsync ( testCode , expected , fixedCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
785+ }
786+
787+ [ Fact ]
788+ [ WorkItem ( 2902 , "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2902" ) ]
789+ public async Task VerifyThatEventInitializersWorkAsExpectedAsync ( )
790+ {
791+ var testCode = @"using System;
792+ public class TestClass
793+ {
794+ public static event EventHandler StaticEvent = delegate { };
795+ public event EventHandler InstanceEvent = delegate { };
796+ }
797+ " ;
798+
799+ var fixedCode = @"using System;
800+ public class TestClass
801+ {
802+ public static event EventHandler StaticEvent = (sender, e) => { };
803+ public event EventHandler InstanceEvent = (sender, e) => { };
804+ }
805+ " ;
806+
807+ DiagnosticResult [ ] expected =
808+ {
809+ Diagnostic ( ) . WithLocation ( 4 , 52 ) ,
810+ Diagnostic ( ) . WithLocation ( 5 , 47 ) ,
811+ } ;
812+
813+ await VerifyCSharpFixAsync ( testCode , expected , fixedCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
814+ }
815+
816+ [ Fact ]
817+ [ WorkItem ( 2902 , "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2902" ) ]
818+ public async Task VerifyInvalidCodeConstructionsAsync ( )
819+ {
820+ var testCode = @"using System;
821+ public class TestClass
822+ {
823+ public static EventHandler[] TestMethod() => delegate { };
824+ }
825+ " ;
826+
827+ DiagnosticResult [ ] expected =
828+ {
829+ Diagnostic ( ) . WithSpan ( 4 , 50 , 4 , 58 ) ,
830+ DiagnosticResult . CompilerError ( "CS1660" ) . WithMessage ( "Cannot convert anonymous method to type 'EventHandler[]' because it is not a delegate type" ) . WithSpan ( 4 , 50 , 4 , 62 ) ,
831+ } ;
832+
833+ var test = new CSharpTest
834+ {
835+ TestCode = testCode ,
836+ FixedCode = testCode ,
837+ } ;
838+
839+ test . ExpectedDiagnostics . AddRange ( expected ) ;
840+ test . RemainingDiagnostics . AddRange ( expected ) ;
841+ await test . RunAsync ( CancellationToken . None ) . ConfigureAwait ( false ) ;
842+ }
713843 }
714844}
0 commit comments