curly
What it does
Section titled “What it does”Reports the body of an if, else, loop or when entry that is one statement without braces (if (a) b();, else c();, for (;;) d();, while (x) y();). else if is not reported. Style always (default) wants braces on every body; style chain wants them on the branches of a chain only where one branch has braces, spans lines or passes the line width, and on a loop body that stands on its own line. The language’s curly_style option overrides the style when the formatter runs the rule. The fix adds the braces.
Why it matters
Section titled “Why it matters”Without braces a second line added later to the body looks as if it belongs to the if or loop, but it runs every time. That is how the 2014 Apple goto fail bug happened, and braces make the extent of the body visible.
To fix, add the braces; the fix does it.
Example
Section titled “Example”class A { void f(boolean x, int[] xs) { if (x) return; if (x) { g(); } else g(); if (x) {... } else { g(); } for (int i = 0; i < 3; i++) g(); for (int v : xs) g(); while (x) g(); do g(); while (x); for (int v : xs) { g(); }...class A { void f(boolean x, int[] xs) { if (x) { return; } if (x) { g(); } else { g(); } if (x) {... } else { g(); } for (int i = 0; i < 3; i++) { g(); } for (int v : xs) { g(); } while (x) { g(); } do { g(); } while (x); for (int v : xs) { g(); }...Configuration
Section titled “Configuration”lint: rules: curly: level: warn # off (default), warn, error options: style: "always"| Option | Type | Default | Description |
|---|---|---|---|
style |
string | "always" |
always: every body; chain: see the rule. The style row of the language’s curly_style option overrides it in the formatter.Values: always, chain |
Enabled by the presets: java/google, kotlin/ktlint.
Suppress one line with sudsy-disable-next-line curly in a comment of the file’s language.
Upstream equivalents
Section titled “Upstream equivalents”The same check in other tools. full: Sudsy reports what the upstream rule reports; partial: only part of it, as the last column says.
| Tool | Rule | Match | What differs |
|---|---|---|---|
| checkstyle | NeedBraces |
partial | No option for single-line bodies. |