Rust-clippy: New Lint: use of built-in (or prelude) type as identifier

Created on 17 Apr 2018  路  2Comments  路  Source: rust-lang/rust-clippy

This compiles today, but doesn't do what many expect:

    struct Foo { x: u32 }
    let y = Foo { x: 4 };
    let Foo { x: u32 } = y;

https://play.rust-lang.org/?gist=2b9bb2b1e25b5a9d85c7adecf301fd22&version=stable

It would be nice if that gave a lint like "variable/module/function/macro u32 has the same name as a built-in type". Could possibly also apply to things like let Vec = 4; too, but I suspect that's less likely because of casing conventions.

cc https://internals.rust-lang.org/t/random-musings-types-in-patterns/7316/9?u=scottmcm
cc https://github.com/rust-lang/rust/blob/9da2112238c565a7e36ada0a1f9c899961910131/src/test/run-pass/weird-exprs.rs#L87-L104

A-correctness L-lint

Most helpful comment

I think that there should be a warning whenever a binding is created that shadows a type in general. This has got to be accidental in 99.9% of all cases!

let u32 = 42i32; // No warning

Try in Playground

This might even make sense to put into the compiler directly.

All 2 comments

I think that there should be a warning whenever a binding is created that shadows a type in general. This has got to be accidental in 99.9% of all cases!

let u32 = 42i32; // No warning

Try in Playground

This might even make sense to put into the compiler directly.

This might even make sense to put into the compiler directly.

The standard library contains modules that share a name with primitives. Other libraries might want to do the same.

Was this page helpful?
0 / 5 - 0 ratings