]>
git.proxmox.com Git - rustc.git/blob - tests/ui/lint/fn_must_use.rs
3 #![warn(unused_must_use)]
5 #[derive(PartialEq, Eq)]
12 fn need_to_use_this_method_value(&self) -> usize {
17 fn need_to_use_this_associated_function_value() -> isize {
23 #[must_use = "no side effects"]
24 fn is_even(&self) -> bool
;
27 impl EvenNature
for MyStruct
{
28 fn is_even(&self) -> bool
{
34 fn replace(&mut self, substitute
: usize) -> usize;
37 impl Replaceable
for MyStruct
{
38 // ↓ N.b.: `#[must_use]` attribute on a particular trait implementation
39 // method won't work; the attribute should be on the method signature in
40 // the trait's definition.
42 fn replace(&mut self, substitute
: usize) -> usize {
43 let previously
= self.n
;
49 #[must_use = "it's important"]
50 fn need_to_use_this_value() -> bool
{
55 need_to_use_this_value(); //~ WARN unused return value
57 let mut m
= MyStruct { n: 2 }
;
58 let n
= MyStruct { n: 3 }
;
60 m
.need_to_use_this_method_value(); //~ WARN unused return value
61 m
.is_even(); // trait method!
62 //~^ WARN unused return value
64 MyStruct
::need_to_use_this_associated_function_value();
65 //~^ WARN unused return value
67 m
.replace(3); // won't warn (annotation needs to be in trait definition)
69 // comparison methods are `must_use`
70 2.eq(&3); //~ WARN unused return value
71 m
.eq(&n
); //~ WARN unused return value
73 // lint includes comparison operators
74 2 == 3; //~ WARN unused comparison
75 m
== n
; //~ WARN unused comparison