Skip to content

Commit

Permalink
Fix analyzer RCS1140 - handle catch without declaration (#1554)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt authored Oct 8, 2024
1 parent bf2a457 commit ca8d20f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Fix analyzer [RCS0053](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS0053) ([PR](https://github.com/dotnet/roslynator/pull/1547))
- Fix analyzer [RCS1223](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1223) ([PR](https://github.com/dotnet/roslynator/pull/1552))
- Fix analyzer [RCS1140](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1140) ([PR](https://github.com/dotnet/roslynator/pull/1554))
- [CLI] Improve removing of unused symbols ([PR](https://github.com/dotnet/roslynator/pull/1550))

## [4.12.7] - 2024-10-01
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,20 @@ private static bool IsExceptionTypeCaughtInMethod(SyntaxNode node, ITypeSymbol e
SyntaxNode parent = node.Parent;
while (parent is not null)
{
if (parent is TryStatementSyntax tryStatement && tryStatement.Catches.Any(catchClause => SymbolEqualityComparer.Default.Equals(exceptionSymbol, semanticModel.GetTypeSymbol(catchClause.Declaration?.Type, cancellationToken))))
if (parent is TryStatementSyntax tryStatement)
{
return true;
foreach (CatchClauseSyntax catchClause in tryStatement.Catches)
{
TypeSyntax exceptionType = catchClause.Declaration?.Type;

if (exceptionType is null
|| SymbolEqualityComparer.Default.Equals(
exceptionSymbol,
semanticModel.GetTypeSymbol(exceptionType, cancellationToken)))
{
return true;
}
}
}

if (parent is MemberDeclarationSyntax or LocalFunctionStatementSyntax)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,32 @@ public void Foo(object parameter)
}
}
""");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.AddExceptionToDocumentationComment)]
public async Task TestNoDiagnostic_CatchWithoutDeclaration()
{
await VerifyNoDiagnosticAsync("""
using System;
public class C
{
/// <summary>
/// Bla
/// </summary>
public void M()
{
try
{
M();
}
catch
{
throw new InvalidOperationException("MyCustomException");
}
}
}
""");
}
}

0 comments on commit ca8d20f

Please sign in to comment.