no_commented_out_code
Reports a comment that is commented-out code. Delete it, or bring the code back.
- Tier
- advisory
- Scope
- Dart
- Fix
- delete the comment
What it reports
Section titled “What it reports”A comment whose whole text is Dart: statements, declarations, directives or class members that parse without an error. The violation covers the whole comment. A run of // lines that stand alone on consecutive lines is one comment, and so is an end-of-line comment after code (save(); // flush();) and a block comment, /* … */.
int total(List<int> prices) { var sum = 0; // for (final price in prices) { // sum += price; // } return sum;}The three comment lines are one violation, from the first // to the last }.
It does not report:
- prose, or a comment that is code only in part, such as a sentence with a call in it;
- a documentation comment,
///or/** … */, with or without a code sample; - a suppression, and the comment directly above it, which is its justification;
- a web address or a licence header;
- a marker comment,
// TODO: start();,// FIXME(ali): stop();.
Commented-out code is code that nothing checks: the compiler does not read it, no test runs it, and a rename passes it by. An AI coding agent leaves it behind when it comments out what it replaced, or comments out a failing call or assertion to get past an error, which hides a defect behind two slashes. Version control already keeps what was deleted, with the date and the reason.
The rule is advisory because telling code from prose is a judgement on text, and the rule can be wrong both ways.
Example
Section titled “Example”Future<void> submit(Order order) async { // await _validate(order); await _api.send(order); // _analytics.track('order_submitted');}Future<void> submit(Order order) async { await _validate(order); await _api.send(order);}If you keep code out on purpose, say why in the same comment. A comment with a sentence in it is prose with an example, and is not reported:
Future<void> submit(Order order) async { await _validate(order); await _api.send(order); // Tracking comes back when the consent screen ships: // _analytics.track('order_submitted');}The fix, Delete the commented-out code, deletes the comment and nothing else, so no blank line is left where it stood. The editor offers it at each violation, and assay fix applies it across the project in one run (see assay fix). A violation with the fix carries [fix available] in the gate’s message.
No fix is offered where deleting the comment would leave an empty catch or constructor body, which an SDK lint of recommended and strict reports.
Rule parameters
Section titled “Rule parameters”Set under the assay: key of the project’s root analysis_options.yaml.
| Rule parameter | Default | Effect |
|---|---|---|
enabled |
true |
false switches the rule off for the whole project. |
exclude |
[] |
Path globs where the rule is silent. |
assay: rules: no_commented_out_code: exclude: ["lib/generated/**"]Suppressing it
Section titled “Suppressing it”Write why on the line directly above the suppression, which goes directly above the first line of the comment:
// The call comes back with the next release of the scheduler.// ignore: assay/no_commented_out_code// scheduler.start();Neither the suppression nor its justification is judged as a comment. A sentence in the same comment as the code works without a suppression, as the example above shows: the rule then takes the comment for prose.
// ignore_for_file: assay/no_commented_out_code silences it in a whole file, and // ignore: no_commented_out_code without the prefix works too. See Suppressions.
Details
Section titled “Details”All cases
What one comment is
Section titled “What one comment is”A comment is judged as a whole, and is one of three things:
- a run of end-of-line comments that stand alone on consecutive lines;
- an end-of-line comment after code,
save(); // flush();; - a block comment,
/* … */.
Commented-out code often has blank lines in it. A run that is no code by itself and leaves a bracket open is read together with the runs after it, across blank lines, up to the run that closes its last bracket. If together they are code, they are one violation:
// void reset(List<int> values) {// values.clear();
// values.add(0);// }Two pieces of code separated by a blank line, each whole by itself, are two violations.
What it does not report
Section titled “What it does not report”- prose;
- a comment that is code only in part: a sentence with a call in it, or a line of prose above or below code in the same run. Such a comment explains, and the code in it is an example;
- a documentation comment,
///or/** … */, with or without a code sample, and the//lines directly below one; - a suppression, and the comment directly above it, which is its justification, whatever it says;
- a web address, alone or beside other text;
- a licence header;
- a marker comment: one with a line that starts with a word in capitals,
// TODO: start();,// FIXME(ali): stop();, with the code below it in the same comment; - a commented-out comment,
// // start();, an empty comment and a line of marks; - code that is not whole: an expression with no semicolon,
// start(); an argument or a collection element,// style: theme.title,; acase; anelsebranch; a lone bracket. The last line of a call chain,// .toList();, is a statement by itself and is reported; - a statement that only reads or computes a value, such as
// value;or// count == 0;, since prose can read as one; - a block comment whose lines start with a star,
* start();.
What counts as code
Section titled “What counts as code”These are reported:
- prose that is also valid Dart:
// Remove later;reads as a variablelaterof the typeRemove, and// Check(value);as a call. A sentence that does not end with a semicolon is never reported. - a code sample apart from its sentence: a blank line or a line of code between them makes the sample a comment of its own. A sample directly after its sentence, in the same run, is not reported.
- code kept as a reference, such as the original line of a port,
// int t0 = (x & m5) << 2;. A word of explanation in the same comment makes it prose. - code that no longer compiles: the comment is parsed and nothing more, so a name that does not exist or a call with the wrong arguments is still code.
Every comment is judged, also at the very top of the file, and test code like any other code.
Why it matters
Section titled “Why it matters”Commented-out code gets a little more wrong with every change around it, and a reader cannot tell whether it is a note, a plan or a mistake. An agent comments out what it replaced “in case it is needed”: that is clutter. Commenting out a failing call or assertion to get past an error hides a defect.
How the fix works
Section titled “How the fix works”- The comment’s own lines go with their line breaks, so no blank line is left where they stood. If a blank line stands both above and below, the one below goes too.
- Code on the same line stays:
save(); // flush();becomessave();. - Braces that held only the comment are left as
{}:if (ready) {\n // run();\n}becomesif (ready) {}. - The file’s line breaks, carriage returns included, stay as they are.
Before:
int total(List<int> prices) { var sum = 0; // for (final price in prices) { // sum += price; // } return sum;}After:
int total(List<int> prices) { var sum = 0; return sum;}No fix in an empty catch or constructor body. Where the comment stands in a block with no statement, and deleting it would make the block an empty one that an SDK lint of recommended and strict reports, the violation says that no fix is available:
empty_catches(blocking under both): the body of a catch clause,} catch (e) { // print(e); }, alsoon Exception { … }; not one whose exception parameter is_, which the lint leaves alone;empty_constructor_bodies: the body of a constructor,Store() { // load(); }.
This holds whatever else the block holds, a prose comment too. An empty if, loop, function or class body is reported by no lint of the presets, so the fix works there.
White space:
- Where the comment ends the file, the blank lines above it go, and the file ends where its last code line ends.
- Blank lines inside the comment, between runs that are one piece of code, go with it.
- A comment after code goes with the space before it. A block comment before code,
/* flush(); */ save();, goes with the space after it, and the code stays where it was indented. - A comment between two pieces of code on one line is replaced by one space only where the two sides would otherwise join into another token:
int/* final k = 1; */ybecomesint y, and- /* … */ -abecomes- -a, not--a. Otherwise the sides are joined:y/* return n; */-1becomesy-1,f(/* a, */b)becomesf(b). - The
{}holds for the body of a function, a class or a collection literal too. Where one pair of braces holds two commented-out passages,assay fixleaves the line breaks between them, which the formatter then writes as{}.
The fix deletes nothing that runs, only a comment. It deletes nothing where the text at the violation is no longer a comment, as when an editor asks for the fix of a violation older than the text. A violation that a suppression hides is never fixed.