Skip to content

avoid_commented_out_code

v0.3.0WarningFixConfigurableCode Quality

Flags comments that look like commented-out Dart code rather than descriptive text. This includes commented-out function definitions, variable declarations, import statements, and other recognizable code patterns. The quick fix removes the flagged comment block.

Commented-out code is technical debt that clutters the codebase and confuses readers about what is intentional. Version control already preserves old code, making commented-out blocks unnecessary. Removing them keeps the codebase clean and reduces cognitive load during code review.

See also: Effective Dart: Documentation

class BadExamples {
// void apply(String value) {
// print(value);
// }
// final x = 42;
// import 'dart:async';
void another() {}
}
class GoodExamples {
// This method handles the main processing logic
// and delegates to the appropriate handler
// Temporarily disabled, enable in 1.0
void another() {}
}

Detection is a heuristic over each block of comments: a block is reported when at least half its non-empty lines look like Dart rather than prose. Blocks are formed from comments on directly consecutive lines — a blank line ends one, and so does any code between them. A comment trailing code (foo(); // note) is its own block, since it annotates the line beside it.

That means a single commented-out line surrounded by explanatory prose may not reach the ratio, and prose that reads like code (// returns null;) can be reported. Suppress those with // ignore: many_lints/avoid_commented_out_code.

This rule is in the opinionated preset, so it is on with preset: opinionated, or by name:

many_lints.yaml
rules:
avoid_commented_out_code: true

To turn it off again:

many_lints.yaml
rules:
avoid_commented_out_code: false

To keep the rule on but skip certain paths, use per-rule exclude.

analysis_options.yaml
many_lints:
rules:
avoid_commented_out_code:
min_lines: 2
Option Type Default Description
min_lines int 1 Minimum number of consecutive commented-out lines before a block is reported