-
Notifications
You must be signed in to change notification settings - Fork 289
Add analyzer to detect assertions in catch blocks (MSTEST0058) #6801
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
84edf98
6386294
abc57cc
76c0c6d
4cc67ec
ee9cdc2
d847571
6b3a088
d7f68eb
ad7336f
9e747bf
80a0e9e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,8 @@ | ||
| ; Unshipped analyzer release | ||
| ; https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md | ||
|
|
||
| ### New Rules | ||
|
|
||
| Rule ID | Category | Severity | Notes | ||
| --------|----------|----------|------- | ||
| MSTEST0058 | Usage | Warning | AvoidAssertsInCatchBlocksAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0058) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using System.Collections.Immutable; | ||
|
|
||
| using Analyzer.Utilities.Extensions; | ||
|
|
||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
| using Microsoft.CodeAnalysis.Operations; | ||
|
|
||
| using MSTest.Analyzers.Helpers; | ||
|
|
||
| namespace MSTest.Analyzers; | ||
|
|
||
| /// <summary> | ||
| /// MSTEST0058: <inheritdoc cref="Resources.AvoidAssertsInCatchBlocksTitle"/>. | ||
| /// </summary> | ||
| [DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] | ||
| public sealed class AvoidAssertsInCatchBlocksAnalyzer : DiagnosticAnalyzer | ||
| { | ||
| private static readonly LocalizableResourceString Title = new(nameof(Resources.AvoidAssertsInCatchBlocksTitle), Resources.ResourceManager, typeof(Resources)); | ||
| private static readonly LocalizableResourceString MessageFormat = new(nameof(Resources.AvoidAssertsInCatchBlocksMessageFormat), Resources.ResourceManager, typeof(Resources)); | ||
| private static readonly LocalizableResourceString Description = new(nameof(Resources.AvoidAssertsInCatchBlocksDescription), Resources.ResourceManager, typeof(Resources)); | ||
|
|
||
| internal static readonly DiagnosticDescriptor Rule = DiagnosticDescriptorHelper.Create( | ||
| DiagnosticIds.AvoidAssertsInCatchBlocksRuleId, | ||
| Title, | ||
| MessageFormat, | ||
| Description, | ||
| Category.Usage, | ||
| DiagnosticSeverity.Warning, | ||
| isEnabledByDefault: true); | ||
|
|
||
| /// <inheritdoc /> | ||
| public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } | ||
| = ImmutableArray.Create(Rule); | ||
|
|
||
| /// <inheritdoc /> | ||
| public override void Initialize(AnalysisContext context) | ||
| { | ||
| context.EnableConcurrentExecution(); | ||
| context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
|
|
||
| context.RegisterCompilationStartAction(context => | ||
| { | ||
| Compilation compilation = context.Compilation; | ||
| INamedTypeSymbol? assertSymbol = compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingAssert); | ||
| INamedTypeSymbol? stringAssertSymbol = compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingStringAssert); | ||
| INamedTypeSymbol? collectionAssertSymbol = compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingCollectionAssert); | ||
|
|
||
| if (assertSymbol is not null || stringAssertSymbol is not null || collectionAssertSymbol is not null) | ||
| { | ||
| context.RegisterOperationAction(context => AnalyzeOperation(context, assertSymbol, stringAssertSymbol, collectionAssertSymbol), OperationKind.Invocation); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private static void AnalyzeOperation( | ||
| OperationAnalysisContext context, | ||
| INamedTypeSymbol? assertSymbol, | ||
| INamedTypeSymbol? stringAssertSymbol, | ||
| INamedTypeSymbol? collectionAssertSymbol) | ||
| { | ||
| var operation = (IInvocationOperation)context.Operation; | ||
|
|
||
|
Check failure on line 66 in src/Analyzers/MSTest.Analyzers/AvoidAssertsInCatchBlocksAnalyzer.cs
|
||
Evangelink marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| INamedTypeSymbol targetType = operation.TargetMethod.ContainingType; | ||
| bool isAssertType = | ||
| targetType.Equals(assertSymbol, SymbolEqualityComparer.Default) || | ||
| targetType.Equals(stringAssertSymbol, SymbolEqualityComparer.Default) || | ||
| targetType.Equals(collectionAssertSymbol, SymbolEqualityComparer.Default); | ||
|
|
||
| if (!isAssertType) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // Walk up the operation tree to check if we're inside a catch clause | ||
| if (IsInsideCatchClause(operation)) | ||
| { | ||
| context.ReportDiagnostic(operation.CreateDiagnostic(Rule)); | ||
| } | ||
| } | ||
|
|
||
| private static bool IsInsideCatchClause(IOperation operation) | ||
| { | ||
| IOperation? current = operation; | ||
| while (current is not null) | ||
| { | ||
| if (current is ICatchClauseOperation) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| current = current.Parent; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.