Search code examples
visual-studio-2017resharper

How to stop resharper removing empty switch default case


Assume I've got the following code:

    public static void PrintFoo(int i)
    {
        switch (i)
        {
            case 0:
                Console.WriteLine("bar!");
                break;
            case 1:
                Console.WriteLine("baz!");
                break;
            default:
                // do nothing
                break;
        }
    }

I want the "default" switch case there with the comment as it shows I'm deliberately not processing any values other than 0 and 1. If I leave out the default case, it's not clear if I meant to do nothing, or just forgot. Indeed, if I delete the default case, I get errors from "IDE0010 Populate switch" showing up in the errors window.

By default Resharper considers this an error, so I have turned off that inspection (Resharper options -> Inspection Severity -> C# -> Redundancies in Code -> Redundant empty switch section).

The problem I have is that when I run code cleanup, it deletes the default case, including the comment. In general I still want code cleanup to fix all the other redundancies in the file, so turning off "Remove code redundancies" in the code cleanup profile isn't an option. Is there a way to get it not to remove the default case in the switch statement?

EDIT: It seems that the default case is only removed if, in the code cleanup window, I select to "Remove code redundancies" and any child of "Code style". If I deselect all the code style items, the default case is not removed, or if I deselect the code redundancies it is not deleted either. Looks like I might have to raise this as a bug with Resharper.


Solution

  • So I reported this to jetbrains who looked into the problem. It turned out that the setting for "Redundant empty switch section" was stored in the "Team shared" settings layer and the code cleanup was pulling the setting from the wrong layer.

    The solution was to go to Resharper -> Manage options -> double click on "this computer" and change the setting there.

    Thanks to Alexander Kurakin for solving the issue.