|
| 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 | +using Microsoft.CodeAnalysis.Operations; |
| 11 | + |
| 12 | +using MSTest.Analyzers.Helpers; |
| 13 | + |
| 14 | +namespace MSTest.Analyzers; |
| 15 | + |
| 16 | +[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] |
| 17 | +public sealed class DoNotStoreStaticTestContextAnalyzer : DiagnosticAnalyzer |
| 18 | +{ |
| 19 | + private static readonly LocalizableResourceString Title = new(nameof(Resources.DoNotStoreStaticTestContextAnalyzerTitle), Resources.ResourceManager, typeof(Resources)); |
| 20 | + private static readonly LocalizableResourceString MessageFormat = new(nameof(Resources.DoNotStoreStaticTestContextAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources)); |
| 21 | + |
| 22 | + internal static readonly DiagnosticDescriptor Rule = DiagnosticDescriptorHelper.Create( |
| 23 | + DiagnosticIds.DoNotStoreStaticTestContextAnalyzerRuleId, |
| 24 | + Title, |
| 25 | + MessageFormat, |
| 26 | + null, |
| 27 | + Category.Usage, |
| 28 | + DiagnosticSeverity.Info, |
| 29 | + isEnabledByDefault: true); |
| 30 | + |
| 31 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } |
| 32 | + = ImmutableArray.Create(Rule); |
| 33 | + |
| 34 | + public override void Initialize(AnalysisContext context) |
| 35 | + { |
| 36 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 37 | + context.EnableConcurrentExecution(); |
| 38 | + |
| 39 | + context.RegisterCompilationStartAction(context => |
| 40 | + { |
| 41 | + if (context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingTestContext, out var testContextSymbol)) |
| 42 | + { |
| 43 | + context.RegisterOperationAction(context => AnalyzeOperation(context, testContextSymbol), OperationKind.SimpleAssignment); |
| 44 | + } |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + private static void AnalyzeOperation(OperationAnalysisContext context, INamedTypeSymbol testContextSymbol) |
| 49 | + { |
| 50 | + ISimpleAssignmentOperation assignmentOperation = (ISimpleAssignmentOperation)context.Operation; |
| 51 | + |
| 52 | + if (assignmentOperation.Target is IMemberReferenceOperation memberReferenceOperation |
| 53 | + && memberReferenceOperation.Instance is null |
| 54 | + && assignmentOperation.Value is IParameterReferenceOperation parameterReferenceOperation |
| 55 | + && SymbolEqualityComparer.Default.Equals(parameterReferenceOperation.Type, testContextSymbol)) |
| 56 | + { |
| 57 | + context.ReportDiagnostic(assignmentOperation.CreateDiagnostic(Rule)); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments