|
| 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 PreferAssertFailOverAlwaysFalseConditionsAnalyzer : DiagnosticAnalyzer |
| 18 | +{ |
| 19 | + private enum EqualityStatus |
| 20 | + { |
| 21 | + Unknown, |
| 22 | + Equal, |
| 23 | + NotEqual, |
| 24 | + } |
| 25 | + |
| 26 | + private const string ExpectedParameterName = "expected"; |
| 27 | + private const string NotExpectedParameterName = "notExpected"; |
| 28 | + private const string ActualParameterName = "actual"; |
| 29 | + private const string ConditionParameterName = "condition"; |
| 30 | + private const string ValueParameterName = "value"; |
| 31 | + |
| 32 | + private static readonly LocalizableResourceString Title = new(nameof(Resources.PreferAssertFailOverAlwaysFalseConditionsTitle), Resources.ResourceManager, typeof(Resources)); |
| 33 | + private static readonly LocalizableResourceString MessageFormat = new(nameof(Resources.PreferAssertFailOverAlwaysFalseConditionsMessageFormat), Resources.ResourceManager, typeof(Resources)); |
| 34 | + |
| 35 | + internal static readonly DiagnosticDescriptor Rule = DiagnosticDescriptorHelper.Create( |
| 36 | + DiagnosticIds.PreferAssertFailOverAlwaysFalseConditionsRuleId, |
| 37 | + Title, |
| 38 | + MessageFormat, |
| 39 | + null, |
| 40 | + Category.Design, |
| 41 | + DiagnosticSeverity.Info, |
| 42 | + isEnabledByDefault: true); |
| 43 | + |
| 44 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } |
| 45 | + = ImmutableArray.Create(Rule); |
| 46 | + |
| 47 | + public override void Initialize(AnalysisContext context) |
| 48 | + { |
| 49 | + context.EnableConcurrentExecution(); |
| 50 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); |
| 51 | + |
| 52 | + context.RegisterCompilationStartAction(context => |
| 53 | + { |
| 54 | + Compilation compilation = context.Compilation; |
| 55 | + INamedTypeSymbol? assertSymbol = compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingAssert); |
| 56 | + if (assertSymbol is not null) |
| 57 | + { |
| 58 | + context.RegisterOperationAction(context => AnalyzeOperation(context, assertSymbol), OperationKind.Invocation); |
| 59 | + } |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + private static void AnalyzeOperation(OperationAnalysisContext context, INamedTypeSymbol assertSymbol) |
| 64 | + { |
| 65 | + var operation = (IInvocationOperation)context.Operation; |
| 66 | + if (assertSymbol.Equals(operation.TargetMethod?.ContainingType, SymbolEqualityComparer.Default) && |
| 67 | + IsAlwaysFalse(operation)) |
| 68 | + { |
| 69 | + context.ReportDiagnostic(operation.CreateDiagnostic(Rule, operation.TargetMethod.Name)); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private static bool IsAlwaysFalse(IInvocationOperation operation) |
| 74 | + => operation.TargetMethod.Name switch |
| 75 | + { |
| 76 | + "IsTrue" => GetConditionArgument(operation) is { Value.ConstantValue: { HasValue: true, Value: false } }, |
| 77 | + "IsFalse" => GetConditionArgument(operation) is { Value.ConstantValue: { HasValue: true, Value: true } }, |
| 78 | + "AreEqual" => GetEqualityStatus(operation, ExpectedParameterName) == EqualityStatus.NotEqual, |
| 79 | + "AreNotEqual" => GetEqualityStatus(operation, NotExpectedParameterName) == EqualityStatus.Equal, |
| 80 | + "IsNotNull" => GetValueArgument(operation) is { Value.ConstantValue: { HasValue: true, Value: null } }, |
| 81 | + _ => false, |
| 82 | + }; |
| 83 | + |
| 84 | + private static IArgumentOperation? GetArgumentWithName(IInvocationOperation operation, string name) |
| 85 | + => operation.Arguments.FirstOrDefault(arg => arg.Parameter?.Name == name); |
| 86 | + |
| 87 | + private static IArgumentOperation? GetConditionArgument(IInvocationOperation operation) |
| 88 | + => GetArgumentWithName(operation, ConditionParameterName); |
| 89 | + |
| 90 | + private static IArgumentOperation? GetValueArgument(IInvocationOperation operation) |
| 91 | + => GetArgumentWithName(operation, ValueParameterName); |
| 92 | + |
| 93 | + private static EqualityStatus GetEqualityStatus(IInvocationOperation operation, string expectedOrNotExpectedParameterName) |
| 94 | + { |
| 95 | + if (GetArgumentWithName(operation, expectedOrNotExpectedParameterName) is { } expectedOrNotExpectedArgument && |
| 96 | + GetArgumentWithName(operation, ActualParameterName) is { } actualArgument && |
| 97 | + expectedOrNotExpectedArgument.Value.ConstantValue.HasValue && |
| 98 | + actualArgument.Value.ConstantValue.HasValue) |
| 99 | + { |
| 100 | + return Equals(expectedOrNotExpectedArgument.Value.ConstantValue.Value, actualArgument.Value.ConstantValue.Value) ? EqualityStatus.Equal : EqualityStatus.NotEqual; |
| 101 | + } |
| 102 | + |
| 103 | + // We are not sure about the equality status |
| 104 | + return EqualityStatus.Unknown; |
| 105 | + } |
| 106 | +} |
0 commit comments