Skip to content

Commit 7f096af

Browse files
authored
Merge pull request #2519 from mavasani/FixCA1031
Handle catch clauses without exception variable in CA1031
2 parents c20436c + 37cb4d6 commit 7f096af

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

src/Microsoft.CodeQuality.Analyzers/Core/ApiDesignGuidelines/DoNotCatchGeneralExceptionTypes.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public DoNotCatchGeneralExceptionTypesAnalyzer() : base(shouldCheckLambdas: true
3535
{
3636
}
3737

38-
protected override Diagnostic CreateDiagnostic(IMethodSymbol containingMethod, SyntaxNode catchNode)
38+
protected override Diagnostic CreateDiagnostic(IMethodSymbol containingMethod, SyntaxToken catchKeyword)
3939
{
40-
return catchNode.CreateDiagnostic(Rule, containingMethod.Name);
40+
return catchKeyword.CreateDiagnostic(Rule, containingMethod.Name);
4141
}
4242
}
4343
}

src/Microsoft.CodeQuality.Analyzers/UnitTests/ApiDesignGuidelines/DoNotCatchGeneralExceptionTypesTests.cs

+21
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,27 @@ End Namespace
650650
GetCA1031BasicResultAt(11, 29, "TestMethod"));
651651
}
652652

653+
[Fact, WorkItem(2518, "https://github.com/dotnet/roslyn-analyzers/issues/2518")]
654+
public void CSharp_NoDiagnostic_SpecificExceptionWithoutVariable()
655+
{
656+
VerifyCSharp(@"
657+
using System;
658+
659+
public class Class1
660+
{
661+
void M()
662+
{
663+
try
664+
{
665+
}
666+
catch (OperationCanceledException)
667+
{
668+
// Comment
669+
}
670+
}
671+
}");
672+
}
673+
653674
private static DiagnosticResult GetCA1031CSharpResultAt(int line, int column, string signature)
654675
{
655676
return GetCSharpResultAt(line, column, DoNotCatchGeneralExceptionTypesAnalyzer.Rule, signature);

src/Microsoft.NetFramework.Analyzers/Core/DoNotCatchCorruptedStateExceptions.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
22

33
using System.Collections.Immutable;
4-
using System.Linq;
54
using Analyzer.Utilities;
65
using Analyzer.Utilities.Extensions;
7-
using Microsoft.NetFramework.Analyzers.Helpers;
86
using Microsoft.CodeAnalysis;
97
using Microsoft.CodeAnalysis.Diagnostics;
108

@@ -37,9 +35,9 @@ public DoNotCatchCorruptedStateExceptionsAnalyzer() : base(shouldCheckLambdas: f
3735
{
3836
}
3937

40-
protected override Diagnostic CreateDiagnostic(IMethodSymbol containingMethod, SyntaxNode catchNode)
38+
protected override Diagnostic CreateDiagnostic(IMethodSymbol containingMethod, SyntaxToken catchKeyword)
4139
{
42-
return catchNode.CreateDiagnostic(Rule, containingMethod.ToDisplayString());
40+
return catchKeyword.CreateDiagnostic(Rule, containingMethod.ToDisplayString());
4341
}
4442
}
4543
}

src/Utilities/Compiler/DoNotCatchGeneralUnlessRethrown.cs

+3-8
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protected DoNotCatchGeneralUnlessRethrownAnalyzer(bool shouldCheckLambdas, strin
2424
_enablingMethodAttributeFullyQualifiedName = enablingMethodAttributeFullyQualifiedName;
2525
}
2626

27-
protected abstract Diagnostic CreateDiagnostic(IMethodSymbol containingMethod, SyntaxNode catchNode);
27+
protected abstract Diagnostic CreateDiagnostic(IMethodSymbol containingMethod, SyntaxToken catchKeyword);
2828

2929
public override void Initialize(AnalysisContext analysisContext)
3030
{
@@ -62,7 +62,7 @@ public override void Initialize(AnalysisContext analysisContext)
6262

6363
foreach (var catchClause in walker.CatchClausesForDisallowedTypesWithoutRethrow)
6464
{
65-
operationBlockAnalysisContext.ReportDiagnostic(CreateDiagnostic(method, catchClause.Syntax));
65+
operationBlockAnalysisContext.ReportDiagnostic(CreateDiagnostic(method, catchClause.Syntax.GetFirstToken()));
6666
}
6767
}
6868
});
@@ -142,12 +142,7 @@ public override void VisitThrow(IThrowOperation operation)
142142

143143
private bool IsCatchTooGeneral(ICatchClauseOperation operation)
144144
{
145-
return IsGenericCatch(operation) || _disallowedCatchTypes.Any(type => operation.ExceptionType.Equals(type));
146-
}
147-
148-
private static bool IsGenericCatch(ICatchClauseOperation operation)
149-
{
150-
return operation.ExceptionDeclarationOrExpression == null;
145+
return _disallowedCatchTypes.Any(type => operation.ExceptionType.Equals(type));
151146
}
152147

153148
private static bool MightBeFilteringBasedOnTheCaughtException(ICatchClauseOperation operation)

0 commit comments

Comments
 (0)