-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #43728 - zackmdavis:fnused, r=eddyb
#[must_use] for functions This implements [RFC 1940](rust-lang/rfcs#1940). The RFC and discussion thereof seem to suggest that tagging `PartialEq::eq` and friends as `#[must_use]` would automatically lint for unused comparisons, but it doesn't work out that way (at least the way I've implemented it): unused `.eq` method calls get linted, but not `==` expressions. (The lint operates on the HIR, which sees binary operations as their own thing, even if they ultimately just call `.eq` _&c._.) What do _you_ think?? Resolves #43302.
- Loading branch information
Showing
4 changed files
with
79 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
|
||
struct MyStruct { | ||
n: usize | ||
} | ||
|
||
impl MyStruct { | ||
#[must_use] | ||
fn need_to_use_this_method_value(&self) -> usize { | ||
self.n | ||
} | ||
} | ||
|
||
#[must_use="it's important"] | ||
fn need_to_use_this_value() -> bool { | ||
false | ||
} | ||
|
||
fn main() { | ||
need_to_use_this_value(); | ||
|
||
let m = MyStruct { n: 2 }; | ||
m.need_to_use_this_method_value(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
warning: unused return value of `need_to_use_this_value` which must be used: it's important | ||
--> $DIR/fn_must_use.rs:29:5 | ||
| | ||
29 | need_to_use_this_value(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: #[warn(unused_must_use)] on by default | ||
|
||
warning: unused return value of `MyStruct::need_to_use_this_method_value` which must be used | ||
--> $DIR/fn_must_use.rs:32:5 | ||
| | ||
32 | m.need_to_use_this_method_value(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|