dotnet / dotnet/roslynator

Analyzer suggestion: in overridden virtual method detect call to different base method

Open
#711 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
3.5k
Forks
294
Avg merge
2h 30m
Merged PRs (30d)
4

Description

Consider the following code sample:
```csharp
public class A
{
public virtual void DoX() { }
public virtual void DoY() { }
}

public class B : A
{
public override void DoX()
{
base.DoY(); // this has a code smell, calling a different base method
}
}
```

In an overridden method it is weird if you call a different method using the `base.` prefix. When copy pasting methods with similar content though, it is an easy error to make. Especially when the method signatures and content are highly similar.

Things I would think the analyzer would need to consider:

1. The method to by analyzed needs to be an override
2. The method it overrides needs to be virtual
3. The method contains a call to a it's base class (`base.` prefix) however it calls a different method than the virtual it overrides. The prefix will nearly often be there in the smelly situation, because you can't call the overriden method in any other way. If you just wanted to call the other method, you'd probably omit the prefix.

This combination of criteria would be the way to find this unusual and possibly buggy code.

Contributor guide

Open the contributing guide

Research direction

No file or test is named in the issue. Start by locating existing C# analyzer implementations and tests in Roslynator, then inspect how overridden methods and base-member invocations are represented; done means the shown DoX/base.DoY case is reported without flagging a call to the overridden method itself.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
devtools
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.