noop-primitive-operations
Same as noop_primitive_operations in Dart lints
- Group
- Performance
- Path
lint/performance/noop-primitive-operations- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 300 of 512
Flags primitive operations that return their receiver unchanged.
Some calls are no-ops on their receiver's type: toInt(), round(),
ceil(), floor() and truncate() on an int, toDouble() on a double,
and toString() on a non-nullable String. Likewise toString() is
redundant on an interpolated expression ('${x.toString()}') or the sole
argument of print (also through a dart:core import prefix), which both
stringify their operand already, and an empty
string literal between two adjacent string literals adds nothing. The rule
reports the redundant method name, or the empty literal. A parenthesized
method call inside print or an interpolation is left alone.
Invalid
void ints(int i) {
i.toInt();
i.round();
i.ceil();
i.floor();
i.truncate();
}
void doubles(double d) {
d.toDouble();
}
void strings(String s, Object o) {
s.toString();
print('value: ${o.toString()}');
print(o.toString());
final joined = 'a' '' 'b';
print(joined);
}
class Stepper {
Stepper(this.value);
final double? value;
static const double _min = 0;
static const double _max = 24;
// `??` and `clamp` keep the operands' `double` type.
void step() {
final current = value ?? _min;
print(current.clamp(_min, _max).toDouble());
print((current + 0.5).toDouble());
}
}
void clamps(int i, double d) {
i.clamp(0, 1).toInt();
d.clamp(0.0, 1.0).toDouble();
}void ints(int i) { i.toInt(); i.toInt(); i.round(); i.round(); i.ceil(); i.ceil(); i.floor(); i.floor(); i.truncate();void doubles(double d) { d.toDouble();void strings(String s, Object o) { s.toString(); s.toString(); print('value: ${o.toString()}'); print('value: ${o.toString()}'); print(o.toString()); print(o.toString()); final joined = 'a' '' 'b'; final current = value ?? _min; print(current.clamp(_min, _max).toDouble()); print(current.clamp(_min, _max).toDouble()); print((current + 0.5).toDouble());void clamps(int i, double d) { i.clamp(0, 1).toInt(); i.clamp(0, 1).toInt(); d.clamp(0.0, 1.0).toDouble();Valid
class Base {
@override
String toString() => 'base';
}
class Child extends Base {
@override
String toString() => '${super.toString()}!';
}
void numbers(double d, num n, int? maybe) {
d.toInt();
d.round();
n.toInt();
n.toDouble();
(1.5).floor();
}
void strings(String? s, Object o) {
s?.toString();
print('value: $o');
print(o);
print(o.toString().length);
print('$s'.length);
final edges = '' 'a' 'b' '';
print(edges);
debugPrint(o.toString());
}
void debugPrint(String message) {}
// Mixed `int` and `double` operands make `clamp` return a `num`.
void mixedClamp(double d, int? maybe) {
d.clamp(0, 1).toDouble();
(maybe ?? 1.5).toDouble();
}Configure noop-primitive-operations
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"performance": {
"noop-primitive-operations": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/performance/noop-primitive-operations: <reason>More performance rules
- prefer-const-border-radius
prefer-const-border-radiusrecommendedflutterFlags
BorderRadius.only(...)whose four corner radii are all equal. - prefer-contains
prefer_containsrecommendedPrefers
containsto comparing the result ofindexOfwith a constant. - prefer-correct-edge-insets-constructor
prefer-correct-edge-insets-constructorrecommendedflutterFlags an
EdgeInsets.only(...)that a more specific constructor expresses better. - prefer-declaring-const-constructor
prefer_declaring_const_constructorflutterFlags a generative constructor that is legally eligible to be
const. - prefer-foreach
prefer_foreachPrefers
forEachto afor-in loop that only passes each element to a function. - prefer-return-await
Await returned SDK Futures inside a try body protected by a catch clause.
- prefer-single-setstateflutter
Merge consecutive Flutter State.setState statements in the same block.
- prefer-use-callbackflutter_hooks
Prefer
useCallbackfor synchronous memoized function literals.