Skip to content

mem::discriminant footgun #3342

Closed
Closed
@oli-obk

Description

@oli-obk

calling mem::discriminant on a &&T will not deref the value to get the discriminant of the value, but just give you the same discriminant irrelevant of what you point to.

The following program demonstrates this:

enum Foo {
    A,
    B,
}

fn main() {
    println!("{:?}", std::mem::discriminant(&Foo::A)); // 0
    println!("{:?}", std::mem::discriminant(&&Foo::A)); // 0
    println!("{:?}", std::mem::discriminant(&Foo::B)); // 1
    println!("{:?}", std::mem::discriminant(&&Foo::B)); // 0
}

It makes little sense to call mem::discriminant on double references, similarly to cloning double references.

We should lint about this. (Also I screwed this up in the compiler: https://github.com/rust-lang/rust/blob/ca2639e82ec4a18d7359efbfb555ea69dd644c97/src/librustc/ich/impls_ty.rs#L515 and I think it's causing ICEs in a PR I'm doing)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.L-correctnessLint: Belongs in the correctness lint group

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions