Skip to content

BRO1520: Place the operator consistently when an expression wraps

When the line breaks next to a binary operator (&&, ||, +, ??, ==, ...) or the conditional operator's ? or :, the operator goes at the beginning of the continuation line (the default) or at the end of the broken line. Not a StyleCop rule; it follows the SDK's dotnet_style_operator_placement_when_wrapping, which no SDK rule enforces.

Example

Before

public class Order
{
    public bool IsReady(bool paid, bool packed, bool cancelled) =>
        paid &&
            packed &&
            !cancelled;

    public string Label(bool express, string name) =>
        express ?
            name + " (express)" :
            name;
}

After

public class Order
{
    public bool IsReady(bool paid, bool packed, bool cancelled) =>
        paid
            && packed
            && !cancelled;

    public string Label(bool express, string name) =>
        express
            ? name + " (express)"
            : name;
}

Why

With the operator first, the start of each continuation line says how it joins the expression; a reader scanning the left edge sees && against || without reading to the end of the line above. It's also the SDK's default for the setting, and what the "wrap expression" refactorings in Visual Studio produce.

How the fix works

Only the operator moves: it goes to the other side of the line break with one space between it and the code on its line. The line break, blank lines and the continuation line's indentation stay as they are, and trailing whitespace on the broken line goes.

Configuration

[*.cs]
# The SDK's setting: beginning_of_line (default) or end_of_line.
dotnet_style_operator_placement_when_wrapping = beginning_of_line

The SDK documents the key for binary operators (its own wrapping refactorings use it there); StyleBro applies it to the conditional operator's ? and : as well, so one key decides where every operator of a wrapped expression goes (the owner's decision).

Not reported

  • A comment or a directive next to the operator (a && // why, #if between the operands): moving the operator would move it across the comment or out of the #if branch.
  • An operator alone on its line (line breaks on both sides): neither placement applies.
  • A query right after the operator (... ?? + from x in ...): the query layout rules (BRO1127-BRO1130) indent its clauses by whether from starts its line, so they would re-indent the query.
  • Pattern combinators (and, or in patterns) and the = of assignments (BRO1522).
  • Code with syntax errors around the operator.

Compared with StyleCop

StyleCop has no such rule. Roslynator's RCS0027 (binary operators) and RCS0028 (?/:) do the same, off by default and with their own option (roslynator_binary_operator_new_line, roslynator_conditional_operator_new_line). stylebro-migrate turns this rule off, so migrating doesn't change a StyleCop-clean repository.