Skip to content

Commit 2616501

Browse files
Code fix turns simple lambda into parameterized #119 (#124)
* Code fix turns simple lambda into parameterized #119 * Update CHANGELOG.md
1 parent aaaf846 commit 2616501

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010

1111
### Added
1212

13-
- PR [#117](https://github.com/marinasundstrom/CheckedExceptions/pull/#117) Handle redundant typed catch #114
13+
- PR [#117](https://github.com/marinasundstrom/CheckedExceptions/pull/#117) Handle redundant typed catch
14+
- PR [#124](https://github.com/marinasundstrom/CheckedExceptions/pull/#124) Code fix turns simple lambda into parameterized
1415

1516
### Fixed
1617

CheckedExceptions.CodeFixes/AddThrowsAttributeCodeFixProvider.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,19 @@ private async Task<Document> AddThrowsAttributeAsync(Document document, SyntaxNo
158158
}
159159
else if (ancestor is LambdaExpressionSyntax lambdaExpression)
160160
{
161+
if (existingAttributeList is null && lambdaExpression is SimpleLambdaExpressionSyntax simpleLambda)
162+
{
163+
lambdaExpression = ParenthesizedLambdaExpression(
164+
ParameterList(
165+
SeparatedList([simpleLambda.Parameter])),
166+
simpleLambda.Body);
167+
}
168+
161169
lambdaExpression = existingAttributeList is not null
162170
? lambdaExpression.ReplaceNode(existingAttributeList!, attributeList)
163171
: lambdaExpression
164172
.WithoutLeadingTrivia()
165-
.AddAttributeLists(attributeList);
173+
.AddAttributeLists(attributeList.WithoutTrailingTrivia());
166174

167175
newAncestor = lambdaExpression
168176
.WithLeadingTrivia(ancestor.GetLeadingTrivia())

CheckedExceptions.Tests/CodeFixes/AddThrowsAttributeCodeFixProviderTests.cs

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,7 @@ public class TestClass
153153
[Throws(typeof(ArgumentNullException))]
154154
public void TestMethod()
155155
{
156-
Action action = [Throws(typeof(ArgumentNullException))]
157-
() =>
156+
Action action = [Throws(typeof(ArgumentNullException))] () =>
158157
{
159158
// Should trigger THROW001
160159
throw new ArgumentNullException();
@@ -172,6 +171,60 @@ public void TestMethod()
172171
await Verifier.VerifyCodeFixAsync(testCode, expectedDiagnostic, fixedCode, expectedIncrementalIterations: 2);
173172
}
174173

174+
[Fact]
175+
public async Task AddsThrowsAttribute_ToSimpleLambda_TurnIntoParameterizedLambda()
176+
{
177+
var testCode = /* lang=c#-test */ """
178+
using System;
179+
using System.Linq;
180+
181+
namespace TestNamespace
182+
{
183+
public class TestClass
184+
{
185+
public void TestMethod()
186+
{
187+
Action<int> action = x =>
188+
{
189+
// Should trigger THROW001
190+
throw new ArgumentNullException();
191+
};
192+
193+
action(42);
194+
}
195+
}
196+
}
197+
""";
198+
199+
var fixedCode = /* lang=c#-test */ """
200+
using System;
201+
using System.Linq;
202+
203+
namespace TestNamespace
204+
{
205+
public class TestClass
206+
{
207+
[Throws(typeof(ArgumentNullException))]
208+
public void TestMethod()
209+
{
210+
Action<int> action = [Throws(typeof(ArgumentNullException))] (x) =>
211+
{
212+
// Should trigger THROW001
213+
throw new ArgumentNullException();
214+
};
215+
216+
action(42);
217+
}
218+
}
219+
}
220+
""";
221+
222+
var expectedDiagnostic = Verifier.UnhandledException("ArgumentNullException")
223+
.WithSpan(13, 17, 13, 51);
224+
225+
await Verifier.VerifyCodeFixAsync(testCode, expectedDiagnostic, fixedCode, expectedIncrementalIterations: 2);
226+
}
227+
175228
[Fact]
176229
public async Task AddsThrowsAttribute_ToLocalFunction_WhenUnhandledExceptionThrown()
177230
{

0 commit comments

Comments
 (0)