ReSharper - forces braces around one line

Can I configure ReSharper to fix C # code when curly braces are not used to surround single-line code, for example:

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

thank

+86
c # resharper
Sep 01 '10 at 21:19
source share
5 answers

Gorohoroh's solution is close, but instead of choosing “Use curly braces for multi-line”, I had to select “Add curly braces” to force him to add curly braces in a single-line scenario. And I had to set this in all six drop-down menus under “Force Braces” to catch all the scenarios:

alt text

What I really hoped for was to set ReSharper to a yellow warning or a red error when this rule was broken, but I did not find a way to do this. Since you're right, Ghorhoroh, Cleanup Code does a lot of things, and I would like to be able to watch and fix rule violations one at a time.

Thanks for your help!

+28
Sep 03 '10 at 13:28
source share

In the new version of ReSharper (2016.x), the code style has been ported. 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

+125
May 7 '16 at 16:29
source share

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

  • Go to the ReSharper tab> Options> Languages> C #> Formatting Style> Bracket Plan
  • Set "Braces in the if-else" statement "to" Use curly braces for multi-line "
  • After saving the changes, select the area for reformatting (there may be a choice of code, file, folder, project, solution - everything you want).
  • Select ReSharper> Tools> Cleanup Code.
  • Profit

Remember that Code Cleanup does a lot of things, and it’s not just about formatting the code (see http://www.jetbrains.com/resharper/webhelp/Code_Cleanup__Index.html for details), so use the function wisely.

+33
Sep 01 2018-10-10T00:
source share

For ReSharper 2016.2.2 .

You must edit your profile to clear it. The corresponding element is called Add / remove curly braces for individual statements in "if-else", "for", "foreach", "while", "do-while", "using" . This element can be found in the C # => Code Style element.

If an item is checked, brackets will be added; if they are not marked, the brackets will be deleted.

Note: the addition does not work now (I don’t know why - perhaps the latest version of ReSharper is too recent). However, if you remove the specified item, existing braces around individual statements will not be deleted.

UPD: the problem is resolved, see the first two comments in this post.

+8
Oct. 16 '16 at 20:08
source share

You can customize it as shown in the screenshot.

force curly brackets for a single line of code

PS: for those who have problems viewing the URL Imgur.com → https://ibb.co/b4xijT

+1
Jul 26 '18 at 8:09
source share



All Articles