Skip to content

Commit

Permalink
Add a configurable option disallowed_symbol_names to CA1031 to allo…
Browse files Browse the repository at this point in the history
…w configuration of disallowed exception types.

Fixes #2713
  • Loading branch information
mavasani committed Sep 3, 2019
1 parent 3f44df7 commit 40cd301
Show file tree
Hide file tree
Showing 20 changed files with 177 additions and 61 deletions.
23 changes: 23 additions & 0 deletions docs/Analyzer Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,29 @@ Examples:
|`dotnet_code_quality.excluded_type_names_with_derived_types = M:NS.MyType` | Matches specific type 'MyType' with given fully qualified name and all of its derived types
|`dotnet_code_quality.excluded_type_names_with_derived_types = M:NS1.MyType1\|M:NS2.MyType2` | Matches specific types 'MyType1' and 'MyType2' with respective fully qualified names and all of their derived types

### Disallowed symbol names
Option Name: `disallowed_symbol_names`

Configurable Rules: [CA1031](https://docs.microsoft.com/visualstudio/code-quality/ca1031-do-not-catch-general-exception-types)

Option Values: Names of symbols (separated by '|') that are disallowed in the context of the analysis.
Allowed symbol name formats:
1. Symbol name only (includes all symbols with the name, regardless of the containing type or namespace)
2. Fully qualified names in the symbol's documentation ID format: https://github.com/dotnet/csharplang/blob/master/spec/documentation-comments.md#id-string-format.
Note that each symbol name requires a symbol kind prefix, such as "M:" prefix for methods, "T:" prefix for types, "N:" prefix for namespaces, etc.
3. `.ctor` for constructors and `.cctor` for static constructors

Default Value: None

Examples:

| Option Value | Summary |
| --- | --- |
|`dotnet_code_quality.disallowed_symbol_names = Validate` | Matches all symbols named 'Validate' in the compilation
|`dotnet_code_quality.disallowed_symbol_names = Validate1\|Validate2` | Matches all symbols named either 'Validate1' or 'Validate2' in the compilation
|`dotnet_code_quality.disallowed_symbol_names = M:NS.MyType.Validate(ParamType)` | Matches specific method 'Validate' with given fully qualified signature
|`dotnet_code_quality.disallowed_symbol_names = M:NS1.MyType1.Validate1(ParamType)\|M:NS2.MyType2.Validate2(ParamType)` | Matches specific methods 'Validate1' and 'Validate2' with respective fully qualified signature

### Dataflow analysis

Configurable Rules: [CA1062](https://docs.microsoft.com/visualstudio/code-quality/ca1062-validate-arguments-of-public-methods), [CA1303](https://docs.microsoft.com/visualstudio/code-quality/ca1303-do-not-pass-literals-as-localized-parameters), [CA1508](../src/Microsoft.CodeQuality.Analyzers/Microsoft.CodeQuality.Analyzers.md#ca1508-avoid-dead-conditional-code), [CA2000](https://docs.microsoft.com/visualstudio/code-quality/ca2000-dispose-objects-before-losing-scope), [CA2100](https://docs.microsoft.com/visualstudio/code-quality/ca2100-review-sql-queries-for-security-vulnerabilities), [CA2213](https://docs.microsoft.com/visualstudio/code-quality/ca2213-disposable-fields-should-be-disposed), Taint analysis rules
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Immutable;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Analyzer.Utilities;
Expand Down Expand Up @@ -39,5 +40,16 @@ protected override Diagnostic CreateDiagnostic(IMethodSymbol containingMethod, S
{
return catchKeyword.CreateDiagnostic(Rule, containingMethod.Name);
}

protected override bool IsConfiguredDisallowedExceptionType(INamedTypeSymbol namedTypeSymbol, Compilation compilation, AnalyzerOptions analyzerOptions, CancellationToken cancellationToken)
{
if (base.IsConfiguredDisallowedExceptionType(namedTypeSymbol, compilation, analyzerOptions, cancellationToken))
{
return true;
}

var symbolNamesOption = analyzerOptions.GetDisallowedSymbolNamesOption(Rule, compilation, cancellationToken);
return symbolNamesOption.Contains(namedTypeSymbol);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1008,10 +1008,10 @@
<comment>1 is a comma-separated list</comment>
</data>
<data name="DoNotCatchGeneralExceptionTypesDescription" xml:space="preserve">
<value>A general exception such as System.Exception or System.SystemException is caught in a catch statement, or a general catch clause is used. General exceptions should not be caught.</value>
<value>A general exception such as System.Exception or System.SystemException or a disallowed exception type is caught in a catch statement, or a general catch clause is used. General and disallowed exceptions should not be caught.</value>
</data>
<data name="DoNotCatchGeneralExceptionTypesMessage" xml:space="preserve">
<value>Modify '{0}' to catch a more specific exception type, or rethrow the exception.</value>
<value>Modify '{0}' to catch a more specific allowed exception type, or rethrow the exception.</value>
</data>
<data name="DoNotCatchGeneralExceptionTypesTitle" xml:space="preserve">
<value>Do not catch general exception types</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
<note />
</trans-unit>
<trans-unit id="DoNotCatchGeneralExceptionTypesDescription">
<source>A general exception such as System.Exception or System.SystemException is caught in a catch statement, or a general catch clause is used. General exceptions should not be caught.</source>
<target state="translated">Obecná výjimka (například System.Exception nebo System.SystemException) se zachytává příkazem catch nebo se používá obecná klauzule catch. Obecné výjimky by se neměly zachytávat.</target>
<source>A general exception such as System.Exception or System.SystemException or a disallowed exception type is caught in a catch statement, or a general catch clause is used. General and disallowed exceptions should not be caught.</source>
<target state="needs-review-translation">Obecná výjimka (například System.Exception nebo System.SystemException) se zachytává příkazem catch nebo se používá obecná klauzule catch. Obecné výjimky by se neměly zachytávat.</target>
<note />
</trans-unit>
<trans-unit id="DoNotCatchGeneralExceptionTypesMessage">
<source>Modify '{0}' to catch a more specific exception type, or rethrow the exception.</source>
<target state="translated">Upravte {0} na zachycení specifičtějšího typu výjimky nebo znovu vyvolejte výjimku.</target>
<source>Modify '{0}' to catch a more specific allowed exception type, or rethrow the exception.</source>
<target state="needs-review-translation">Upravte {0} na zachycení specifičtějšího typu výjimky nebo znovu vyvolejte výjimku.</target>
<note />
</trans-unit>
<trans-unit id="DoNotCatchGeneralExceptionTypesTitle">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
<note />
</trans-unit>
<trans-unit id="DoNotCatchGeneralExceptionTypesDescription">
<source>A general exception such as System.Exception or System.SystemException is caught in a catch statement, or a general catch clause is used. General exceptions should not be caught.</source>
<target state="translated">Eine allgemeine Ausnahme wie "System.Exception" oder "System.SystemException" wurde durch eine catch-Anweisung abgefangen, oder es wird eine Klausel verwendet, um Ausnahmen generell abzufangen. Allgemeine Ausnahmen sollten jedoch nicht abgefangen werden.</target>
<source>A general exception such as System.Exception or System.SystemException or a disallowed exception type is caught in a catch statement, or a general catch clause is used. General and disallowed exceptions should not be caught.</source>
<target state="needs-review-translation">Eine allgemeine Ausnahme wie "System.Exception" oder "System.SystemException" wurde durch eine catch-Anweisung abgefangen, oder es wird eine Klausel verwendet, um Ausnahmen generell abzufangen. Allgemeine Ausnahmen sollten jedoch nicht abgefangen werden.</target>
<note />
</trans-unit>
<trans-unit id="DoNotCatchGeneralExceptionTypesMessage">
<source>Modify '{0}' to catch a more specific exception type, or rethrow the exception.</source>
<target state="translated">Ändern Sie "{0}", um spezifischere Ausnahmetypen abzufangen, oder lösen Sie die Ausnahme erneut aus.</target>
<source>Modify '{0}' to catch a more specific allowed exception type, or rethrow the exception.</source>
<target state="needs-review-translation">Ändern Sie "{0}", um spezifischere Ausnahmetypen abzufangen, oder lösen Sie die Ausnahme erneut aus.</target>
<note />
</trans-unit>
<trans-unit id="DoNotCatchGeneralExceptionTypesTitle">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
<note />
</trans-unit>
<trans-unit id="DoNotCatchGeneralExceptionTypesDescription">
<source>A general exception such as System.Exception or System.SystemException is caught in a catch statement, or a general catch clause is used. General exceptions should not be caught.</source>
<target state="translated">Una excepción general como System.Exception o System.SystemException se captura con una instrucción catch, o bien se utiliza una cláusula catch general. No deben capturarse excepciones generales.</target>
<source>A general exception such as System.Exception or System.SystemException or a disallowed exception type is caught in a catch statement, or a general catch clause is used. General and disallowed exceptions should not be caught.</source>
<target state="needs-review-translation">Una excepción general como System.Exception o System.SystemException se captura con una instrucción catch, o bien se utiliza una cláusula catch general. No deben capturarse excepciones generales.</target>
<note />
</trans-unit>
<trans-unit id="DoNotCatchGeneralExceptionTypesMessage">
<source>Modify '{0}' to catch a more specific exception type, or rethrow the exception.</source>
<target state="translated">Modifique "{0}" para capturar un tipo de excepción más específico, o bien reinicie la excepción.</target>
<source>Modify '{0}' to catch a more specific allowed exception type, or rethrow the exception.</source>
<target state="needs-review-translation">Modifique "{0}" para capturar un tipo de excepción más específico, o bien reinicie la excepción.</target>
<note />
</trans-unit>
<trans-unit id="DoNotCatchGeneralExceptionTypesTitle">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
<note />
</trans-unit>
<trans-unit id="DoNotCatchGeneralExceptionTypesDescription">
<source>A general exception such as System.Exception or System.SystemException is caught in a catch statement, or a general catch clause is used. General exceptions should not be caught.</source>
<target state="translated">Une exception générale de type System.Exception ou System.SystemException est interceptée dans une instruction catch, ou une clause catch générale est utilisée. Les exceptions générales ne doivent pas être interceptées.</target>
<source>A general exception such as System.Exception or System.SystemException or a disallowed exception type is caught in a catch statement, or a general catch clause is used. General and disallowed exceptions should not be caught.</source>
<target state="needs-review-translation">Une exception générale de type System.Exception ou System.SystemException est interceptée dans une instruction catch, ou une clause catch générale est utilisée. Les exceptions générales ne doivent pas être interceptées.</target>
<note />
</trans-unit>
<trans-unit id="DoNotCatchGeneralExceptionTypesMessage">
<source>Modify '{0}' to catch a more specific exception type, or rethrow the exception.</source>
<target state="translated">Modifiez '{0}' pour intercepter un type d'exception plus spécifique ou relevez l'exception.</target>
<source>Modify '{0}' to catch a more specific allowed exception type, or rethrow the exception.</source>
<target state="needs-review-translation">Modifiez '{0}' pour intercepter un type d'exception plus spécifique ou relevez l'exception.</target>
<note />
</trans-unit>
<trans-unit id="DoNotCatchGeneralExceptionTypesTitle">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
<note />
</trans-unit>
<trans-unit id="DoNotCatchGeneralExceptionTypesDescription">
<source>A general exception such as System.Exception or System.SystemException is caught in a catch statement, or a general catch clause is used. General exceptions should not be caught.</source>
<target state="translated">In un'istruzione catch è stata rilevata un'eccezione generale, come System.Exception o System.SystemException, oppure viene usata una clausola catch generale. Le eccezioni generali non dovrebbero essere rilevate.</target>
<source>A general exception such as System.Exception or System.SystemException or a disallowed exception type is caught in a catch statement, or a general catch clause is used. General and disallowed exceptions should not be caught.</source>
<target state="needs-review-translation">In un'istruzione catch è stata rilevata un'eccezione generale, come System.Exception o System.SystemException, oppure viene usata una clausola catch generale. Le eccezioni generali non dovrebbero essere rilevate.</target>
<note />
</trans-unit>
<trans-unit id="DoNotCatchGeneralExceptionTypesMessage">
<source>Modify '{0}' to catch a more specific exception type, or rethrow the exception.</source>
<target state="translated">Modificare '{0}' in modo da rilevare un tipo di eccezione più specifico o generare di nuovo l'eccezione.</target>
<source>Modify '{0}' to catch a more specific allowed exception type, or rethrow the exception.</source>
<target state="needs-review-translation">Modificare '{0}' in modo da rilevare un tipo di eccezione più specifico o generare di nuovo l'eccezione.</target>
<note />
</trans-unit>
<trans-unit id="DoNotCatchGeneralExceptionTypesTitle">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
<note />
</trans-unit>
<trans-unit id="DoNotCatchGeneralExceptionTypesDescription">
<source>A general exception such as System.Exception or System.SystemException is caught in a catch statement, or a general catch clause is used. General exceptions should not be caught.</source>
<target state="translated">System.Exception または System.SystemException などの一般的な例外は catch ステートメントでキャッチされます。または一般的な catch 句を使用します。一般的な例外をキャッチしないでください。</target>
<source>A general exception such as System.Exception or System.SystemException or a disallowed exception type is caught in a catch statement, or a general catch clause is used. General and disallowed exceptions should not be caught.</source>
<target state="needs-review-translation">System.Exception または System.SystemException などの一般的な例外は catch ステートメントでキャッチされます。または一般的な catch 句を使用します。一般的な例外をキャッチしないでください。</target>
<note />
</trans-unit>
<trans-unit id="DoNotCatchGeneralExceptionTypesMessage">
<source>Modify '{0}' to catch a more specific exception type, or rethrow the exception.</source>
<target state="translated">'{0}' を変更して特定の例外の種類をさらにキャッチするか、例外を再スローします。</target>
<source>Modify '{0}' to catch a more specific allowed exception type, or rethrow the exception.</source>
<target state="needs-review-translation">'{0}' を変更して特定の例外の種類をさらにキャッチするか、例外を再スローします。</target>
<note />
</trans-unit>
<trans-unit id="DoNotCatchGeneralExceptionTypesTitle">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
<note />
</trans-unit>
<trans-unit id="DoNotCatchGeneralExceptionTypesDescription">
<source>A general exception such as System.Exception or System.SystemException is caught in a catch statement, or a general catch clause is used. General exceptions should not be caught.</source>
<target state="translated">System.Exception 또는 System.SystemException과 같은 일반 예외가 catch 문에서 catch되거나, 일반 catch 절이 사용됩니다. 일반 예외는 catch되면 안 됩니다.</target>
<source>A general exception such as System.Exception or System.SystemException or a disallowed exception type is caught in a catch statement, or a general catch clause is used. General and disallowed exceptions should not be caught.</source>
<target state="needs-review-translation">System.Exception 또는 System.SystemException과 같은 일반 예외가 catch 문에서 catch되거나, 일반 catch 절이 사용됩니다. 일반 예외는 catch되면 안 됩니다.</target>
<note />
</trans-unit>
<trans-unit id="DoNotCatchGeneralExceptionTypesMessage">
<source>Modify '{0}' to catch a more specific exception type, or rethrow the exception.</source>
<target state="translated">'{0}'을(를) 수정하여 좀 더 구체적인 예외 유형을 catch하거나 예외를 다시 throw합니다.</target>
<source>Modify '{0}' to catch a more specific allowed exception type, or rethrow the exception.</source>
<target state="needs-review-translation">'{0}'을(를) 수정하여 좀 더 구체적인 예외 유형을 catch하거나 예외를 다시 throw합니다.</target>
<note />
</trans-unit>
<trans-unit id="DoNotCatchGeneralExceptionTypesTitle">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
<note />
</trans-unit>
<trans-unit id="DoNotCatchGeneralExceptionTypesDescription">
<source>A general exception such as System.Exception or System.SystemException is caught in a catch statement, or a general catch clause is used. General exceptions should not be caught.</source>
<target state="translated">W instrukcji catch przechwycono wyjątek ogólny, taki jak System.Exception lub System.SystemException, lub jest używana ogólna klauzula catch. Wyjątki ogólne nie powinny być przechwytywane.</target>
<source>A general exception such as System.Exception or System.SystemException or a disallowed exception type is caught in a catch statement, or a general catch clause is used. General and disallowed exceptions should not be caught.</source>
<target state="needs-review-translation">W instrukcji catch przechwycono wyjątek ogólny, taki jak System.Exception lub System.SystemException, lub jest używana ogólna klauzula catch. Wyjątki ogólne nie powinny być przechwytywane.</target>
<note />
</trans-unit>
<trans-unit id="DoNotCatchGeneralExceptionTypesMessage">
<source>Modify '{0}' to catch a more specific exception type, or rethrow the exception.</source>
<target state="translated">Zmodyfikuj element „{0}”, aby przechwytywać dokładniej określony typ wyjątku, lub ponownie zgłoś wyjątek.</target>
<source>Modify '{0}' to catch a more specific allowed exception type, or rethrow the exception.</source>
<target state="needs-review-translation">Zmodyfikuj element „{0}”, aby przechwytywać dokładniej określony typ wyjątku, lub ponownie zgłoś wyjątek.</target>
<note />
</trans-unit>
<trans-unit id="DoNotCatchGeneralExceptionTypesTitle">
Expand Down
Loading

0 comments on commit 40cd301

Please sign in to comment.