You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is nothing here that says that Int or any other integer type will be used, but because we are using integer literals, we end up inferring that we should use integer division. However, because 1 < 8, this division will always produce 0. It is more likely that the user wanted to compute 0.125 in Double or some other floating-point type.
In this situation, we should warn the developer that the result will always be rounded to 0, and provide two fix-its: one which changes the integer literals to floating-point literals, and one which silences the warning by mentioning Int explicitly.
To be clear, here are some code snippets which ideally should not warn:
let x: Int = 1 / 8
let x = Int(1) / 8
let x = 1 / Int(8)
let x = 1 / 8 as Int
functionTakingInt(1 / 8) // Bonus points: warn if a Double would also be valid in that argument
let x = 1.0 / 8
let x = 1 / 8.0
let x = 1.0 / 8.0
let x = 9 / 8
let x = 0 / 8
let x = -9 / 8
let x = y / 8
let x = 1 / y
let x = y / y
One way to make the idea of "Int was inferred" concrete might be to ask whether we were forced to rely on default type constraints to determine the literals' types.
The text was updated successfully, but these errors were encountered:
Additional Detail from JIRA
md5: 85fc84b679e3eb6e54def2cf78308558
Issue Description:
Consider the following code:
There is nothing here that says that
Int
or any other integer type will be used, but because we are using integer literals, we end up inferring that we should use integer division. However, because 1 < 8, this division will always produce 0. It is more likely that the user wanted to compute 0.125 inDouble
or some other floating-point type.In this situation, we should warn the developer that the result will always be rounded to 0, and provide two fix-its: one which changes the integer literals to floating-point literals, and one which silences the warning by mentioning
Int
explicitly.To be clear, here are some code snippets which ideally should not warn:
One way to make the idea of "
Int
was inferred" concrete might be to ask whether we were forced to rely on default type constraints to determine the literals' types.The text was updated successfully, but these errors were encountered: