@@ -446,5 +446,242 @@ public void Test()
446446
447447 await VerifyCSharpFixAsync ( testCode , expected , fixedCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
448448 }
449+
450+ [ Fact ]
451+ public async Task TestDelegateUseAsConstructorArgumentsAsync ( )
452+ {
453+ var testCode = @"
454+ using System;
455+ public class TypeName
456+ {
457+ public TypeName(Action argument)
458+ {
459+
460+ }
461+
462+ public void Test()
463+ {
464+ new TypeName(delegate { });
465+ }
466+ }" ;
467+ string fixedCode = @"
468+ using System;
469+ public class TypeName
470+ {
471+ public TypeName(Action argument)
472+ {
473+
474+ }
475+
476+ public void Test()
477+ {
478+ new TypeName(() => { });
479+ }
480+ }" ;
481+ var expected = Diagnostic ( ) . WithLocation ( 12 , 22 ) ;
482+ await VerifyCSharpFixAsync ( testCode , expected , fixedCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
483+ }
484+
485+ [ Fact ]
486+ public async Task TestDelegateUseAsConstructorArgumentsWithConflictingExpressionOverloadAsync ( )
487+ {
488+ var testCode = @"
489+ using System;
490+ using System.Linq.Expressions;
491+ public class TypeName
492+ {
493+ public TypeName(Action argument)
494+ {
495+
496+ }
497+
498+ public TypeName(Expression<Action> argument)
499+ {
500+
501+ }
502+
503+ public void Test()
504+ {
505+ new TypeName(delegate { });
506+ }
507+ }" ;
508+ await VerifyCSharpDiagnosticAsync ( testCode , DiagnosticResult . EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
509+ }
510+
511+ [ Fact ]
512+ public async Task TestDelegateUseAsConstructorArgumentsWithNonConflictingExpressionOverloadAsync ( )
513+ {
514+ var testCode = @"
515+ using System;
516+ using System.Linq.Expressions;
517+ public class TypeName
518+ {
519+ public TypeName(Action argument)
520+ {
521+
522+ }
523+
524+ public TypeName(Expression<Func<int>> argument)
525+ {
526+
527+ }
528+
529+ public void Test()
530+ {
531+ new TypeName(delegate { });
532+ }
533+ }" ;
534+ var fixedCode = @"
535+ using System;
536+ using System.Linq.Expressions;
537+ public class TypeName
538+ {
539+ public TypeName(Action argument)
540+ {
541+
542+ }
543+
544+ public TypeName(Expression<Func<int>> argument)
545+ {
546+
547+ }
548+
549+ public void Test()
550+ {
551+ new TypeName(() => { });
552+ }
553+ }" ;
554+ var expected = Diagnostic ( ) . WithLocation ( 18 , 22 ) ;
555+ await VerifyCSharpFixAsync ( testCode , expected , fixedCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
556+ }
557+
558+ [ Fact ]
559+ public async Task TestDelegateUseAsIndexerArgumentsAsync ( )
560+ {
561+ var testCode = @"
562+ using System;
563+ public class TypeName
564+ {
565+ public int this[Action argument]
566+ {
567+ get { return 0; }
568+ }
569+
570+ public void Test()
571+ {
572+ int _ = this[delegate { }];
573+ }
574+ }" ;
575+ string fixedCode = @"
576+ using System;
577+ public class TypeName
578+ {
579+ public int this[Action argument]
580+ {
581+ get { return 0; }
582+ }
583+
584+ public void Test()
585+ {
586+ int _ = this[() => { }];
587+ }
588+ }" ;
589+ var expected = Diagnostic ( ) . WithLocation ( 12 , 22 ) ;
590+ await VerifyCSharpFixAsync ( testCode , expected , fixedCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
591+ }
592+
593+ [ Fact ]
594+ public async Task TestDelegateUseAsIndexerArgumentsWithConflictingExpressionOverloadAsync ( )
595+ {
596+ var testCode = @"
597+ using System;
598+ using System.Linq.Expressions;
599+ public class TypeName
600+ {
601+ public int this[Action argument]
602+ {
603+ get { return 0; }
604+ }
605+ public int this[Expression<Action> argument]
606+ {
607+ get { return 0; }
608+ }
609+ public void Test()
610+ {
611+ int _ = this[delegate { }];
612+ }
613+ }" ;
614+ await VerifyCSharpDiagnosticAsync ( testCode , DiagnosticResult . EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
615+ }
616+
617+ [ Fact ]
618+ public async Task TestDelegateUseAsIndexerArgumentsWithNonConflictingExpressionOverloadAsync ( )
619+ {
620+ var testCode = @"
621+ using System;
622+ using System.Linq.Expressions;
623+ public class TypeName
624+ {
625+ public int this[Action argument]
626+ {
627+ get { return 0; }
628+ }
629+
630+ public int this[Expression<Func<int>> argument]
631+ {
632+ get { return 0; }
633+ }
634+
635+ public void Test()
636+ {
637+ int _ = this[delegate { }];
638+ }
639+ }" ;
640+ var fixedCode = @"
641+ using System;
642+ using System.Linq.Expressions;
643+ public class TypeName
644+ {
645+ public int this[Action argument]
646+ {
647+ get { return 0; }
648+ }
649+
650+ public int this[Expression<Func<int>> argument]
651+ {
652+ get { return 0; }
653+ }
654+
655+ public void Test()
656+ {
657+ int _ = this[() => { }];
658+ }
659+ }" ;
660+ var expected = Diagnostic ( ) . WithLocation ( 18 , 22 ) ;
661+ await VerifyCSharpFixAsync ( testCode , expected , fixedCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
662+ }
663+
664+ [ Fact ]
665+ public async Task TestInvalidOverloadAsync ( )
666+ {
667+ var testCode = @"
668+ using System;
669+ using System.Linq.Expressions;
670+ public unsafe class TypeName
671+ {
672+ void Method(int* data) { throw null; }
673+ void Caller() => Method(delegate { });
674+ }" ;
675+ var fixedCode = @"
676+ using System;
677+ using System.Linq.Expressions;
678+ public unsafe class TypeName
679+ {
680+ void Method(int* data) { throw null; }
681+ void Caller() => Method(delegate { });
682+ }" ;
683+ var expected = DiagnosticResult . CompilerError ( "CS1660" ) . WithLocation ( 7 , 29 ) ;
684+ await VerifyCSharpFixAsync ( testCode , expected , fixedCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
685+ }
449686 }
450687}
0 commit comments