summaryrefslogtreecommitdiffstats
path: root/tests/ui/lint/lint-enum-intrinsics-non-enums.rs
blob: 8ad337064e5bf34a01df2873517bb203300eb5ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Test the enum_intrinsics_non_enums lint.

#![feature(variant_count)]

use std::mem::{discriminant, variant_count};

enum SomeEnum {
    A,
    B,
}

struct SomeStruct;

fn generic_discriminant<T>(v: &T) {
    discriminant::<T>(v);
}

fn generic_variant_count<T>() -> usize {
    variant_count::<T>()
}

fn test_discriminant() {
    discriminant(&SomeEnum::A);
    generic_discriminant(&SomeEnum::B);

    discriminant(&());
    //~^ error: the return value of `mem::discriminant` is unspecified when called with a non-enum type

    discriminant(&&SomeEnum::B);
    //~^ error: the return value of `mem::discriminant` is unspecified when called with a non-enum type

    discriminant(&SomeStruct);
    //~^ error: the return value of `mem::discriminant` is unspecified when called with a non-enum type

    discriminant(&123u32);
    //~^ error: the return value of `mem::discriminant` is unspecified when called with a non-enum type

    discriminant(&&123i8);
    //~^ error: the return value of `mem::discriminant` is unspecified when called with a non-enum type
}

fn test_variant_count() {
    variant_count::<SomeEnum>();
    generic_variant_count::<SomeEnum>();

    variant_count::<&str>();
    //~^ error: the return value of `mem::variant_count` is unspecified when called with a non-enum type

    variant_count::<*const u8>();
    //~^ error: the return value of `mem::variant_count` is unspecified when called with a non-enum type

    variant_count::<()>();
    //~^ error: the return value of `mem::variant_count` is unspecified when called with a non-enum type

    variant_count::<&SomeEnum>();
    //~^ error: the return value of `mem::variant_count` is unspecified when called with a non-enum type
}

fn main() {
    test_discriminant();
    test_variant_count();

    // The lint ignores cases where the type is generic, so these should be
    // allowed even though their return values are unspecified
    generic_variant_count::<SomeStruct>();
    generic_discriminant::<SomeStruct>(&SomeStruct);
}