-
Notifications
You must be signed in to change notification settings - Fork 1.6k
unnecessary_cast on as u64
for a c_ulong
#10555
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
Comments
Note that use core::ffi::c_ulong;
fn main() {
let x: c_ulong = 0;
let y: u64 = x.into();
println!("Hello, {y}!");
} produces:
|
This one is tricky, #8596 would cover the case of |
On Wed, Mar 29, 2023 at 04:50:24AM -0700, Alex Macleod wrote:
This one is tricky, #8596 would cover the case of `some_u64 as c_ulong` where we can check the `rustc_hir::Ty` of `c_ulong` to see it's an alias, but for the `x` in `x as u64` we only have the `rustc_middle::ty::Ty` which doesn't distinguish `u64` from `c_ulong`
I realize that it's hard to distinguish type aliases, but I think it's
important in order to avoid false positives like this. Casting a c_ulong
to a u64 is useful in portable code.
|
I saw https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/type.20aliases.20and.20names.20in.20errors today, may be a good way to resolve it if it does end up happening Yeah I'm not saying it's unimportant, just a note for contributors looking through issues that this one wouldn't be easy |
The demonstrative example has been handled but the general issue remains, e.g. fn main() {
let pid = unsafe { libc::getpid() };
pid as i32;
} |
Ignore more type aliases in `unnecessary_cast` This is potentially the worst code I've ever written, and even if not, it's very close to being on par with starb. This will ignore `call() as i32` and `local_obtained_from_call as i32` now. This should fix every reasonable way to reproduce #10555, but likely not entirely. changelog: Ignore more type aliases in `unnecessary_cast`
clippy falsely complains about these lines. The problem is known, but unfixed [1]. So lets silence the warning until a fix is widely available. clippy version: clippy 0.1.70 (90c5418 2023-05-31). [1] rust-lang/rust-clippy#10555 Reported-by: Kent Gibson <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Erik Schilling <[email protected]> Acked-by: Viresh Kumar <[email protected]> Reviewed-by: Kent Gibson <[email protected]> Signed-off-by: Bartosz Golaszewski <[email protected]>
- trips on false positive rust-lang/rust-clippy#10555
Summary
unnecessary_cast triggers on casts from C FFI type aliases, even though the type aliases may not always match the type being cast to.
Lint Name
unnecessary_cast
Reproducer
I tried this code:
I saw this happen:
I expected to not get an error, because
c_ulong
is a type alias that isn't alwaysu64
.I thought #8596 was supposed to fix this.
Version
Additional Labels
No response
The text was updated successfully, but these errors were encountered: