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

False positive for useless_transmute #6356

Closed
digama0 opened this issue Nov 20, 2020 · 1 comment · Fixed by #8564
Closed

False positive for useless_transmute #6356

digama0 opened this issue Nov 20, 2020 · 1 comment · Fixed by #8564
Labels
C-bug Category: Clippy is not doing the correct thing E-hard Call for participation: This a hard problem and requires more experience or effort to work on I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@digama0
Copy link
Contributor

digama0 commented Nov 20, 2020

Source: (SO question, playground link)

#![allow(clippy::needless_lifetimes)]
#![warn(clippy::useless_transmute)]

pub fn insert<'a, K: Eq, V>(this: &'a mut Vec<(K, V)>, key: K, val: V) -> &'a mut V {
    for (key1, val1) in & /* 'b */ mut *this {
        if key == *key1 {
            return unsafe {
                std::mem::transmute::<&/* 'b */ mut V, &/* 'a */ mut V>(val1)
            }
        }
    }
    let this = & /* 'c */ mut *this;
    this.push((key, val));
    &mut this.last_mut().unwrap().1
}

Result:

warning: transmute from a type (`&mut V`) to itself
 --> src/lib.rs:8:17
  |
8 |                 std::mem::transmute::<&/* 'b */ mut V, &/* 'a */ mut V>(val1)
  |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
note: the lint level is defined here
 --> src/lib.rs:2:9
  |
2 | #![warn(clippy::useless_transmute)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_transmute

The transmute is not useless because without it the code does not compile. The comments make it a little clearer what's going on: the transmute is being used to change lifetimes, but since this is not represented in the syntax it looks like it's casting a type to itself. My suggested fix would be to skip the lint if the type being cast to itself contains embedded lifetimes (either a shared or unique reference, or a lifetime parameter in the type).

Meta

  • cargo clippy -V: clippy 0.0.212 (1773f60 2020-11-08)
  • rustc -Vv:
    rustc 1.49.0-nightly (1773f60ea 2020-11-08)
    binary: rustc
    commit-hash: 1773f60ea5d42e86b8fdf78d2fc5221ead222bc1
    commit-date: 2020-11-08
    host: x86_64-unknown-linux-gnu
    release: 1.49.0-nightly
    
@digama0 digama0 added the C-bug Category: Clippy is not doing the correct thing label Nov 20, 2020
@flip1995 flip1995 added E-hard Call for participation: This a hard problem and requires more experience or effort to work on I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied labels Dec 6, 2020
@rail-rain
Copy link
Contributor

I think this is essentially a duplicate of #5343 (this also means the cause is actualy not the implicit syntax, but because Clippy checks types without running the borrow checker). The suggested fix is interesting though. Unlike #5343, which suggests using the real borrow checker; ignoring every types with some lifetimes is far easier to implement although it would introduce a false negative where U outlives T.

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 E-hard Call for participation: This a hard problem and requires more experience or effort to work on 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.

3 participants