Skip to content

Commit

Permalink
Overrides with additional attributes aren't entirely pointless. (#148)
Browse files Browse the repository at this point in the history
* Add test for subclass adding attributes
* Add tests for property and event
* add Indexer test
* Overrides that declare attributes should not be considered pointless.
  • Loading branch information
yaakov-h authored May 20, 2021
1 parent c0daf0c commit 1d0ea78
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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 { }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ sealed class IsMeaningfulVisitor : CSharpSyntaxVisitor<bool>

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))
Expand All @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 1d0ea78

Please sign in to comment.