Lint for integral divisions that are widened to a float#10313
Merged
SethTisue merged 1 commit intoscala:2.12.xfrom Mar 8, 2023
Merged
Lint for integral divisions that are widened to a float#10313SethTisue merged 1 commit intoscala:2.12.xfrom
SethTisue merged 1 commit intoscala:2.12.xfrom
Conversation
c11c480 to
df1b859
Compare
SethTisue
reviewed
Feb 20, 2023
SethTisue
reviewed
Feb 20, 2023
SethTisue
reviewed
Feb 20, 2023
Member
SethTisue
left a comment
There was a problem hiding this comment.
LGTM except for the minor tweaks I've suggested
som-snytt
reviewed
Feb 20, 2023
Member
Author
|
I tested this, it works well and seems very valueable. |
Contributor
|
I'm going to try this out! I hope the change is not too divisive. |
hamzaremmal
pushed a commit
to hamzaremmal/scala3
that referenced
this pull request
May 2, 2025
hamzaremmal
pushed a commit
to scala/scala3
that referenced
this pull request
May 7, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add a lint warning for integer divisions that are implicitly converted to floats (
someInt / 2: Double). Such expressions are probably a mistake, or otherwise very likely to be misunderstood by reviewers / readers.The same argument could be made for other integer operations as they can overflow.
Int.MaxValue * 2: Doubleis different fromInt.MaxValue.toDouble * 2. But division is much more likely to be a bug because the result is different also without an overflow.The existing
-Ywarn-numeric-widenflag (-Wnumeric-widenon 2.13) warns about all widening conversions, which is probably too restrictive for some users.While working on this, I noticed a couple of extension methods on integrals that are nonsensical:
ceil,floor,isNaN,isNegInfinity,isPosInfinity,isInfinite,isInfinity,isFinite. We could add a warning for those in RefChecks, for example.someInt.isNaNcompiles toPredef.float2Float(someInt.toFloat).isNaN, the widening conversiontoFloatis inserted during implicit search.