dart-lang / linter

Linter for Dart.
https://dart.dev/tools/linter-rules
BSD 3-Clause "New" or "Revised" License
628 stars 172 forks source link

proposal: `prefer_double_literals` #4510

Open mateusfccp opened 1 year ago

mateusfccp commented 1 year ago

prefer_double_literals

Description

A lint that warns if someone uses an int literal where the target should receive a double. Basically, the inverse of prefer_int_literals.

Details

Since Dart 2.1, we are allowed to pass an int where a double is expected, and it will be converted to its double equivalent. For instance, we can do final double a = 1; instead of final double a = 1.0;.

https://github.com/dart-lang/sdk/issues/34824 introduced prefer_int_literals, where the linter suggests doing it whenever possible. However, sometimes the user wants the contrary, because using double literals makes it clearer that we are dealing with doubles.

An example of this is in the Flutter repository style guide itself: Use double literals for double constants

Kind

Enforce style advise

Bad Examples

final double a = 1;
printDouble(1);

void printDouble(double a) => print(a);

Good Examples

final double a = 1.0;
printDouble(1.0);

void printDouble(double a) => print(a);

Discussion

mateusfccp commented 11 months ago

If I open a PR for this, will it get accepted?

srawlins commented 10 months ago

Sorry for the delayed response. I don't think a PR for this would be accepted yet. We must have a consensus that it would be a high value lint.

lrhn commented 9 months ago

Just as a comment, I think this lint still be at least as valuable as prefer_int_literals. Both are entirely style based, there is absolutely no semantic difference between using a decimal integer literal and a double literal with the same value.

That might not be "high value" on an absolute scale, but if the two lints can share any code, the effort to add the second lint should be lower than the first.

So potentially bigger value and lower cost should make this a slam-dunk if evaluated with the same measures as prefer_int_literals.

I personally find integer literals less readable, because they don't tell mere that the value is actually a double, where other people likely find the .0 to be unnecessary noise. It's mainly subjective taste, and a choice of style for consistency.

I'd personally use this lint, and wouldn't use prefer_int_literals.

mateusfccp commented 9 months ago

In our team we use Flutter style guide as our style guide, and I was surprised that there was no lint to support it, just the opposite.

That might not be "high value" on an absolute scale, but if the two minutes can share any code, the effort to add the second lint should be lower than the first.

This is exactly what I was thinking. The code will probably be mostly the same, so it does not seems costful to implement.

srawlins commented 8 months ago

Well, it'd probably be accepted then. We don't meet as a team to accept or reject proposals. The number of people you need to convince is on the order of 1 😛 . I won't guarantee it'd be accepted.

To be transparent: I really don't like our lack of process here either. We've responded to PRs before with "whoa, let's get consensus on a proposal first." But if someone proposes a lint rule, we also have no process for reviewing it; the vast majority languish.

mateusfccp commented 8 months ago

@lrhn @srawlins

I just opened a PR in https://dart-review.googlesource.com/c/sdk/+/337842

squiddy-code commented 2 months ago

Any update on this?

lrhn commented 2 months ago

None since the last comments on the PR.

The way I'd scope this feature would be to make it precisely the opposite of prefer_int_literals, in that it triggers for precisely the same expressions, numerals with integer values in a double-expecting context, but instead of requiring an integer numeral, with no fraction or exponent, it prohibits it.

prefer_double_literals

Description

Disallows using a decimal integer numeral where it will be interpreted as a double literal. Instead use a floating point numeral with a fraction and/or exponent part, even if it is only a trailing .0.

Details

Since Dart 2.1, an integer numeral (decimal digits with no fraction or exponent part) in a context where a double value is required, is interpreted as a double literal. For instance, one can do final double a = 1; instead of final double a = 1.0;, and it means precisely the same thing.

This lint insists on having non-integer numeral in those positions, to make it explicitly visible that the value is a double value, not an int. That means a floating point numeral with at a fraction part and/or an exponent part. Typically this is ensured by appending .0 to the integer numeral.

The lint prefer_int_literals requires using an integer numeral where a number literal has an integral value and will be interpreted as a double value. This lints requires using a floating point literal in the same places.

An example of this is in the Flutter repository style guide itself: Use double literals for double constants

Kind

Enforce style advise

Bad Examples

final double a = 1;
printDouble(1);

void printDouble(double a) => print(a);

Good Examples

final double a = 1.0;
printDouble(1.0);

void printDouble(double a) => print(a);

Discussion