|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using System.Collections.Immutable; |
| 5 | + |
| 6 | +using Analyzer.Utilities.Extensions; |
| 7 | + |
| 8 | +using Microsoft.CodeAnalysis; |
| 9 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 10 | + |
| 11 | +using MSTest.Analyzers.Helpers; |
| 12 | + |
| 13 | +namespace MSTest.Analyzers; |
| 14 | + |
| 15 | +[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] |
| 16 | +public sealed class PreferTestCleanupOverDisposeAnalyzer : DiagnosticAnalyzer |
| 17 | +{ |
| 18 | + private static readonly LocalizableResourceString Title = new(nameof(Resources.PreferTestCleanupOverDisposeTitle), Resources.ResourceManager, typeof(Resources)); |
| 19 | + private static readonly LocalizableResourceString MessageFormat = new(nameof(Resources.PreferTestCleanupOverDisposeMessageFormat), Resources.ResourceManager, typeof(Resources)); |
| 20 | + |
| 21 | + internal static readonly DiagnosticDescriptor Rule = DiagnosticDescriptorHelper.Create( |
| 22 | + DiagnosticIds.PreferTestCleanupOverDisposeRuleId, |
| 23 | + Title, |
| 24 | + MessageFormat, |
| 25 | + null, |
| 26 | + Category.Design, |
| 27 | + DiagnosticSeverity.Info, |
| 28 | + isEnabledByDefault: false); |
| 29 | + |
| 30 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } |
| 31 | + = ImmutableArray.Create(Rule); |
| 32 | + |
| 33 | + public override void Initialize(AnalysisContext context) |
| 34 | + { |
| 35 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 36 | + context.EnableConcurrentExecution(); |
| 37 | + |
| 38 | + context.RegisterCompilationStartAction(context => |
| 39 | + { |
| 40 | + if (context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemIDisposable, out var idisposableSymbol)) |
| 41 | + { |
| 42 | + var iasyncDisposableSymbol = context.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemIAsyncDisposable); |
| 43 | + var valueTaskSymbol = context.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemThreadingTasksValueTask); |
| 44 | + context.RegisterSymbolAction(context => AnalyzeSymbol(context, idisposableSymbol, iasyncDisposableSymbol, valueTaskSymbol), SymbolKind.Method); |
| 45 | + } |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol idisposableSymbol, INamedTypeSymbol? iasyncDisposableSymbol, |
| 50 | + INamedTypeSymbol? valueTaskSymbol) |
| 51 | + { |
| 52 | + IMethodSymbol methodSymbol = (IMethodSymbol)context.Symbol; |
| 53 | + |
| 54 | + if (methodSymbol.IsAsyncDisposeImplementation(iasyncDisposableSymbol, valueTaskSymbol) |
| 55 | + || methodSymbol.IsDisposeImplementation(idisposableSymbol)) |
| 56 | + { |
| 57 | + context.ReportDiagnostic(methodSymbol.CreateDiagnostic(Rule)); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments