no-empty-block
Same as no-empty-block in Dart Code Metrics
- Group
- Suspicious
- Path
lint/suspicious/no-empty-block- Default
- Off, opt in
- Origin
- Dart Code Metrics
- Rule
- 284 of 512
Flags empty blocks ({}).
An empty function, method, loop, or switch-case body is frequently an unfinished implementation or a leftover after code was deleted, and it hides whether the emptiness is deliberate. Fill in the intended behavior, or, if a no-op is genuinely correct, add a comment saying so. A block containing only a comment is treated as intentional and left alone. The report lands on the closing brace.
Invalid
// Test cases for no-empty-block rule
// All violations are marked inline below.
void testEmptyCatchBlock() {
try {
riskyOperation();
} catch (e) {
}
}
void testEmptyIfBlock() {
if (condition) {
}
}
void testEmptyElseBlock() {
if (condition) {
print("yes");
} else {
}
}
void testEmptyMethodBody() {
onPressed() {
}
}
class Widget {
void onTap() {
}
void onLongPress() {
}
}
void testEmptyForLoop() {
for (int i = 0; i < 10; i++) {
}
}
void testEmptyWhileLoop() {
while (condition) {
}
}
void testEmptyFinallyBlock() {
try {
doSomething();
} finally {
}
}
void testMultipleEmptyBlocks() {
if (a) {
}
if (b) {
}
}
void testEmptyOnBlock(Future<String> future) {
future.then((_) {
});
} } catch (e) { } if (condition) { } } else { } onPressed() { } void onTap() { } void onLongPress() { } for (int i = 0; i < 10; i++) { } while (condition) { } } finally { } if (a) { } if (b) { } future.then((_) { });Valid
// Good cases for no-empty-block rule
// No violations expected
void testCatchBlockWithHandler() {
try {
riskyOperation();
} catch (e) {
logger.error('Operation failed: $e');
}
}
void testCatchBlockWithRethrow() {
try {
riskyOperation();
} catch (e) {
rethrow;
}
}
void testIfBlockWithBody() {
if (condition) {
print("yes");
}
}
void testElseBlockWithBody() {
if (condition) {
print("yes");
} else {
print("no");
}
}
void testMethodWithBody() {
onPressed() {
print("pressed");
}
}
class Widget {
void onTap() {
handleTap();
}
void onLongPress() {
// Allow long press without action for now
handleLongPress();
}
}
void testForLoopWithBody() {
for (int i = 0; i < 10; i++) {
print(i);
}
}
void testWhileLoopWithBody() {
while (condition) {
process();
condition = evaluate();
}
}
void testFinallyBlockWithBody() {
try {
doSomething();
} finally {
cleanup();
}
}
void testIfBlockWithComment() {
if (impossible) {
// This condition should never be true in practice
throw AssertionError('Impossible state reached');
}
}
void testCatchWithComment() {
try {
parse();
} catch (e) {
// Intentionally swallow parse errors in legacy format
}
}
void testAsyncCallback(Future<String> future) {
future.then((value) {
print('Got: $value');
});
}
void testEmptyCallbackWithComment(VoidCallback callback) {
callback = () {
// No-op callback for testing
};
}
class ErrorHandler {
void handleOptional(Error? error) {
if (error == null) {
// No error to handle, silent success
return;
}
log(error);
}
}Configure no-empty-block
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"suspicious": {
"no-empty-block": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/suspicious/no-empty-block: <reason>More suspicious rules
- no-equal-arguments
no-equal-argumentsFlags an argument passed more than once in the same invocation.
- no-equal-then-else
no-equal-then-elserecommendedFlags an
if/elseor ternary whose two branches are identical. - no-magic-string
Extract inline string literals into named variables or constants.
- no-self-comparisons
no_self_comparisonsrecommendedFlags proven self-comparisons of side-effect-free scalar values.
- no-wildcard-variable-uses
no_wildcard_variable_usesrecommendedFlags references to a wildcard variable or parameter (a name made solely of underscores, such as
_or__). - overridden-fields
overridden_fieldsrecommendedFlags instance fields that redeclare an inherited field.
- prefer-unique-test-namestest
Reports duplicate test descriptions within a group and constant names in loops.
- recursive-getters
recursive_gettersrecommendedFlags a getter that unconditionally returns itself.