Skip to content

False positives when using transmute() with raw trait object pointers to change trait object lifetimes #2906

Closed
@okready

Description

@okready

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)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.I-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions