noop-primitive-operations

Same as noop_primitive_operations in Dart lints

Path
lint/performance/noop-primitive-operations
Default
On (recommended)
Origin
Dart lints
Rule
300 of 512
recommended

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

example.dartdart
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();
}
The expression has no effect and can be removed.
1void ints(int i) {
2 i.toInt();
The expression has no effect and can be removed.
2 i.toInt();
3 i.round();
The expression has no effect and can be removed.
3 i.round();
4 i.ceil();
The expression has no effect and can be removed.
4 i.ceil();
5 i.floor();
The expression has no effect and can be removed.
5 i.floor();
6 i.truncate();
The expression has no effect and can be removed.
9void doubles(double d) {
10 d.toDouble();
The expression has no effect and can be removed.
13void strings(String s, Object o) {
14 s.toString();
The expression has no effect and can be removed.
14 s.toString();
15 print('value: ${o.toString()}');
The expression has no effect and can be removed.
15 print('value: ${o.toString()}');
16 print(o.toString());
The expression has no effect and can be removed.
16 print(o.toString());
17 final joined = 'a' '' 'b';
The expression has no effect and can be removed.
29 final current = value ?? _min;
30 print(current.clamp(_min, _max).toDouble());
The expression has no effect and can be removed.
30 print(current.clamp(_min, _max).toDouble());
31 print((current + 0.5).toDouble());
The expression has no effect and can be removed.
35void clamps(int i, double d) {
36 i.clamp(0, 1).toInt();
The expression has no effect and can be removed.
36 i.clamp(0, 1).toInt();
37 d.clamp(0.0, 1.0).toDouble();

Valid

example.dartdart
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.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "performance": {
        "noop-primitive-operations": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/performance/noop-primitive-operations: <reason>
  • prefer-const-border-radiusprefer-const-border-radiusrecommendedflutter

    Flags BorderRadius.only(...) whose four corner radii are all equal.

  • prefer-containsprefer_containsrecommended

    Prefers contains to comparing the result of indexOf with a constant.

  • prefer-correct-edge-insets-constructorprefer-correct-edge-insets-constructorrecommendedflutter

    Flags an EdgeInsets.only(...) that a more specific constructor expresses better.

  • prefer-declaring-const-constructorprefer_declaring_const_constructorflutter

    Flags a generative constructor that is legally eligible to be const.

  • prefer-foreachprefer_foreach

    Prefers forEach to a for-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 useCallback for synchronous memoized function literals.