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

Lint default numeric type fallback #6064

Closed
Nemo157 opened this issue Sep 18, 2020 · 6 comments · Fixed by #6662
Closed

Lint default numeric type fallback #6064

Nemo157 opened this issue Sep 18, 2020 · 6 comments · Fixed by #6662
Assignees
Labels
A-lint Area: New lints L-restriction Lint: Belongs in the restriction lint group

Comments

@Nemo157
Copy link
Member

Nemo157 commented Sep 18, 2020

What it does

Lint any integer or floating point constants that have their type determined by the default fallback rather than inference

Categories (optional)

  • Kind: pedantic

What is the advantage of the recommended code over the original code

Avoids unintended use of the numeric default types, forcing the author to think about what type is appropriate.

Drawbacks

Forcing the author to think about what type is appropriate.

Example

let (x, y) = (5, 6.0);

Could be written as:

let (x, y): (i32, f32) = (5, 6.0);
let (x, y) = (5i32, 6.0f32);
@Nemo157 Nemo157 added the A-lint Area: New lints label Sep 18, 2020
@memoryruins
Copy link

It could be helpful to know when under-constrained inference has used a default numeric type, and I agree that pedantic makes sense for the category. Users who want this lint can optionally enable it, while minimizing friction with users that enjoy the current i32 and f32 fallback. Considerations from rfc #212 "restore int fallback" likely still apply here.

Experience with lack of fallback
Unfortunately, when doing the measurements that led us to decide to remove the int fallback, we neglected to consider coding "in the small" (specifically, we did not include tests in the measurements). It turns out that when writing small programs, which includes not only "hello world" sort of things but also tests, the lack of integer inference fallback is quite annoying. This is particularly troublesome since small program are often people's first exposure to Rust. The problems most commonly occur when integers are "consumed" by printing them out to the screen or by asserting equality, both of which are very common in small programs and testing.

For an example outside of Rust, Glasgow Haskell Compiler (GHC) has -Wtype-defaults, which is an off by default warning.

-Wtype-defaults
Have the compiler warn/inform you where in your source the Haskell defaulting mechanism for numeric types kicks in.
This is useful information when converting code from a context that assumed one default into one with another

@flip1995
Copy link
Member

flip1995 commented Oct 5, 2020

I would rather categorize this lint as restriction.

@memoryruins
Copy link

Ah right, restriction ^^; it would definitely fit there.

@Y-Nak
Copy link
Contributor

Y-Nak commented Jan 30, 2021

Can I take this issue if no one working on it?

@flip1995
Copy link
Member

Yeah sure! If you have any questions don't hesitate to ask here, on Zulip or open a WIP PR.

@rustbot assign @Y-Nak

@camsteffen camsteffen added the L-restriction Lint: Belongs in the restriction lint group label Feb 7, 2021
@bors bors closed this as completed in e2753f9 Feb 16, 2021
@Stargateur
Copy link

Stargateur commented Apr 3, 2021

I would clearly put this as warning, specially for integer.

It's very strange that the lint suggest to use i32, one should better think to what the type should be. The advice could be wrong in most case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints L-restriction Lint: Belongs in the restriction lint group
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants