Skip to content

trivial_numeric_casts warning incorrect when casting between type aliases #23739

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
lilyball opened this issue Mar 26, 2015 · 8 comments
Closed
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.

Comments

@lilyball
Copy link
Contributor

The new trivial_numeric_casts lint incorrectly warns when casting between two types that happen to be type aliases. This is incorrect because the types may not actually be type aliases under all build configurations. Case in point, libc::c_long is i32 on some architectures and i64 on others. If I believe the trivial_numeric_casts warning when casting from an i64 to a libc::c_long, that breaks compilation on 32-bit architectures.

#![feature(libc)]

extern crate libc;

fn main() {
    let x: i64 = 1;
    let _y = x as libc::c_long;
//           ^~~~~~~~~~~~~~~~~
// warning: trivial numeric cast: `i64` as `i64`, #[warn(trivial_numeric_casts)] on by default
}
@lilyball
Copy link
Contributor Author

cc @nrc

@lilyball
Copy link
Contributor Author

The lint was added in #23630

@sfackler sfackler added the A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. label Mar 26, 2015
@nikomatsakis
Copy link
Contributor

the idea was that you could "allow" the lint in that case...?

@alexcrichton
Copy link
Member

I personally agree that this is such a common use case that I would prefer to not have to #[allow(trivial_numeric_casts)] in all crates that have some form of FFI binding.

@Ryman
Copy link
Contributor

Ryman commented Mar 26, 2015

@nikomatsakis There's poor interaction with macros in that case. I had a similar case for function pointer casting, which would require library users to use #![allow(..)] in their crates which use nickel.

It's along the lines of http://is.gd/8ZfS1I

@eddyb
Copy link
Member

eddyb commented Mar 26, 2015

What about an attribute (maybe #[allow(trivial_numeric_casts)] itself, but that might not work) on the types aliases which may vary between compilations?

@nrc
Copy link
Member

nrc commented Mar 26, 2015

This is correct behaviour (according to the RFC). We warn if it is a trivial cast, we don't take account of what type aliases might be under different configurations (I'm not even sure we could). If you are doing these kind of casts, you should just use #[allow(trivial_numeric_casts)].

Closing, since this is correct behaviour. Please file an RFC issue if you think the spec'ed behaviour is incorrect.

@nrc nrc closed this as completed Mar 26, 2015
@alexcrichton
Copy link
Member

I've opened an RFC issue about this: rust-lang/rfcs#1020

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

No branches or pull requests

7 participants