107

Can I configure ReSharper to fix C# code when curly braces are not used to surround a single-line code block, like this:

if (blnSomeCondition)
   DoSomething();  // complain

if (blnSomeOtherCondition)
{
   DoSomethingElse(); // don't complain
}

Thanks

7
  • 3
    Good question, but what's wrong with the first syntax? Curl braces are unnecessary.
    – Micah
    Sep 1, 2010 at 21:31
  • 48
    Some developers argue that it's better practice to include curly braces around a single-line code block since it can be more readable and more maintainable. Sep 1, 2010 at 22:27
  • 34
    In addition, I've seen how many times not using curling braces has led to bugs. Someone makes a change, sees the if and adds a new line, not realizing it won't execute as part of the conditional. It's subtle but has happened. Sep 2, 2010 at 8:17
  • 2
    I don't really have a problem with the first format, but I can see that it could lead to bugs in the scenario that Hadi mentioned. Our team has decided that it's a coding standard that we wish to enforce, which is why I ask the question.
    – Booberry
    Sep 3, 2010 at 13:17
  • 6
    My personal preference is to not use the braces for single line 'if's, but to place the 'if' and the following statement onto the same physical line, e.g. 'if (blnSomeCondition) Do Something();'.
    – Paul Ruane
    Sep 3, 2010 at 13:33

7 Answers 7

164

In the new version of ReSharper (2016.x) is has been moved to the Code Style. ReSharper 2016.1.1

UPD1: for ReSharper 2017.x ReSharper 2017.1.2

UPD2: for ReSharper 2018.x ReSharper 2018.1

UPD3: for ReSharper 2019.x ReSharper 2019.1

UPD4: for ReSharper 2020.x enter image description here

35

Ryan is correct (note however that the his link refers to R# 2.0 help). The specific procedure is as follows:

  1. Go to ReSharper > Options > Languages > C# > Formatting Style > Braces Layout
  2. Set "Braces in "if-else" statement" to "Use braces for multiline"
  3. After saving the changes, select a scope to reformat (could be a code selection, file, folder, project, solution - anything you want).
  4. Choose ReSharper > Tools > Cleanup Code.
  5. Profit.

Remember that Code Cleanup does numerous things and they're not only related to code formatting (see details at http://www.jetbrains.com/resharper/webhelp/Code_Cleanup__Index.html), so use the feature wisely.

0
28

Gorohoroh's solution is close, but instead of selecting "Use braces for multiline" I had to select "Add braces" to force it to add the braces in the single-line scenario. And I had to set that in all six dropdowns under "Force Braces" to catch all the scenarios:

alt text

What I was really hoping for was to be able to set up ReSharper to do a yellow warning or red error when the rule was violated, but I haven't found a way to do that. Because you're right, Gorohoroh, Cleanup Code does a lot of stuff and I'd like to have the option to look at and fix the rule violations one at a time.

Thanks for your help!

0
8

For ReSharper 2016.2.2.

You should edit your profile for cleaning up. The proper item is called Add/Remove braces for single statements in "if-else", "for", "foreach", "while", "do-while", "using". This item can be found within the C# => Code styles item.

If the item is checked, braces will be added; if unchecked, braces will be removed.

Note: adding doesn't work now (don't know why - perhaps, too fresh version of ReSharper). However, if you will uncheck the mentioned item, the existent braces around single statements will not be removed.

UPD: The problem solved, see the first two comments under this post.

2
  • 2
    This works in 2016.3.2 a little differently - under Options, go to Code Editing --> C# --> Code styles --> Braces --> In "If" statement, and then pick "Enforce always" under Preference. Other options are "Do not enforce", "Enforce if body is multi-line", "Enforce if statement is multi-line", etc.
    – Chait
    Feb 15, 2017 at 21:21
  • Nice. Thank you! And after these settings, the cleaning up described by me works properly.
    – Jordan
    Feb 16, 2017 at 17:16
2

You can configure it as seen on the screenshot.

force curly brackets for a single line of code

PS: for the ones to have problem seeing Imgur.com urls -> https://ibb.co/b4xijT

1

Just if it can help somebody else to save time, on one machine of my company, following Sergey advices was not enough despite the great quality of his post.

On concerned setup (VS2017, R# 2019, both up to date) these settings on R# side were already good, but I also had to modify it in Visual Studio options :

  • In Options window, go to Text editor -> C# -> Code style -> Formatting -> General,
  • Check "Perform Additional code cleanup during formatting" and "Add/remove braces for single-line control statement"
0

Well it is moved again. The new place is under the Syntax Style on version ReSharper 2020.01.

Go to ReSharper > Options > Code Editing > C# > Syntax Style > Braces

Here is the screen shot from ReSharper 2020.01

enter image description here

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.