|
| 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 | +/// <summary> |
| 16 | +/// MSTEST0036: <inheritdoc cref="Resources.DoNotUseShadowingTitle"/>. |
| 17 | +/// </summary> |
| 18 | +[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] |
| 19 | +public sealed class DoNotUseShadowingAnalyzer : DiagnosticAnalyzer |
| 20 | +{ |
| 21 | + private static readonly LocalizableResourceString Title = new(nameof(Resources.DoNotUseShadowingTitle), Resources.ResourceManager, typeof(Resources)); |
| 22 | + private static readonly LocalizableResourceString Description = new(nameof(Resources.DoNotUseShadowingDescription), Resources.ResourceManager, typeof(Resources)); |
| 23 | + private static readonly LocalizableResourceString MessageFormat = new(nameof(Resources.DoNotUseShadowingMessageFormat), Resources.ResourceManager, typeof(Resources)); |
| 24 | + |
| 25 | + internal static readonly DiagnosticDescriptor DoNotUseShadowingRule = DiagnosticDescriptorHelper.Create( |
| 26 | + DiagnosticIds.DoNotUseShadowingRuleId, |
| 27 | + Title, |
| 28 | + MessageFormat, |
| 29 | + Description, |
| 30 | + Category.Design, |
| 31 | + DiagnosticSeverity.Warning, |
| 32 | + isEnabledByDefault: true); |
| 33 | + |
| 34 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } |
| 35 | + = ImmutableArray.Create(DoNotUseShadowingRule); |
| 36 | + |
| 37 | + public override void Initialize(AnalysisContext context) |
| 38 | + { |
| 39 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 40 | + context.EnableConcurrentExecution(); |
| 41 | + |
| 42 | + context.RegisterCompilationStartAction(context => |
| 43 | + { |
| 44 | + if (context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingTestClassAttribute, out INamedTypeSymbol? testClassAttributeSymbol)) |
| 45 | + { |
| 46 | + context.RegisterSymbolAction( |
| 47 | + context => AnalyzeSymbol(context, testClassAttributeSymbol), |
| 48 | + SymbolKind.NamedType); |
| 49 | + } |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol testClassAttributeSymbol) |
| 54 | + { |
| 55 | + var namedTypeSymbol = (INamedTypeSymbol)context.Symbol; |
| 56 | + if (namedTypeSymbol.TypeKind != TypeKind.Class |
| 57 | + || !namedTypeSymbol.GetAttributes().Any(attr => SymbolEqualityComparer.Default.Equals(attr.AttributeClass, testClassAttributeSymbol))) |
| 58 | + { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + Dictionary<string, List<ISymbol>> symbolGroups = GetBaseMembers(namedTypeSymbol); |
| 63 | + foreach (ISymbol member in namedTypeSymbol.GetMembers()) |
| 64 | + { |
| 65 | + foreach (ISymbol baseMember in symbolGroups.GetValueOrDefault(member.Name, new List<ISymbol>())) |
| 66 | + { |
| 67 | + // Check if the member is shadowing a base class member |
| 68 | + if (IsMemberShadowing(member, baseMember)) |
| 69 | + { |
| 70 | + context.ReportDiagnostic(member.CreateDiagnostic(DoNotUseShadowingRule, member.Name)); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private static Dictionary<string, List<ISymbol>> GetBaseMembers(INamedTypeSymbol namedTypeSymbol) |
| 77 | + { |
| 78 | + Dictionary<string, List<ISymbol>> symbolGroups = new(); |
| 79 | + INamedTypeSymbol? currentType = namedTypeSymbol.BaseType; |
| 80 | + while (currentType is not null) |
| 81 | + { |
| 82 | + foreach (ISymbol member in currentType.GetMembers()) |
| 83 | + { |
| 84 | + if ((member is IMethodSymbol methodSymbol && (methodSymbol.MethodKind == MethodKind.PropertyGet || methodSymbol.MethodKind == MethodKind.PropertySet)) |
| 85 | + || member.IsOverride || member.Name == ".ctor") |
| 86 | + { |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + if (!symbolGroups.TryGetValue(member.Name, out List<ISymbol>? members)) |
| 91 | + { |
| 92 | + members = new List<ISymbol>(); |
| 93 | + symbolGroups[member.Name] = members; |
| 94 | + } |
| 95 | + |
| 96 | + // Add the member to the list |
| 97 | + members.Add(member); |
| 98 | + } |
| 99 | + |
| 100 | + currentType = currentType.BaseType; |
| 101 | + } |
| 102 | + |
| 103 | + return symbolGroups; |
| 104 | + } |
| 105 | + |
| 106 | + private static bool IsMemberShadowing(ISymbol member, ISymbol baseMember) |
| 107 | + { |
| 108 | + // Ensure both members are of the same kind (Method, Property, etc.) |
| 109 | + if (member.Kind != baseMember.Kind || member.IsOverride) |
| 110 | + { |
| 111 | + return false; |
| 112 | + } |
| 113 | + |
| 114 | + // Compare methods |
| 115 | + if (member is IMethodSymbol methodSymbol && baseMember is IMethodSymbol baseMethodSymbol) |
| 116 | + { |
| 117 | + return methodSymbol.Name == baseMethodSymbol.Name && |
| 118 | + methodSymbol.Parameters.Length == baseMethodSymbol.Parameters.Length && |
| 119 | + methodSymbol.Parameters.Zip(baseMethodSymbol.Parameters, (p1, p2) => |
| 120 | + SymbolEqualityComparer.Default.Equals(p1.Type, p2.Type)).All(equal => equal); |
| 121 | + } |
| 122 | + |
| 123 | + // Compare properties |
| 124 | + else if (member is IPropertySymbol propertySymbol && baseMember is IPropertySymbol basePropertySymbol) |
| 125 | + { |
| 126 | + return propertySymbol.Name == basePropertySymbol.Name && |
| 127 | + SymbolEqualityComparer.Default.Equals(propertySymbol.Type, basePropertySymbol.Type); |
| 128 | + } |
| 129 | + |
| 130 | + return false; |
| 131 | + } |
| 132 | +} |
0 commit comments