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

transmute_ptr_to_ptr: False positive when using transmute in consts #5959

Closed
WaffleLapkin opened this issue Aug 25, 2020 · 0 comments · Fixed by #5999
Closed

transmute_ptr_to_ptr: False positive when using transmute in consts #5959

WaffleLapkin opened this issue Aug 25, 2020 · 0 comments · Fixed by #5999
Labels
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

Comments

@WaffleLapkin
Copy link
Member

Using transmute in consts (recently stabilized) to cast one pointer to another raises transmute_ptr_to_ptr warning, despite the fact that dereferencing raw pointers in constants is unstable.

const _: &() = {
    struct ZST;
    let zst = &ZST;

    unsafe {
        core::mem::transmute::<&'static ZST, &'static ()>(zst)   
    }
};
warning: transmute from a reference to a reference
 --> src/lib.rs:6:9
  |
6 |         core::mem::transmute::<&'static ZST, &'static ()>(zst)   
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(zst as *const _::ZST as *const ())`
  |
  = note: `#[warn(clippy::transmute_ptr_to_ptr)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#transmute_ptr_to_ptr

(playground)

const _: &() = {
    struct ZST;
    let zst = &ZST;

    unsafe {
        &*(zst as *const ZST as *const ())
    }
};
error[E0658]: dereferencing raw pointers in constants is unstable
 --> src/lib.rs:6:9
  |
6 |         &*(zst as *const ZST as *const ())
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: see issue #51911 <https://github.com/rust-lang/rust/issues/51911> for more information

(playground)

Meta

  • cargo clippy -V: clippy 0.0.212 (cfbc6d4 2020-08-21)
  • rustc -Vv:
    rustc 1.46.0-beta.5 (cfbc6d4cf 2020-08-21)
    binary: rustc
    commit-hash: cfbc6d4cf1acbac5c025acf8ed68929caa551df3
    commit-date: 2020-08-21
    host: x86_64-unknown-linux-gnu
    release: 1.46.0-beta.5
    LLVM version: 10.0
    
@WaffleLapkin WaffleLapkin added the C-bug Category: Clippy is not doing the correct thing label Aug 25, 2020
@ebroto ebroto added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Aug 27, 2020
rail-rain added a commit to rail-rain/rust-clippy that referenced this issue Sep 1, 2020
Avoid firing the lint when `transmute` in const contexts
as dereferencing raw pointers in consts is unstable. cc rust-lang#5959
@bors bors closed this as completed in 48248af Sep 1, 2020
@bors bors closed this as completed in #5999 Sep 1, 2020
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-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants