Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn on lossy conversion of literals & constants #13792

Merged
merged 1 commit into from Nov 3, 2021

Conversation

dwijnand
Copy link
Member

No description provided.

Co-authored-by: Dale Wijnand <dale.wijnand@gmail.com>
@dwijnand dwijnand marked this pull request as ready for review October 27, 2021 15:25
@dwijnand dwijnand requested a review from sjrd October 27, 2021 15:26
@SethTisue
Copy link
Member

We copied the text of the warning directly from Scala 2.

On further thought, I realized that if you do want the conversion, writing .toFloat or .toDouble, as the warning message suggests, is many more additional characters than simply appending d or f to the constant. Scala 2:

scala 2.13.6> 123456789: Float
              ^
              warning: Widening conversion from Int to Float is deprecated because it loses precision. Write `.toFloat` instead.
val res7: Float = 1.23456792E8

scala 2.13.6> 123456789f
val res8: Float = 1.23456792E8

but as @dwijnand points out, the single character at the end of the constant could be quite easy to miss, so perhaps the recommendation we are making isn't so bad.

No strong opinion on this, but I think the PR is mergeable as-is given that it achieves parity with an already well-established Scala 2 behavior.

@dwijnand dwijnand merged commit 83effc3 into scala:master Nov 3, 2021
@dwijnand dwijnand deleted the lint-too-big-constants-pr branch November 3, 2021 17:17
@nicolasstucki
Copy link
Contributor

Made test fail in nightly build #13878

@Kordyjan Kordyjan added this to the 3.1.2 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Assigning integral literals to floating-point types should warn when losing precision
5 participants