summaryrefslogtreecommitdiffstats
path: root/src/test/ui/lint/unused/unused_attributes-must_use.rs
blob: 51f868706b69b76402f5be9d07c6a27624409706 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#![allow(dead_code, path_statements)]
#![deny(unused_attributes, unused_must_use)]
#![feature(asm_experimental_arch, stmt_expr_attributes, trait_alias)]

#[must_use] //~ ERROR `#[must_use]` has no effect
extern crate std as std2;

#[must_use] //~ ERROR `#[must_use]` has no effect
mod test_mod {}

#[must_use] //~ ERROR `#[must_use]` has no effect
use std::arch::global_asm;

#[must_use] //~ ERROR `#[must_use]` has no effect
const CONST: usize = 4;
#[must_use] //~ ERROR `#[must_use]` has no effect
#[no_mangle]
static STATIC: usize = 4;

#[must_use]
struct X;

#[must_use]
enum Y {
    Z,
}

#[must_use]
union U {
    unit: (),
}

#[must_use] //~ ERROR `#[must_use]` has no effect
impl U {
    #[must_use]
    fn method() -> i32 {
        4
    }
}

#[must_use]
#[no_mangle]
fn foo() -> i64 {
    4
}

#[must_use] //~ ERROR `#[must_use]` has no effect
extern "Rust" {
    #[link_name = "STATIC"]
    #[must_use] //~ ERROR `#[must_use]` has no effect
    static FOREIGN_STATIC: usize;

    #[link_name = "foo"]
    #[must_use]
    fn foreign_foo() -> i64;
}

#[must_use] //~ ERROR unused attribute
global_asm!("");

#[must_use] //~ ERROR `#[must_use]` has no effect
type UseMe = ();

fn qux<#[must_use] T>(_: T) {} //~ ERROR `#[must_use]` has no effect

#[must_use]
trait Use {
    #[must_use] //~ ERROR `#[must_use]` has no effect
    const ASSOC_CONST: usize = 4;
    #[must_use] //~ ERROR `#[must_use]` has no effect
    type AssocTy;

    #[must_use]
    fn get_four(&self) -> usize {
        4
    }
}

#[must_use] //~ ERROR `#[must_use]` has no effect
impl Use for () {
    type AssocTy = ();
}

#[must_use] //~ ERROR `#[must_use]` has no effect
trait Alias = Use;

#[must_use] //~ ERROR `#[must_use]` has no effect
macro_rules! cool_macro {
    () => {
        4
    };
}

fn main() {
    #[must_use] //~ ERROR `#[must_use]` has no effect
    let x = || {};
    x();

    let x = #[must_use] //~ ERROR `#[must_use]` has no effect
    || {};
    x();

    X; //~ ERROR that must be used
    Y::Z; //~ ERROR that must be used
    U { unit: () }; //~ ERROR that must be used
    U::method(); //~ ERROR that must be used
    foo(); //~ ERROR that must be used

    unsafe {
        foreign_foo(); //~ ERROR that must be used
    };

    CONST;
    STATIC;
    unsafe { FOREIGN_STATIC };
    cool_macro!();
    qux(4);
    ().get_four(); //~ ERROR that must be used

    match Some(4) {
        #[must_use] //~ ERROR `#[must_use]` has no effect
        Some(res) => res,
        None => 0,
    };

    struct PatternField {
        foo: i32,
    }
    let s = PatternField { #[must_use]  foo: 123 }; //~ ERROR `#[must_use]` has no effect
    let PatternField { #[must_use] foo } = s; //~ ERROR `#[must_use]` has no effect
}