tags:

views:

157

answers:

3

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

+3  A: 

I believe I found what you're looking for...

http://www.jetbrains.com/resharper/documentation/help20/Options/braceslayout.html

Ryan Berger
+4  A: 

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.

gorohoroh
+1 for additional info
Chris Lively
+2  A: 

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!

Booberry