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
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);
}const double myDouble = 8.0;const double myDouble = 8.0;double? maybe = 1.0;class Box { final double width = 2.0; Box.named({required double size}); double get area => 3.0; double scale() { return 4.0; } double operator +(Box other) => 5.0; double negative() => -6.0;void main() { double local = 7.0; double local = 7.0; var list = <double>[1.0, 2.5]; var list = <double>[1.0, 2.5]; takesDouble(9.0); takesDouble(9.0); takesDouble(1, named: 10.0); takesDouble(1, named: 10.0); Box(11.0); Box(11.0); Box.named(size: 12.0); double inner() { return 1e3; } for (double i = 0.0; i < 1; i++) {}void constructorAndCascadeArguments(Matrix matrix) { const Insets.fromSTEB(12.0, 4.0, 12.0, 4.0);void constructorAndCascadeArguments(Matrix matrix) { const Insets.fromSTEB(12.0, 4.0, 12.0, 4.0);void constructorAndCascadeArguments(Matrix matrix) { const Insets.fromSTEB(12.0, 4.0, 12.0, 4.0);void constructorAndCascadeArguments(Matrix matrix) { const Insets.fromSTEB(12.0, 4.0, 12.0, 4.0); matrix ..setEntry(3, 2, -1.0) ..setEntry(3, 2, -1.0) ..setEntry(0, 0, 2.0);class NumericChild extends NumericBase { const NumericChild() : super(pressure: 0.0, elevation: 8.0);class NumericChild extends NumericBase { const NumericChild() : super(pressure: 0.0, elevation: 8.0); const NumericChild() : super(pressure: 0.0, elevation: 8.0); const NumericChild.named() : super.named(-2.0, elevation: 4.0); 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 GenericNumericChild extends GenericNumericBase<double> { const GenericNumericChild() : super(5.0); const GenericNumericChild() : super(5.0); const GenericNumericChild.named() : super.named(value: 6.0); final int marker; const InitializedNumericChild() : marker = 0, super(pressure: 10.0);void genericGetterInvocation(RandomValue random) { when(random.nextDouble).thenReturn(0.0); when(random.nextDouble).thenReturn(0.0); when(random.nextDouble).thenReturn(1.0);Valid
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.
{
"linter": {
"rules": {
"style": {
"prefer-int-literals": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/prefer-int-literals: <reason>More style rules
- prefer-interpolation-to-compose-strings
prefer_interpolation_to_compose_stringsrecommendedPrefers string interpolation to composing strings with
+. - prefer-is-empty
prefer_is_emptyrecommendedFlags
.lengthcomparisons equivalent to a.isEmptycheck. - prefer-is-not-empty
prefer_is_not_emptyrecommendedFlags
.lengthcomparisons equivalent to a.isNotEmptycheck. - prefer-is-not-operator
prefer_is_not_operatorrecommendedFlags a negated type test
!(x is T)that should use theis!operator. - prefer-iterable-of
prefer-iterable-ofrecommendedFlags
.fromcollection constructors in favor of the.ofconstructor. - prefer-iterable-where-type
prefer_iterable_whereTyperecommendedFlags
.where((e) => e is T), which.whereType<T>()expresses directly. - prefer-last
prefer-lastFlags manual last-element indexing in favor of the
.lastgetter. - prefer-match-file-name
Match a file's sole class or enum name to its snake-case basename.