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

Add warning when ptr-to-int cast would truncate #31012

Closed
jethrogb opened this issue Jan 19, 2016 · 4 comments
Closed

Add warning when ptr-to-int cast would truncate #31012

jethrogb opened this issue Jan 19, 2016 · 4 comments
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut.

Comments

@jethrogb
Copy link
Contributor

There should be a lint that warns if you cast a pointer to any integer smaller than usize. For example to prevent cases like this:

println!("{}",std::mem::size_of::<std::fs::File> as u32);
@sfackler sfackler added the A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. label Jan 19, 2016
@Amanieu
Copy link
Member

Amanieu commented Jan 19, 2016

In this example, a warning should only be issued when compiling to a 64-bit target, since casting a pointer to u32 is perfectly valid on 32-bit targets. Maybe something like "Warning: cast to u32 may truncate pointer value". If truncation is really intended then the warning can be avoided by adding an intermediate usize cast.

@jethrogb
Copy link
Contributor Author

There could also be a default-allow lint that complains about any pointer cast not to usize.

@nagisa
Copy link
Member

nagisa commented Jan 19, 2016

Should be reported to clippy instead, I think. Cast possible truncation lint may be what you’re looking for.

@steveklabnik
Copy link
Member

Since new lints have a big impact on users of rustc, the policy is that they should go through the RFC process like other user-facing changes. As such, I'm going to give this one a close, but if anyone comes across this ticket and wants this lint, consider adding it to clippy and/or writing up an RFC. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut.
Projects
None yet
Development

No branches or pull requests

5 participants