unnecessary-const
Same as unnecessary_const in Dart lints
- Group
- Complexity
- Path
lint/complexity/unnecessary-const- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 452 of 512
Flags a redundant const keyword inside an already-const context.
Once an expression sits in a constant context — a const collection, the
arguments of a const constructor invocation, a const variable initializer,
or another const expression — every nested const is inferred and the
keyword is just noise; removing it leaves behavior unchanged. The rule
tracks const depth as it descends through const variable, field, and local
declarations and const expressions, flagging any explicit const marker
(on a new, list, map, set, or dot-shorthand) found at depth greater than
zero.
Invalid
// Bad: a `const` keyword used where the context is already constant.
class Point {
final int x;
final int y;
const Point(this.x, this.y);
}
const outerList = const [1, 2, 3];
const outerMap = const {'a': 1};
const outerSet = const {1, 2, 3};
const point = const Point(1, 2);
const nestedList = [const [1, 2]];
const wrapped = [const Point(1, 2)]; const outerList = const [1, 2, 3];const outerList = const [1, 2, 3];const outerMap = const {'a': 1};const outerMap = const {'a': 1};const outerSet = const {1, 2, 3};const outerSet = const {1, 2, 3};const point = const Point(1, 2);const point = const Point(1, 2);const nestedList = [const [1, 2]];const nestedList = [const [1, 2]];const wrapped = [const Point(1, 2)];Valid
// Good: `const` only where it is actually required to establish a constant.
class Point {
final int x;
final int y;
const Point(this.x, this.y);
}
const outerList = [1, 2, 3]; // implicitly const, no keyword needed
const point = Point(1, 2); // implicitly const
const nested = [
[1],
[2],
]; // all implicit
final runtime = const [1, 2, 3]; // final var: `const` needed to make it constant
var mutable = const <int>[1]; // `const` needed (context is not constant)
void f() {
const local = [1, 2, 3]; // implicitly const initializer
print(const [1, 2, 3]); // `const` needed: argument context is not constant
print(local);
}Configure unnecessary-const
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"complexity": {
"unnecessary-const": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/complexity/unnecessary-const: <reason>More complexity rules
- unnecessary-getters-setters
unnecessary_getters_settersrecommendedFlags a trivial getter/setter pair that only reads and writes a private field.
- unnecessary-lambdas
unnecessary_lambdasrecommendedFlags a closure that only forwards its parameters to a function a tear-off could name.
- unnecessary-overrides
unnecessary_overridesrecommendedFlags a member that does nothing but forward to
superwith the same arguments and resolved contract. - widget-tree-depthflutter
Limits the depth of an inline Flutter widget construction tree.
- avoid-classes-with-only-static-members
avoid_classes_with_only_static_membersFlags a class that declares only static members.
- avoid-collapsible-if
Flags an else-free
ifwhose only then-statement is another else-freeif. - avoid-complex-arithmetic-expressions
Reports arithmetic expressions containing too many terms.
- avoid-complex-conditions
Flags boolean expressions with more logical operands than the configured threshold.