avoid_commented_out_code
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.
Why use this rule
Section titled “Why use this rule”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() {}}Known limitations
Section titled “Known limitations”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.
Turning this rule off
Section titled “Turning this rule off”This rule is in the opinionated preset, so it is on with
preset: opinionated, or by name:
rules: avoid_commented_out_code: trueTo turn it off again:
rules: avoid_commented_out_code: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Options
Section titled “Options”many_lints: rules: avoid_commented_out_code: min_lines: 2rules: 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 |
Related rules
Section titled “Related rules”avoid_accessing_other_classes_private_members— Make the underscore mean what everyone reads it as.avoid_complex_conditions— Keep boolean conditions within an operand budget.avoid_deep_nesting— Keep control flow within a nesting budget.avoid_default_tostring— Don’t interpolate objects that don’t override toString.