1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 #![warn(unused_must_use)]
15 #[derive(PartialEq, Eq)]
22 fn need_to_use_this_method_value(&self) -> usize {
27 fn need_to_use_this_associated_function_value() -> isize {
33 #[must_use = "no side effects"]
34 fn is_even(&self) -> bool;
37 impl EvenNature for MyStruct {
38 fn is_even(&self) -> bool {
44 fn replace(&mut self, substitute: usize) -> usize;
47 impl Replaceable for MyStruct {
48 // ↓ N.b.: `#[must_use]` attribute on a particular trait implementation
49 // method won't work; the attribute should be on the method signature in
50 // the trait's definition.
52 fn replace(&mut self, substitute: usize) -> usize {
53 let previously = self.n;
59 #[must_use = "it's important"]
60 fn need_to_use_this_value() -> bool {
65 need_to_use_this_value(); //~ WARN unused return value
67 let mut m = MyStruct { n: 2 };
68 let n = MyStruct { n: 3 };
70 m.need_to_use_this_method_value(); //~ WARN unused return value
71 m.is_even(); // trait method!
72 //~^ WARN unused return value
74 MyStruct::need_to_use_this_associated_function_value();
75 //~^ WARN unused return value
77 m.replace(3); // won't warn (annotation needs to be in trait definition)
79 // comparison methods are `must_use`
80 2.eq(&3); //~ WARN unused return value
81 m.eq(&n); //~ WARN unused return value
83 // lint includes comparison operators
84 2 == 3; //~ WARN unused comparison
85 m == n; //~ WARN unused comparison