From 1d0ea7852e66a8223cf09e759bd82786557e8a90 Mon Sep 17 00:00:00 2001 From: Yaakov Date: Thu, 20 May 2021 15:57:16 +1000 Subject: [PATCH] Overrides with additional attributes aren't entirely pointless. (#148) * Add test for subclass adding attributes * Add tests for property and event * add Indexer test * Overrides that declare attributes should not be considered pointless. --- .../Attributes/Source.cs | 53 +++++++++++++++++++ .../PointlessOverrideAnalyzer.cs | 20 +++++++ 2 files changed, 73 insertions(+) create mode 100644 WTG.Analyzers.Test/TestData/PointlessOverrideAnalyzer/Attributes/Source.cs diff --git a/WTG.Analyzers.Test/TestData/PointlessOverrideAnalyzer/Attributes/Source.cs b/WTG.Analyzers.Test/TestData/PointlessOverrideAnalyzer/Attributes/Source.cs new file mode 100644 index 00000000..dc9aad52 --- /dev/null +++ b/WTG.Analyzers.Test/TestData/PointlessOverrideAnalyzer/Attributes/Source.cs @@ -0,0 +1,53 @@ +using System; +using System.ComponentModel; + +namespace Magic +{ + class DerivedClass : BaseClass + { + [Obsolete("Please don't use this anymore")] + public override void Method(int argument1) => base.Method(argument1); + + [EditorBrowsable(EditorBrowsableState.Never)] + public override object Property + { + get => base.Property; + set => base.Property = value; + } + + [EditorBrowsable(EditorBrowsableState.Advanced)] + public override event EventHandler Event + { + add => base.Event += value; + remove => base.Event -= value; + } + + [Obsolete("Please don't use this anymore")] + public override object this[int index] + { + get => base[index]; + set => base[index] = value; + } + } + + class BaseClass + { + public virtual void Method(int argument1) + { + } + + public virtual object Property { get; set; } + + public virtual event EventHandler Event + { + add { } + remove { } + } + + public virtual object this[int index] + { + get => null; + set { } + } + } +} diff --git a/WTG.Analyzers/Analyzers/PointlessOverride/PointlessOverrideAnalyzer.cs b/WTG.Analyzers/Analyzers/PointlessOverride/PointlessOverrideAnalyzer.cs index 7c535388..7c5f0546 100644 --- a/WTG.Analyzers/Analyzers/PointlessOverride/PointlessOverrideAnalyzer.cs +++ b/WTG.Analyzers/Analyzers/PointlessOverride/PointlessOverrideAnalyzer.cs @@ -94,6 +94,11 @@ sealed class IsMeaningfulVisitor : CSharpSyntaxVisitor public override bool VisitMethodDeclaration(MethodDeclarationSyntax node) { + if (node.AttributeLists.Any()) + { + return true; + } + var expression = node.Accept(SolitaryExpressionLocator.Instance); if (expression == null || !expression.IsKind(SyntaxKind.InvocationExpression)) @@ -117,6 +122,11 @@ public override bool VisitMethodDeclaration(MethodDeclarationSyntax node) public override bool VisitPropertyDeclaration(PropertyDeclarationSyntax node) { + if (node.AttributeLists.Any()) + { + return true; + } + if (node.ExpressionBody != null) { return !IsMatchingSelf(node.ExpressionBody.Accept(SolitaryExpressionLocator.Instance)); @@ -175,6 +185,11 @@ bool IsMatchingSelf(ExpressionSyntax? expression) public override bool VisitIndexerDeclaration(IndexerDeclarationSyntax node) { + if (node.AttributeLists.Any()) + { + return true; + } + if (node.ExpressionBody != null) { return !IsMatchingSelf(node.ExpressionBody.Accept(SolitaryExpressionLocator.Instance)); @@ -234,6 +249,11 @@ bool IsMatchingSelf(ExpressionSyntax? expression) public override bool VisitEventDeclaration(EventDeclarationSyntax node) { + if (node.AttributeLists.Any()) + { + return true; + } + foreach (var accessor in node.AccessorList.Accessors) { switch (accessor.Kind())