Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix analyzer RCS1140 - handle catch without declaration #1554

Merged
merged 7 commits into from
Oct 8, 2024
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
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");
}
}
}
""");
}
}