ReSharper - force curly braces around single line

Viewed 24947

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 Answers

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.

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

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"

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

Related