Skip to content

Commit ffe11e1

Browse files
committed
Added test for invalid construction and local function
1 parent 77ee36f commit ffe11e1

3 files changed

Lines changed: 73 additions & 3 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1130CodeFixProvider.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ private static SyntaxNode ReplaceWithLambda(SemanticModel semanticModel, Anonymo
103103
case SyntaxKind.ArrowExpressionClause:
104104
case SyntaxKind.ReturnStatement:
105105
argumentList = GetMemberReturnTypeArgumentList(semanticModel, anonymousMethod);
106+
if (argumentList.IsEmpty)
107+
{
108+
return null;
109+
}
110+
106111
break;
107112
}
108113

@@ -204,9 +209,8 @@ private static ImmutableArray<string> GetEqualsArgumentList(SemanticModel semant
204209
private static ImmutableArray<string> GetMemberReturnTypeArgumentList(SemanticModel semanticModel, AnonymousMethodExpressionSyntax anonymousMethod)
205210
{
206211
var enclosingSymbol = semanticModel.GetEnclosingSymbol(anonymousMethod.Parent.SpanStart);
207-
var returnType = (INamedTypeSymbol)((IMethodSymbol)enclosingSymbol).ReturnType;
208-
209-
return returnType.DelegateInvokeMethod.Parameters.Select(ps => ps.Name).ToImmutableArray();
212+
var returnType = ((IMethodSymbol)enclosingSymbol).ReturnType as INamedTypeSymbol;
213+
return (returnType == null) ? ImmutableArray<string>.Empty : returnType.DelegateInvokeMethod.Parameters.Select(ps => ps.Name).ToImmutableArray();
210214
}
211215

212216
private static List<ParameterSyntax> GenerateUniqueParameterNames(SemanticModel semanticModel, AnonymousMethodExpressionSyntax anonymousMethod, ImmutableArray<string> argumentNames)

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/ReadabilityRules/SA1130CSharp7UnitTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,47 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp7.ReadabilityRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
69
using StyleCop.Analyzers.Test.ReadabilityRules;
10+
using Xunit;
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
12+
StyleCop.Analyzers.ReadabilityRules.SA1130UseLambdaSyntax,
13+
StyleCop.Analyzers.ReadabilityRules.SA1130CodeFixProvider>;
714

815
public class SA1130CSharp7UnitTests : SA1130UnitTests
916
{
17+
[Fact]
18+
[WorkItem(2902, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2902")]
19+
public async Task VerifyLocalFunctionAsync()
20+
{
21+
var testCode = @"using System;
22+
public class TestClass
23+
{
24+
public void TestMethod()
25+
{
26+
EventHandler LocalTestFunction() => delegate { };
27+
}
28+
}
29+
";
30+
31+
var fixedCode = @"using System;
32+
public class TestClass
33+
{
34+
public void TestMethod()
35+
{
36+
EventHandler LocalTestFunction() => (sender, e) => { };
37+
}
38+
}
39+
";
40+
41+
DiagnosticResult[] expected =
42+
{
43+
Diagnostic().WithLocation(6, 45),
44+
};
45+
46+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
47+
}
1048
}
1149
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1130UnitTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,5 +812,33 @@ public class TestClass
812812

813813
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
814814
}
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+
}
815843
}
816844
}

0 commit comments

Comments
 (0)