Skip to content

Lint integer variable fallback to i32 #6634

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

Closed
tamuhey opened this issue Jan 24, 2021 · 1 comment
Closed

Lint integer variable fallback to i32 #6634

tamuhey opened this issue Jan 24, 2021 · 1 comment
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy T-middle Type: Probably requires verifiying types

Comments

@tamuhey
Copy link
Member

tamuhey commented Jan 24, 2021

From https://stackoverflow.com/questions/65867874/how-to-deny-int-variable-without-explicit-type-annotation-in-rust?noredirect=1#comment116458771_65867874

Related: https://github.com/rust-lang/rfcs/blob/master/text/0212-restore-int-fallback.md

What it does

Reports integer variable fallback to i32, e.g.

let mut counter = 0; # Warning, because the type of `counter` is ambiguous and fallback to `i32`
let mut counter: u64 = 0; # OK
let mut counter = 0usize # OK

The below code should also be accepted because the variable is inferred though it is not annotated:

fn f(x: usize);
let mut counter = 0; # OK, because the variable `counter` is inferred as `usize` later.
f(counter)

Categories (optional)

maybe pedantic

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

I want this lint rule because it can cause overflow unintendedly:

let mut counter = 0;
for _ in 0..1_000_000_000_000usize {
    counter += 1;  # overflow!
    ...
}

Drawbacks

May feel tedious

Example

let counter = 0;

Could be written as:

let counter = 0usize;
let counter: usize = 0;
// or make `couter` to be inferred as `usize`
f(counter); // f: (usize) -> (); for example.
@tamuhey tamuhey added the A-lint Area: New lints label Jan 24, 2021
@camsteffen camsteffen added good-first-issue These issues are a good way to get started with Clippy T-middle Type: Probably requires verifiying types labels Jan 26, 2021
@Y-Nak
Copy link
Contributor

Y-Nak commented Jan 30, 2021

This issue seems duplicate of #6064.

@tamuhey tamuhey closed this as completed Jan 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy T-middle Type: Probably requires verifiying types
Projects
None yet
Development

No branches or pull requests

3 participants