prefer-int-literals

Same as prefer_int_literals in Dart lints

Group
Style
Path
lint/style/prefer-int-literals
Default
Off, opt in
Origin
Dart lints
Rule
374 of 512

Flags a whole-number double literal, such as 1.0, where the context type is double so the int literal 1 means the same value.

Since Dart 2.1 an int literal in a double context is a double constant, so the .0 is noise:

const double myDouble = 8.0;  // flagged
final anotherDouble = myDouble + 7.0e2; // fine: no `double` context
main() { someMethod(6.0); }   // flagged when the parameter is `double`

The literal (optionally negated) is checked only where the replacement keeps its meaning: the initializer of a variable, field, or top-level declared double; an element of a <double>[...] list; a value returned from a function, method, getter, or operator declared to return double; and an argument whose parameter resolves to double, including super and this constructor initializers with instantiated superclass parameters. Parenthesized literals and values outside the 64-bit integer range are left alone.

Invalid

example.dartdart
const double myDouble = 8.0;
double? maybe = 1.0;

class Box {
  final double width = 2.0;
  Box(double size);
  Box.named({required double size});
  double get area => 3.0;
  double scale() {
    return 4.0;
  }
  double operator +(Box other) => 5.0;
}

void takesDouble(double value, {double named = 0}) {}

double negative() => -6.0;

void main() {
  double local = 7.0;
  var list = <double>[1.0, 2.5];
  takesDouble(9.0);
  takesDouble(1, named: 10.0);
  Box(11.0);
  Box.named(size: 12.0);
  double inner() {
    return 1e3;
  }
  for (double i = 0.0; i < 1; i++) {}
  print([local, list, inner]);
}

// Field formals resolve even when their fields follow a named constructor.
class Insets {
  const Insets.fromSTEB(this.start, this.top, this.end, this.bottom);
  final double start, top, end, bottom;
}

class Matrix {
  void setEntry(int row, int column, double value) {}
}

void constructorAndCascadeArguments(Matrix matrix) {
  const Insets.fromSTEB(12.0, 4.0, 12.0, 4.0);
  matrix
    ..setEntry(3, 2, -1.0)
    ..setEntry(0, 0, 2.0);
}

// Super/redirect initializer arguments use the same resolved contexts as calls.
class NumericBase {
  final double pressure, elevation;
  const NumericBase({required this.pressure, this.elevation = 0});
  const NumericBase.named(this.pressure, {this.elevation = 0});
}

class NumericChild extends NumericBase {
  const NumericChild() : super(pressure: 0.0, elevation: 8.0);
  const NumericChild.named() : super.named(-2.0, elevation: 4.0);
  const NumericChild.value(double value) : super(pressure: value);
  const NumericChild.redirect() : this.value(9.0);
}

class GenericNumericBase<T> {
  final T value;
  const GenericNumericBase(this.value);
  const GenericNumericBase.named({required this.value});
}

class GenericNumericChild extends GenericNumericBase<double> {
  const GenericNumericChild() : super(5.0);
  const GenericNumericChild.named() : super.named(value: 6.0);
}

class InitializedNumericChild extends NumericBase {
  final int marker;
  const InitializedNumericChild() : marker = 0, super(pressure: 10.0);
}

// Generic function-valued getters infer the result of callback tear-offs.
class Stub<T> {
  void thenReturn(T value) {}
}

Stub<T> Function<T>(T Function()) get when => throw 0;

class RandomValue {
  double nextDouble() => 0;
}

void genericGetterInvocation(RandomValue random) {
  when(random.nextDouble).thenReturn(0.0);
  when(random.nextDouble).thenReturn(1.0);
}
Use an integer literal instead of this unnecessary `double` literal.
1const double myDouble = 8.0;
Use an integer literal instead of this unnecessary `double` literal.
1const double myDouble = 8.0;
2double? maybe = 1.0;
Use an integer literal instead of this unnecessary `double` literal.
4class Box {
5 final double width = 2.0;
Use an integer literal instead of this unnecessary `double` literal.
7 Box.named({required double size});
8 double get area => 3.0;
Use an integer literal instead of this unnecessary `double` literal.
9 double scale() {
10 return 4.0;
Use an integer literal instead of this unnecessary `double` literal.
11 }
12 double operator +(Box other) => 5.0;
Use an integer literal instead of this unnecessary `double` literal.
16
17double negative() => -6.0;
Use an integer literal instead of this unnecessary `double` literal.
19void main() {
20 double local = 7.0;
Use an integer literal instead of this unnecessary `double` literal.
20 double local = 7.0;
21 var list = <double>[1.0, 2.5];
Use an integer literal instead of this unnecessary `double` literal.
21 var list = <double>[1.0, 2.5];
22 takesDouble(9.0);
Use an integer literal instead of this unnecessary `double` literal.
22 takesDouble(9.0);
23 takesDouble(1, named: 10.0);
Use an integer literal instead of this unnecessary `double` literal.
23 takesDouble(1, named: 10.0);
24 Box(11.0);
Use an integer literal instead of this unnecessary `double` literal.
24 Box(11.0);
25 Box.named(size: 12.0);
Use an integer literal instead of this unnecessary `double` literal.
26 double inner() {
27 return 1e3;
Use an integer literal instead of this unnecessary `double` literal.
28 }
29 for (double i = 0.0; i < 1; i++) {}
Use an integer literal instead of this unnecessary `double` literal.
43void constructorAndCascadeArguments(Matrix matrix) {
44 const Insets.fromSTEB(12.0, 4.0, 12.0, 4.0);
Use an integer literal instead of this unnecessary `double` literal.
43void constructorAndCascadeArguments(Matrix matrix) {
44 const Insets.fromSTEB(12.0, 4.0, 12.0, 4.0);
Use an integer literal instead of this unnecessary `double` literal.
43void constructorAndCascadeArguments(Matrix matrix) {
44 const Insets.fromSTEB(12.0, 4.0, 12.0, 4.0);
Use an integer literal instead of this unnecessary `double` literal.
43void constructorAndCascadeArguments(Matrix matrix) {
44 const Insets.fromSTEB(12.0, 4.0, 12.0, 4.0);
Use an integer literal instead of this unnecessary `double` literal.
45 matrix
46 ..setEntry(3, 2, -1.0)
Use an integer literal instead of this unnecessary `double` literal.
46 ..setEntry(3, 2, -1.0)
47 ..setEntry(0, 0, 2.0);
Use an integer literal instead of this unnecessary `double` literal.
57class NumericChild extends NumericBase {
58 const NumericChild() : super(pressure: 0.0, elevation: 8.0);
Use an integer literal instead of this unnecessary `double` literal.
57class NumericChild extends NumericBase {
58 const NumericChild() : super(pressure: 0.0, elevation: 8.0);
Use an integer literal instead of this unnecessary `double` literal.
58 const NumericChild() : super(pressure: 0.0, elevation: 8.0);
59 const NumericChild.named() : super.named(-2.0, elevation: 4.0);
Use an integer literal instead of this unnecessary `double` literal.
58 const NumericChild() : super(pressure: 0.0, elevation: 8.0);
59 const NumericChild.named() : super.named(-2.0, elevation: 4.0);
Use an integer literal instead of this unnecessary `double` literal.
60 const NumericChild.value(double value) : super(pressure: value);
61 const NumericChild.redirect() : this.value(9.0);
Use an integer literal instead of this unnecessary `double` literal.
70class GenericNumericChild extends GenericNumericBase<double> {
71 const GenericNumericChild() : super(5.0);
Use an integer literal instead of this unnecessary `double` literal.
71 const GenericNumericChild() : super(5.0);
72 const GenericNumericChild.named() : super.named(value: 6.0);
Use an integer literal instead of this unnecessary `double` literal.
76 final int marker;
77 const InitializedNumericChild() : marker = 0, super(pressure: 10.0);
Use an integer literal instead of this unnecessary `double` literal.
91void genericGetterInvocation(RandomValue random) {
92 when(random.nextDouble).thenReturn(0.0);
Use an integer literal instead of this unnecessary `double` literal.
92 when(random.nextDouble).thenReturn(0.0);
93 when(random.nextDouble).thenReturn(1.0);

Valid

example.dartdart
const double myDouble = 8;
final anotherDouble = myDouble + 7.0e2;
const num numValue = 1.0;
double fraction = 1.5;
var inferred = 2.0;
Object object = 3.0;
double huge = 1e300;
double parenthesized = (4.0);

void takesNum(num value) {}

void takesObject(Object value) {}

num numReturn() => 5.0;

double closure() {
  var f = () {
    return 6.0;
  };
  return f();
}

void main() {
  takesNum(7.0);
  takesObject(8.0);
  var list = [1.0, 2.0];
  var nums = <num>[3.0];
  dynamic d = 9.0;
  print([list, nums, d, 1.0.toString()]);
}

class CascadeObjectContexts {
  Object value = 0;
  void operator []=(num index, Object value) {}
}

void declaredAndCascadeContexts(CascadeObjectContexts values, num? number) {
  Object promoted = 1.0;
  if (promoted is double) {
    promoted = 2.0;
  }
  if (number is double?) {
    number ??= 3.0;
  }
  values
    ..value = 4.0
    ..[5.0] = 6.0;
  double conditionContext = (7.0 == 8.0) ? 1 : 2;
}

// The official lint checks no assignment contexts, even of `double` targets.
class CascadeDoubleContexts {
  double value = 0;
  void operator []=(double index, double value) {}
}

void cascadeContexts(CascadeDoubleContexts values) {
  values
    ..value = 13.0;
  values
    ..[14.0] =
      15.0;
}

class NumericBase {
  final double pressure, elevation;
  const NumericBase({required this.pressure, this.elevation = 0});
}

class NumericChild extends NumericBase {
  const NumericChild() : super(pressure: 1.5, elevation: (2.0));
  const NumericChild.integer() : super(pressure: 2);
}

class GenericNumericBase<T> {
  final T value;
  const GenericNumericBase(this.value);
  const GenericNumericBase.named({required this.value});
}

class BroadNumericChild extends GenericNumericBase<num> {
  const BroadNumericChild() : super(7.0);
  const BroadNumericChild.named() : super.named(value: 8.0);
}

class UnconstrainedNumericChild<T> extends GenericNumericBase<T> {
  const UnconstrainedNumericChild(T value) : super(value);
}

Configure prefer-int-literals

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "prefer-int-literals": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/prefer-int-literals: <reason>
  • prefer-interpolation-to-compose-stringsprefer_interpolation_to_compose_stringsrecommended

    Prefers string interpolation to composing strings with +.

  • prefer-is-emptyprefer_is_emptyrecommended

    Flags .length comparisons equivalent to a .isEmpty check.

  • prefer-is-not-emptyprefer_is_not_emptyrecommended

    Flags .length comparisons equivalent to a .isNotEmpty check.

  • prefer-is-not-operatorprefer_is_not_operatorrecommended

    Flags a negated type test !(x is T) that should use the is! operator.

  • prefer-iterable-ofprefer-iterable-ofrecommended

    Flags .from collection constructors in favor of the .of constructor.

  • prefer-iterable-where-typeprefer_iterable_whereTyperecommended

    Flags .where((e) => e is T), which .whereType<T>() expresses directly.

  • prefer-lastprefer-last

    Flags manual last-element indexing in favor of the .last getter.

  • prefer-match-file-name

    Match a file's sole class or enum name to its snake-case basename.