Our website uses cookies to enhance your browsing experience.
Accept
to the top

Webinar: Let's make a programming language. Lexer - 29.04

>
>
>
V3013. It is odd that the body of...
menu mobile close menu
Additional information
toggle menu Contents

V3013. It is odd that the body of 'Foo_1' function is fully equivalent to the body of 'Foo_2' function.

Nov 10 2015

The analyzer has detected two methods with identical implementations. Although having two identical methods is not an error, they should still be reviewed.

The diagnostic rule detects the following error types:

class Bound
{
  ....
  float GetLeft() { return _left; }
  float GetRight() { return _left; }
};

A typo causes two logically different methods to perform the same actions. The fixed code:

float GetLeft() { return _left; }
float GetRight() { return _right; }

In the example, the identical bodies of the GetLeft() and GetRight() methods indicate an error. However, issuing warnings for all identical methods could result in many false positives. Therefore, the analyzer has a number of exceptions where a warning about identical method bodies is not required.

Some examples of exceptions:

  • the analyzer does not report about identical methods' bodies if they do not use variables except for arguments. For example: bool IsXYZ() { return true; };
  • methods with identical bodies are repeated more than twice;
  • methods' bodies consist of only the throw() statement;
  • and so on.

There are a number of ways to handle the false positives:

  • If false-positive warnings relate to the files of external libraries or tests, add the path to these files or folders to the exception list.
  • If false-positive warnings refer to your own code, add the //-V3013 comment to suppress them.
  • If there are too many false positives, completely disable this diagnostic rule in the analyzer's settings.
  • Modify the code to make one method call another with the same code.

The real-world code example where two methods designed for different operations are implemented in the same way:

public void Pause(FrameworkElement target)
{
  if (Storyboard != null)
  {
    Storyboard.Pause(target);
  }
}

public void Stop(FrameworkElement target)
{
  if (Storyboard != null)
  {
    Storyboard.Stop(target);
  }
}

public void Resume(FrameworkElement target)
{
  if (Storyboard != null)
  {
    Storyboard.Pause(target);
  }
}

After copying the Pause method twice, the developers forgot to update the body of the last method, Resume.

The fixed code:

public void Resume(FrameworkElement target)
{
  if (Storyboard != null)
  {
    Storyboard.Resume(target);
  }
}

You can look at examples of errors detected by the V3013 diagnostic.