Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Fixed

- PR [#306](https://github.com/marinasundstrom/CheckedExceptions/pull/306) Account for LINQ pipeline exceptions when analyzing foreach statements inside try-catch blocks

## [2.5.0] - 2025-09-05

### Added
Expand Down
44 changes: 39 additions & 5 deletions CheckedExceptions.Tests/CheckedExceptionsAnalyzerTests.Linq.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,45 @@ await Verifier.VerifyAnalyzerAsync(test, setup: o =>
o.ExpectedDiagnostics.AddRange(expected, expected2, expected3, expected4);
o.DisabledDiagnostics.Remove(CheckedExceptionsAnalyzer.DiagnosticIdRedundantExceptionDeclaration);
}, executable: true);
}

[Fact]
public async Task PassDelegateByVariable()
{
}

[Fact]
public async Task ForEachCaughtByCatchAll()
{
var test = /* lang=c#-test */ """
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;

IEnumerable<int> items = [];
var query = items.Where(x => int.Parse("10") == x);
try
{
foreach (var item in query) { }
}
catch
{
}
""";

var expected = Verifier.Diagnostic(CheckedExceptionsAnalyzer.DiagnosticIdImplicitlyDeclaredException)
.WithArguments("FormatException")
.WithSpan(7, 34, 7, 45);

var expected2 = Verifier.Diagnostic(CheckedExceptionsAnalyzer.DiagnosticIdImplicitlyDeclaredException)
.WithArguments("OverflowException")
.WithSpan(7, 34, 7, 45);

await Verifier.VerifyAnalyzerAsync(test, setup: o =>
{
o.ExpectedDiagnostics.AddRange(expected, expected2);
}, executable: true);
}

[Fact]
public async Task PassDelegateByVariable()
{
var test = /* lang=c#-test */ """
#nullable enable
using System;
Expand Down
7 changes: 7 additions & 0 deletions CheckedExceptions/CheckedExceptionsAnalyzer.Analysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,13 @@ private static void CollectExceptionsFromExpression(SyntaxNode expression, Compi
}
}
}

// Capture exceptions from deferred enumerable pipelines directly referenced by the expression itself
var exprOp = semanticModel.GetOperation(expression);
if (exprOp is not null)
{
CollectEnumerationExceptions(exprOp, exceptions, compilation, semanticModel, settings, default);
}
}

private static HashSet<INamedTypeSymbol>? GetCaughtExceptions(SyntaxList<CatchClauseSyntax> catchClauses, SemanticModel semanticModel)
Expand Down
2 changes: 1 addition & 1 deletion Test/CheckedExceptions.settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"defaultExceptionClassification": "NonStrict",
"defaultExceptionClassification": "Strict",
"exceptions": {
"System.NotImplementedException": "Informational",
"System.IO.IOException": "Informational",
Expand Down
27 changes: 26 additions & 1 deletion Test/LinqTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,41 @@ public void Test1()
private static void ExplicitlyDeclaredThrows()
{
IEnumerable<int> items = [];
var query = items.Where([Throws(typeof(FormatException), typeof(OverflowException))] (x) => x == int.Parse("10"));
var query = items.Where((x) => x == int.Parse("10"));
var x = query;
var r = x.Select((z) => 2);

foreach (var n in query) { }

try
{
foreach (var n in query) { }
}
catch (OverflowException exc)
{

}
catch
{

}

foreach (var item in r)
{

}

try
{
foreach (var item in r)
{

}
}
catch
{

}
}

private static void ImplicitlyDeclaredThrows()
Expand Down
Loading