Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RCS1031: False positive on using + return in switch-statement #632

Closed
Debbus72 opened this issue Jan 23, 2020 · 0 comments
Closed

RCS1031: False positive on using + return in switch-statement #632

Debbus72 opened this issue Jan 23, 2020 · 0 comments

Comments

@Debbus72
Copy link

Based on code in our application, I have created the following example:

public static void Main(string[] args)
{
   switch (args[0])
   {
        default:
            { //RCS1031
                using var fs = new FileStream(@"c:\test.txt", FileMode.Open);
                return;
            }
    }
}

The following things happen in this example:

  • We use the new using var ...
  • We return directly from the case/default.
    Because of using var C# forces us to use the braces around the whole case-statement. Omitting the braces results in the error "CS8647 A using variable cannot be used directly within a switch section (consider using braces)." However, the rule "RCS1031- Remove unnecessary braces" is showing on those braces.

We have work-around for this, like not do a return but a break (which is just below the closing-brace). The working example (which does not show the RCS1031 rule) would be:

  default:
  {
      using var fs = new FileStream(@"c:\test2.txt", FileMode.Open);
  }
  break;

For me this is a real edge-case and frankly I quickly rewrote this to not do the return inside the switch. So see if you want/can to fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants