Skip to content

Commit

Permalink
#7 Fix issue with false positive analyzer reaction. Fix warnings for …
Browse files Browse the repository at this point in the history
…exception anayzer.
  • Loading branch information
denis-prodan committed Dec 21, 2018
1 parent fbcb70e commit 9f62a17
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,5 @@ paket-files/

# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
*.pyc
/ndepend
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,24 @@ public void RethrowWithoutInner()
}";
var code = BuildCode(test);

var expected = GetDiagnostic(Descriptors.RethrowWithoutInnerExceptionId, Descriptors.RethrowWithoutInnerMessage, DiagnosticSeverity.Info);
var expected = GetDiagnostic(Descriptors.RethrowWithoutInnerExceptionId, Descriptors.RethrowWithoutInnerMessage, DiagnosticSeverity.Warning);

VerifyCSharpDiagnostic(code, expected);
}

[TestMethod]
public void LogCorrect()
{
var test = @"
catch(Exception e)
{
Console.WriteLine(e.ToString());
}";
var code = BuildCode(test);

VerifyCSharpDiagnostic(code);
}

private DiagnosticResult GetDiagnostic(string diagnosticId, string description, DiagnosticSeverity severity = DiagnosticSeverity.Warning)
{
return new DiagnosticResult
Expand Down
20 changes: 16 additions & 4 deletions Exceptions/ExceptionsAnalyzer/ExceptionsAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ namespace ExceptionsAnalyzer
public class ExceptionsAnalyzer : DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
ImmutableArray.Create(
ImmutableArray.Create(new[]
{
Descriptors.NoExceptionUsageDescriptor,
Descriptors.RethrowSameExceptionDescriptor,
Descriptors.RethrowWithoutInnerDescriptor,
Descriptors.ExceptionAnalyzerErrorDescriptor);
Descriptors.ExceptionAnalyzerErrorDescriptor
});

public override void Initialize(AnalysisContext context)
{
Expand All @@ -30,7 +32,11 @@ public override void Initialize(AnalysisContext context)
}
context.RegisterSyntaxNodeAction(Analyze, SyntaxKind.TryStatement);

void Analyze(SyntaxNodeAnalysisContext con) => AnalyzeSymbol(con, proceedNoCheck, proceedRethrow, proceedNoInner);
void Analyze(SyntaxNodeAnalysisContext con) => AnalyzeSymbol(
context: con,
proceedNoCheck: proceedNoCheck,
proceedRethrow: proceedRethrow,
proceedNoInner: proceedNoInner);
}

private static void AnalyzeSymbol(SyntaxNodeAnalysisContext context, bool proceedNoCheck, bool proceedRethrow, bool proceedNoInner)
Expand Down Expand Up @@ -137,7 +143,13 @@ private static bool IsVariableUsedInExpression(string variableName, ExpressionSy
{
if (expression is InvocationExpressionSyntax invocation)
{
return invocation.ArgumentList.Arguments.Any(x => IsVariableUsedInExpression(variableName, x.Expression));
var isUsedAsIdentifier = IsVariableUsedInExpression(variableName, invocation.Expression);
return isUsedAsIdentifier || invocation.ArgumentList.Arguments.Any(x => IsVariableUsedInExpression(variableName, x.Expression));
}

if (expression is MemberAccessExpressionSyntax memberAccess)
{
return IsVariableUsedInExpression(variableName, memberAccess.Expression);
}

if (expression is IdentifierNameSyntax identifierName)
Expand Down

0 comments on commit 9f62a17

Please sign in to comment.