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 positives when using transmute() with raw trait object pointers to change trait object lifetimes #2906

Closed
okready opened this issue Jul 8, 2018 · 2 comments · Fixed by #8564
Labels
C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@okready
Copy link

okready commented Jul 8, 2018

With clippy 0.0.212, I'm getting what appears to be false positives when using transmute() to cast either to or from a raw trait object pointer where the input and output types have invariant lifetimes, or if the output type has a longer lifetime such as 'static (casting using as isn't allowed in such cases). No warnings are triggered if the source and target types are both references.

use std::mem::transmute;

pub trait DummyTrait {}

pub unsafe fn ref_to_ref<'a, 'b>(obj: &'a (DummyTrait + 'a)) -> &'b (DummyTrait + 'b) {
    // No warnings.
    transmute(obj)
}

pub unsafe fn ptr_to_ptr<'a, 'b>(obj: *const (DummyTrait + 'a)) -> *const (DummyTrait + 'b) {
    // Triggers `transmute_ptr_to_ptr` warning.
    transmute(obj)
}

pub unsafe fn ptr_to_ref<'a, 'b>(obj: *const (DummyTrait + 'a)) -> &'b (DummyTrait + 'b) {
    // Triggers `transmute_ptr_to_ref` warning.
    transmute(obj)
}

pub unsafe fn ref_to_ptr<'a, 'b>(obj: &'a (DummyTrait + 'a)) -> *const (DummyTrait + 'b) {
    // Triggers `useless_transmute` warning.
    transmute(obj)
}
@rail-rain
Copy link
Contributor

rail-rain commented Dec 26, 2020

The warnings has changed due to #5343. Here is the lastest one for your convinience.

#![allow(clippy::missing_safety_doc)]
// `useless_transmute` is now an allowed-by-default lint.
#![warn(clippy::useless_transmute)]

use std::mem::transmute;

pub trait DummyTrait {}

pub unsafe fn ref_to_ref<'a, 'b>(obj: &'a (dyn DummyTrait + 'a)) -> &'b (dyn DummyTrait + 'b) {
    // Triggers `useless_transmute` warning.
    // Note: this is the false positive introduced #5343
    transmute(obj)
}

pub unsafe fn ptr_to_ptr<'a, 'b>(obj: *const (dyn DummyTrait + 'a)) -> *const (dyn DummyTrait + 'b) {
    // Triggers `useless_transmute` warning.
    // Note: `useless_transmute` took over because `useless_transmute` has a higher priority than `transmute_ptr_to_ptr`
    // besides the false positive above.
    transmute(obj)
}

pub unsafe fn ptr_to_ref<'a, 'b>(obj: *const (dyn DummyTrait + 'a)) -> &'b (dyn DummyTrait + 'b) {
    // Triggers `transmute_ptr_to_ref` warning.
    transmute(obj)
}

pub unsafe fn ref_to_ptr<'a, 'b>(obj: &'a (dyn DummyTrait + 'a)) -> *const (dyn DummyTrait + 'b) {
    // Triggers `useless_transmute` warning.
    transmute(obj)
}

This issue means (every?) lints for transmute don't account lifetimes, including transmutes_expressible_as_ptr_cast (if you commented out other transmute related lints, it would fire on the example).

@rail-rain
Copy link
Contributor

@rustbot label +E-medium +L-bug +L-false-positive

@rustbot rustbot added E-medium Call for participation: Medium difficulty level problem and requires some initial experience. C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Dec 26, 2020
@bors bors closed this as completed in 7000e75 May 31, 2022
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-medium Call for participation: Medium difficulty level problem and requires some initial experience. I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants