|
| 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.Testing.Platform; |
| 11 | + |
| 12 | +using MSTest.Analyzers.Helpers; |
| 13 | + |
| 14 | +namespace MSTest.Analyzers; |
| 15 | + |
| 16 | +/// <summary> |
| 17 | +/// MSTEST0042: <inheritdoc cref="Resources.DuplicateDataRowTitle"/>. |
| 18 | +/// </summary> |
| 19 | +[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] |
| 20 | +public sealed class DuplicateDataRowAnalyzer : DiagnosticAnalyzer |
| 21 | +{ |
| 22 | + internal static readonly DiagnosticDescriptor Rule = DiagnosticDescriptorHelper.Create( |
| 23 | + DiagnosticIds.DuplicateDataRowRuleId, |
| 24 | + new LocalizableResourceString(nameof(Resources.DuplicateDataRowTitle), Resources.ResourceManager, typeof(Resources)), |
| 25 | + new LocalizableResourceString(nameof(Resources.DuplicateDataRowMessageFormat), Resources.ResourceManager, typeof(Resources)), |
| 26 | + null, |
| 27 | + Category.Usage, |
| 28 | + DiagnosticSeverity.Warning, |
| 29 | + isEnabledByDefault: true); |
| 30 | + |
| 31 | + /// <inheritdoc /> |
| 32 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule); |
| 33 | + |
| 34 | + /// <inheritdoc /> |
| 35 | + public override void Initialize(AnalysisContext context) |
| 36 | + { |
| 37 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 38 | + context.EnableConcurrentExecution(); |
| 39 | + |
| 40 | + context.RegisterCompilationStartAction(context => |
| 41 | + { |
| 42 | + if (context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingDataRowAttribute, out INamedTypeSymbol? dataRowAttribute)) |
| 43 | + { |
| 44 | + context.RegisterSymbolAction( |
| 45 | + context => AnalyzeSymbol(context, dataRowAttribute), |
| 46 | + SymbolKind.Method); |
| 47 | + } |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol dataRowAttribute) |
| 52 | + { |
| 53 | + var methodSymbol = (IMethodSymbol)context.Symbol; |
| 54 | + var dataRowArguments = new HashSet<ImmutableArray<TypedConstant>>(TypedConstantArrayComparer.Instance); |
| 55 | + |
| 56 | + foreach (AttributeData attribute in methodSymbol.GetAttributes()) |
| 57 | + { |
| 58 | + if (!dataRowAttribute.Equals(attribute.AttributeClass, SymbolEqualityComparer.Default)) |
| 59 | + { |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + if (!dataRowArguments.Add(attribute.ConstructorArguments)) |
| 64 | + { |
| 65 | + context.ReportDiagnostic((attribute.ApplicationSyntaxReference?.GetSyntax(context.CancellationToken).GetLocation() ?? Location.None).CreateDiagnostic(Rule)); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private sealed class TypedConstantArrayComparer : IEqualityComparer<ImmutableArray<TypedConstant>> |
| 71 | + { |
| 72 | + public static TypedConstantArrayComparer Instance { get; } = new(); |
| 73 | + |
| 74 | + public bool Equals(ImmutableArray<TypedConstant> x, ImmutableArray<TypedConstant> y) |
| 75 | + { |
| 76 | + if (x.Length != y.Length) |
| 77 | + { |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + for (int i = 0; i < x.Length; i++) |
| 82 | + { |
| 83 | + if (AreTypedConstantEquals(x[i], y[i])) |
| 84 | + { |
| 85 | + return false; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return true; |
| 90 | + } |
| 91 | + |
| 92 | + private static bool AreTypedConstantEquals(TypedConstant typedConstant1, TypedConstant typedConstant2) |
| 93 | + { |
| 94 | + // If the Kind doesn't match or the Type doesn't match, they are not equal. |
| 95 | + if (typedConstant1.Kind != typedConstant2.Kind || |
| 96 | + SymbolEqualityComparer.Default.Equals(typedConstant1.Type, typedConstant2.Type)) |
| 97 | + { |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + // If IsNull is true and the Kind is array, Values will return default(ImmutableArray<TypedConstant>), not empty. |
| 102 | + // To avoid dealing with that, we do the quick IsNull checks first. |
| 103 | + // If both are nulls, then we are good, everything is equal. |
| 104 | + if (typedConstant1.IsNull && typedConstant2.IsNull) |
| 105 | + { |
| 106 | + return true; |
| 107 | + } |
| 108 | + |
| 109 | + // If only one is null, then we are not equal. |
| 110 | + if (typedConstant1.IsNull || typedConstant2.IsNull) |
| 111 | + { |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + // If the kind is array (at this point we know both have the same Kind), we compare Values. |
| 116 | + // Accessing `Value` property for arrays will throw so we need to have explicit check to decide whether |
| 117 | + // we compare `Value` or `Values`. |
| 118 | + if (typedConstant1.Kind == TypedConstantKind.Array) |
| 119 | + { |
| 120 | + return TypedConstantArrayComparer.Instance.Equals(typedConstant1.Values, typedConstant2.Values); |
| 121 | + } |
| 122 | + |
| 123 | + // At this point, the type is matching and the kind is matching and is not array. |
| 124 | + return object.Equals(typedConstant1.Value, typedConstant2.Value); |
| 125 | + } |
| 126 | + |
| 127 | + public int GetHashCode(ImmutableArray<TypedConstant> obj) |
| 128 | + { |
| 129 | + var hashCode = default(RoslynHashCode); |
| 130 | + foreach (TypedConstant typedConstant in obj) |
| 131 | + { |
| 132 | + hashCode.Add(typedConstant.Kind); |
| 133 | + hashCode.Add(SymbolEqualityComparer.Default.GetHashCode(typedConstant.Type)); |
| 134 | + } |
| 135 | + |
| 136 | + return hashCode.ToHashCode(); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments