From 2ff5497325ff3d8fa00f7297aaf05611cbc56887 Mon Sep 17 00:00:00 2001 From: Yaakov Smith Date: Thu, 20 May 2021 14:52:24 +1000 Subject: [PATCH 1/4] Add test for subclass adding attributes --- .../Attributes/Source.cs | 19 +++++++++++++++++++ 1 file changed, 19 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..e908f56d --- /dev/null +++ b/WTG.Analyzers.Test/TestData/PointlessOverrideAnalyzer/Attributes/Source.cs @@ -0,0 +1,19 @@ +using System; +using System.ComponentModel; + +namespace Magic +{ + class DerivedClass : BaseClass + { + [Obsolete("Please don't use this anymore")] + [EditorBrowsable(EditorBrowsableState.Never)] + public override void Method(int argument1) => base.Method(argument1); + } + + class BaseClass + { + public virtual void Method(int argument1) + { + } + } +} From 3d71655aa2777a352fea9f103a447553f8ed01fb Mon Sep 17 00:00:00 2001 From: Yaakov Smith Date: Thu, 20 May 2021 14:55:06 +1000 Subject: [PATCH 2/4] Add tests for property and event --- .../Attributes/Source.cs | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/WTG.Analyzers.Test/TestData/PointlessOverrideAnalyzer/Attributes/Source.cs b/WTG.Analyzers.Test/TestData/PointlessOverrideAnalyzer/Attributes/Source.cs index e908f56d..462f272a 100644 --- a/WTG.Analyzers.Test/TestData/PointlessOverrideAnalyzer/Attributes/Source.cs +++ b/WTG.Analyzers.Test/TestData/PointlessOverrideAnalyzer/Attributes/Source.cs @@ -6,8 +6,21 @@ namespace Magic class DerivedClass : BaseClass { [Obsolete("Please don't use this anymore")] - [EditorBrowsable(EditorBrowsableState.Never)] 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; + } } class BaseClass @@ -15,5 +28,13 @@ class BaseClass public virtual void Method(int argument1) { } + + public virtual object Property { get; set; } + + public virtual event EventHandler Event + { + add { } + remove { } + } } } From 616af023be121d4c2cd5f2ce3e3b0f5d486f5ceb Mon Sep 17 00:00:00 2001 From: Yaakov Smith Date: Thu, 20 May 2021 14:58:11 +1000 Subject: [PATCH 3/4] add Indexer test --- .../PointlessOverrideAnalyzer/Attributes/Source.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/WTG.Analyzers.Test/TestData/PointlessOverrideAnalyzer/Attributes/Source.cs b/WTG.Analyzers.Test/TestData/PointlessOverrideAnalyzer/Attributes/Source.cs index 462f272a..dc9aad52 100644 --- a/WTG.Analyzers.Test/TestData/PointlessOverrideAnalyzer/Attributes/Source.cs +++ b/WTG.Analyzers.Test/TestData/PointlessOverrideAnalyzer/Attributes/Source.cs @@ -21,6 +21,13 @@ 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 @@ -36,5 +43,11 @@ public virtual event EventHandler Event add { } remove { } } + + public virtual object this[int index] + { + get => null; + set { } + } } } From 0d837fb9d5e238339d80bf9fa428a336668073b1 Mon Sep 17 00:00:00 2001 From: Yaakov Smith Date: Thu, 20 May 2021 14:58:30 +1000 Subject: [PATCH 4/4] Overrides that declare attributes should not be considered pointless. --- .../PointlessOverrideAnalyzer.cs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) 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())