Different brace behaviour for single line if

Greetings, I'm attempting to make my resharper do the following when formatting C# code:

if(something) return true;

if(somethingelse) {
     // do things
} else {
     // do other things
     // more than one line
}


However under Formatting Style -> Braces Layout there's only an option for Braces in "if-else" statement. If i set this too add braces I get this:

if(something) {
     return true;
}

if(somethingelse) {
     // do things
} else {
     // do other things
     // more than one line
}


And when set to use braces for multiline this is the result:

if(something) return true;

if(somethingelse) // do things
else {
     // do other things
     // more than one line
}



Is there an option that makes resharper not add braces only when it is a single line if statement without an else/else if?

1 comment
Comment actions Permalink

Yes, in R# 2016.
Look under Code Style > Braces > In "if" statement > Enforce if statement is multi-line.

// No braces - just when single-line statement
if (something) return true;

// Otherwise braces - due to else statement or multi-line body
if (something) {
return true;
} else {
return false;
}

if (something) {
something = false;
  return true;
}

Other choices include:

  • Enforce if body is multi-line: add braces only to individual multi-line bodies.
  • Enforce if any part requires braces: add braces to all bodies if one body is multi-line.

Note: result is also affected by Formatting Style > Line Breaks and Wrapping > Preserve Existing Formatting > Break line in single embedded statement, and Break line in a block with a single statement.

0

Please sign in to leave a comment.