else clause with an empty body
ID |
dart.correctness.avoid_empty_else |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Suspicious Construct |
Language |
Dart |
Description
Reports an else clause whose body is an empty block or an empty statement, so the clause does nothing at all.
Rationale
An else that does nothing is either leftover scaffolding or a real defect. The dangerous form is else ;, where the semicolon is the whole clause and the block that follows it looks like the else branch but always runs, whatever the condition was — a misreading the layout actively encourages. An empty else { } is less harmful but still misleading: a reader has to decide whether the branch was forgotten, and every later edit inherits that doubt.
The following code illustrates the pattern detected by this rule:
class Router {
void route(bool enabled, String path) {
// FLAGGED: else clause with an empty body
if (enabled) {
dispatch(path);
} else {
}
}
void dispatch(String path) {}
}