0

We found the following bug in our code as a result of a git merge.

public static void error(ref string test)
{
    var diff = 123 - new Random().Next();
    if (diff == 0)
        test = $"diff == 0";
    {
        return;
    }
    
    //This will never execute
    test = $"diff != 0";
}

As you can see the missing braces has caused a block statement with a return in it. Code analysis reports the code as unreachable which would alert most people. A unit test would also pick this up. However the issue is a block statement is rarely ever the programmers intention and during the git merge this error is not immediately obvious (due to no code analysis running in most merge windows).

enter image description here

Is there anyway to make this (the block statement, not the unreachable code) a compile error/warning via custom analysis rules in C# via Jetbrains Rider or Visual Studio?

2
  • So do you want to change the compiler warning "Code is unreachable" into a compiler error? Commented Nov 8, 2022 at 1:54
  • Maybe this can help stackoverflow.com/questions/2520853/… Commented Nov 8, 2022 at 2:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.