From 9f62a17e932954b66dcf95783bed8a96abdb9e5b Mon Sep 17 00:00:00 2001 From: Denis Prodan Date: Fri, 14 Dec 2018 12:21:33 -0400 Subject: [PATCH] #7 Fix issue with false positive analyzer reaction. Fix warnings for exception anayzer. --- .gitignore | 3 ++- .../ExceptionsAnalyzerUnitTests.cs | 15 +++++++++++++- .../ExceptionsAnalyzer/ExceptionsAnalyzer.cs | 20 +++++++++++++++---- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index ff40179..0818427 100644 --- a/.gitignore +++ b/.gitignore @@ -259,4 +259,5 @@ paket-files/ # Python Tools for Visual Studio (PTVS) __pycache__/ -*.pyc \ No newline at end of file +*.pyc +/ndepend diff --git a/Exceptions/ExceptionsAnalyzer.Test/ExceptionsAnalyzerUnitTests.cs b/Exceptions/ExceptionsAnalyzer.Test/ExceptionsAnalyzerUnitTests.cs index f02f503..d6aaa45 100644 --- a/Exceptions/ExceptionsAnalyzer.Test/ExceptionsAnalyzerUnitTests.cs +++ b/Exceptions/ExceptionsAnalyzer.Test/ExceptionsAnalyzerUnitTests.cs @@ -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 diff --git a/Exceptions/ExceptionsAnalyzer/ExceptionsAnalyzer.cs b/Exceptions/ExceptionsAnalyzer/ExceptionsAnalyzer.cs index 71dc175..a7b9372 100644 --- a/Exceptions/ExceptionsAnalyzer/ExceptionsAnalyzer.cs +++ b/Exceptions/ExceptionsAnalyzer/ExceptionsAnalyzer.cs @@ -13,11 +13,13 @@ namespace ExceptionsAnalyzer public class ExceptionsAnalyzer : DiagnosticAnalyzer { public override ImmutableArray SupportedDiagnostics => - ImmutableArray.Create( + ImmutableArray.Create(new[] + { Descriptors.NoExceptionUsageDescriptor, Descriptors.RethrowSameExceptionDescriptor, Descriptors.RethrowWithoutInnerDescriptor, - Descriptors.ExceptionAnalyzerErrorDescriptor); + Descriptors.ExceptionAnalyzerErrorDescriptor + }); public override void Initialize(AnalysisContext context) { @@ -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) @@ -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)