Unnamed repository; edit this file 'description' to name the repository.
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
use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};

// Diagnostic: unused-must-use
//
// This diagnostic is triggered when a value with the `#[must_use]` attribute
// is dropped without being used.
pub(crate) fn unused_must_use(
    ctx: &DiagnosticsContext<'_, '_>,
    d: &hir::UnusedMustUse,
) -> Diagnostic {
    Diagnostic::new_with_syntax_node_ptr(
        ctx,
        DiagnosticCode::RustcLint("unused_must_use"),
        "unused return value that must be used",
        d.expr.map(Into::into),
    )
    .stable()
}

#[cfg(test)]
mod tests {
    use crate::tests::check_diagnostics;

    #[test]
    fn unused_must_use_function_call() {
        check_diagnostics(
            r#"
#[must_use]
fn produces() -> i32 { 0 }
fn main() {
    produces();
  //^^^^^^^^^^ warn: unused return value that must be used
}
"#,
        );
    }

    #[test]
    fn unused_must_use_method_call() {
        check_diagnostics(
            r#"
struct S;
impl S {
    #[must_use]
    fn produces(&self) -> i32 { 0 }
}
fn main() {
    let s = S;
    s.produces();
  //^^^^^^^^^^^^ warn: unused return value that must be used
}
"#,
        );
    }

    #[test]
    fn unused_must_use_type() {
        check_diagnostics(
            r#"
#[must_use]
struct Important;
fn produces() -> Important { Important }
fn main() {
    produces();
  //^^^^^^^^^^ warn: unused return value that must be used
}
"#,
        );
    }

    #[test]
    fn no_warning_when_value_used() {
        check_diagnostics(
            r#"
#[must_use]
fn produces() -> i32 { 0 }
fn main() {
    let _x = produces();
}
"#,
        );
    }

    #[test]
    fn no_warning_when_no_must_use_attribute() {
        check_diagnostics(
            r#"
fn ordinary() -> i32 { 0 }
fn main() {
    ordinary();
}
"#,
        );
    }

    #[test]
    fn no_warning_when_value_assigned() {
        check_diagnostics(
            r#"
#[must_use]
fn produces() -> i32 { 0 }
fn main() {
    let x;
    x = produces();
    let _ = x;
}
"#,
        );
    }
}