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

transmutes_expressible_as_ptr_casts suggests invalid cast #9894

Closed
matthiaskrgr opened this issue Nov 20, 2022 · 0 comments · Fixed by #10193
Closed

transmutes_expressible_as_ptr_casts suggests invalid cast #9894

matthiaskrgr opened this issue Nov 20, 2022 · 0 comments · Fixed by #10193
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@matthiaskrgr
Copy link
Member

Summary

.

Lint Name

transmutes_expressible_as_ptr_casts

Reproducer

I tried this code:

#[derive(PartialEq, Debug)]
struct p {
  x: isize,
  y: isize,
}

fn p(x: isize, y: isize) -> p {
    p {
        x: x,
        y: y
    }
}

fn test_class() {
  let q = p(1, 2);
  let mut r = p(1, 2);

  unsafe {
  println!("q = {:x}, r = {:x}",
         (::std::mem::transmute::<*const p, usize>(&q)),
         (::std::mem::transmute::<*const p, usize>(&r)));
  }
}

pub fn main() {
    test_class();
}

I saw this happen:

warning: failed to automatically apply fixes suggested by rustc to crate `clpy`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
warning: type `p` should have an upper camel case name
 --> src/main.rs:2:8
  |
2 | struct p {
  |        ^ help: convert the identifier to upper camel case: `P`
  |
  = note: `#[warn(non_camel_case_types)]` on by default

error[E0606]: casting `&p` as `usize` is invalid
  --> src/main.rs:20:10
   |
20 |          &q as usize,
   |          ^^^^^^^^^^^
   |
   = help: cast through a raw pointer first

error[E0606]: casting `&p` as `usize` is invalid
  --> src/main.rs:21:10
   |
21 |          &r as usize);
   |          ^^^^^^^^^^^
   |
   = help: cast through a raw pointer first

error: aborting due to 2 previous errors; 1 warning emitted

For more information about this error, try `rustc --explain E0606`.
Original diagnostics will follow.

warning: type `p` should have an upper camel case name
 --> src/main.rs:2:8
  |
2 | struct p {
  |        ^ help: convert the identifier to upper camel case: `P`
  |
  = note: `#[warn(non_camel_case_types)]` on by default

warning: variable does not need to be mutable
  --> src/main.rs:16:7
   |
16 |   let mut r = p(1, 2);
   |       ----^
   |       |
   |       help: remove this `mut`
   |
   = note: `#[warn(unused_mut)]` on by default

warning: transmute from `*const p` to `usize` which could be expressed as a pointer cast instead
  --> src/main.rs:20:10
   |
20 |          (::std::mem::transmute::<*const p, usize>(&q)),
   |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&q as usize`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#transmutes_expressible_as_ptr_casts
   = note: requested on the command line with `-W clippy::transmutes-expressible-as-ptr-casts`

warning: transmute from `*const p` to `usize` which could be expressed as a pointer cast instead
  --> src/main.rs:21:10
   |
21 |          (::std::mem::transmute::<*const p, usize>(&r)));
   |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&r as usize`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#transmutes_expressible_as_ptr_casts

warning: `clpy` (bin "clpy") generated 4 warnings (run `cargo fix --bin "clpy"` to apply 3 suggestion

Version

rustc 1.67.0-nightly (c5d82ed7a 2022-11-19)
binary: rustc
commit-hash: c5d82ed7a4ad94a538bb87e5016e7d5ce0bd434b
commit-date: 2022-11-19
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly

Additional Labels

No response

@matthiaskrgr matthiaskrgr added C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied I-false-positive Issue: The lint was triggered on code it shouldn't have labels Nov 20, 2022
@bors bors closed this as completed in 89d443a Jan 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
1 participant