Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt committed Oct 8, 2024
1 parent 852f658 commit ee69608
Showing 1 changed file with 31 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,13 @@ private static bool CanAddExceptionToComment(
{
if (info.IsEmptyElement)
{
containsException = ContainsException((XmlEmptyElementSyntax)info.Element, exceptionSymbol, semanticModel, cancellationToken);
containsException = ContainsException((XmlEmptyElementSyntax)info.Element,
exceptionSymbol, semanticModel, cancellationToken);
}
else
{
containsException = ContainsException((XmlElementSyntax)info.Element, exceptionSymbol, semanticModel, cancellationToken);
containsException = ContainsException((XmlElementSyntax)info.Element, exceptionSymbol,
semanticModel, cancellationToken);
}
}

Expand All @@ -141,23 +143,26 @@ private static bool CanAddExceptionToComment(
}

return !containsIncludeOrExclude
&& !containsException;
&& !containsException;
}

private static bool ContainsException(XmlElementSyntax xmlElement, INamedTypeSymbol exceptionSymbol, SemanticModel semanticModel, CancellationToken cancellationToken)
private static bool ContainsException(XmlElementSyntax xmlElement, INamedTypeSymbol exceptionSymbol,
SemanticModel semanticModel, CancellationToken cancellationToken)
{
XmlElementStartTagSyntax startTag = xmlElement.StartTag;

return startTag is not null
&& ContainsException(startTag.Attributes, exceptionSymbol, semanticModel, cancellationToken);
&& ContainsException(startTag.Attributes, exceptionSymbol, semanticModel, cancellationToken);
}

private static bool ContainsException(XmlEmptyElementSyntax xmlEmptyElement, INamedTypeSymbol exceptionSymbol, SemanticModel semanticModel, CancellationToken cancellationToken)
private static bool ContainsException(XmlEmptyElementSyntax xmlEmptyElement, INamedTypeSymbol exceptionSymbol,
SemanticModel semanticModel, CancellationToken cancellationToken)
{
return ContainsException(xmlEmptyElement.Attributes, exceptionSymbol, semanticModel, cancellationToken);
}

private static bool ContainsException(SyntaxList<XmlAttributeSyntax> attributes, INamedTypeSymbol exceptionSymbol, SemanticModel semanticModel, CancellationToken cancellationToken)
private static bool ContainsException(SyntaxList<XmlAttributeSyntax> attributes, INamedTypeSymbol exceptionSymbol,
SemanticModel semanticModel, CancellationToken cancellationToken)
{
foreach (XmlAttributeSyntax xmlAttribute in attributes)
{
Expand All @@ -176,7 +181,8 @@ private static bool ContainsException(SyntaxList<XmlAttributeSyntax> attributes,
// https://github.com/dotnet/roslyn/issues/22923
if (exceptionSymbol.IsGenericType
&& symbol.IsGenericType
&& SymbolEqualityComparer.Default.Equals(exceptionSymbol.ConstructedFrom, symbol.ConstructedFrom))
&& SymbolEqualityComparer.Default.Equals(exceptionSymbol.ConstructedFrom,
symbol.ConstructedFrom))
{
return true;
}
Expand Down Expand Up @@ -216,30 +222,33 @@ private static ISymbol GetDeclarationSymbol(ISymbol symbol)
private static bool InheritsFromException(ITypeSymbol typeSymbol, INamedTypeSymbol exceptionSymbol)
{
return typeSymbol?.TypeKind == TypeKind.Class
&& typeSymbol.BaseType?.IsObject() == false
&& typeSymbol.InheritsFrom(exceptionSymbol);
&& typeSymbol.BaseType?.IsObject() == false
&& typeSymbol.InheritsFrom(exceptionSymbol);
}

/// <summary>
/// Walk upwards from throw statement and find all try statements in method and see if any of them catches the thrown exception type
/// </summary>
private static bool IsExceptionTypeCaughtInMethod(SyntaxNode node, ITypeSymbol exceptionSymbol, SemanticModel semanticModel, CancellationToken cancellationToken)
private static bool IsExceptionTypeCaughtInMethod(SyntaxNode node, ITypeSymbol exceptionSymbol,
SemanticModel semanticModel, CancellationToken cancellationToken)
{
SyntaxNode parent = node.Parent;
while (parent is not null)
{
if (parent is TryStatementSyntax tryStatement
&& tryStatement.Catches.Any(catchClause =>
if (parent is TryStatementSyntax tryStatement)
{
TypeSyntax exceptionType = catchClause.Declaration?.Type;
foreach (CatchClauseSyntax catchClause in tryStatement.Catches)
{
TypeSyntax exceptionType = catchClause.Declaration?.Type;

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

if (parent is MemberDeclarationSyntax or LocalFunctionStatementSyntax)
Expand All @@ -256,4 +265,4 @@ private static bool IsExceptionTypeCaughtInMethod(SyntaxNode node, ITypeSymbol e

return false;
}
}
}

0 comments on commit ee69608

Please sign in to comment.