提交 8f19cadf 编写于 作者: B bors

Auto merge of #55003 - zackmdavis:must_use_assoc_fn, r=varkor

`#[must_use]` for associated functions is supposed to actually work

In the comments of (closed, defunct) pull request #54884, @Centril [noted that](https://github.com/rust-lang/rust/pull/54884#issuecomment-427626495) must-use annotations didn't work on an associated function (what other communities might call a "static method"). Subsequent logging revealed that in this case we have a `Def::Method`, whereas the lint pass was only matching on `Def::Fn`. (One could argue that those def-names are thereby misleading—must-use for `self`-ful methods have always worked—but documenting or reworking that can be left to another day.)

r? @varkor
......@@ -867,7 +867,7 @@ impl<T: ?Sized> Clone for Rc<T> {
///
/// let five = Rc::new(5);
///
/// Rc::clone(&five);
/// let _ = Rc::clone(&five);
/// ```
#[inline]
fn clone(&self) -> Rc<T> {
......@@ -1304,7 +1304,7 @@ impl<T: ?Sized> Clone for Weak<T> {
///
/// let weak_five = Rc::downgrade(&Rc::new(5));
///
/// Weak::clone(&weak_five);
/// let _ = Weak::clone(&weak_five);
/// ```
#[inline]
fn clone(&self) -> Weak<T> {
......
......@@ -713,7 +713,7 @@ impl<T: ?Sized> Clone for Arc<T> {
///
/// let five = Arc::new(5);
///
/// Arc::clone(&five);
/// let _ = Arc::clone(&five);
/// ```
#[inline]
fn clone(&self) -> Arc<T> {
......@@ -1135,7 +1135,7 @@ impl<T: ?Sized> Clone for Weak<T> {
///
/// let weak_five = Arc::downgrade(&Arc::new(5));
///
/// Weak::clone(&weak_five);
/// let _ = Weak::clone(&weak_five);
/// ```
#[inline]
fn clone(&self) -> Weak<T> {
......
......@@ -80,10 +80,11 @@ fn check_stmt(&mut self, cx: &LateContext, s: &hir::Stmt) {
match callee.node {
hir::ExprKind::Path(ref qpath) => {
let def = cx.tables.qpath_def(qpath, callee.hir_id);
if let Def::Fn(_) = def {
Some(def)
} else { // `Def::Local` if it was a closure, for which we
None // do not currently support must-use linting
match def {
Def::Fn(_) | Def::Method(_) => Some(def),
// `Def::Local` if it was a closure, for which we
// do not currently support must-use linting
_ => None
}
},
_ => None
......
......@@ -12,7 +12,7 @@
fn check<Clone>(_c: Clone) {
fn check2() {
<() as std::clone::Clone>::clone(&());
let _ = <() as std::clone::Clone>::clone(&());
}
check2();
}
......
......@@ -22,6 +22,11 @@ impl MyStruct {
fn need_to_use_this_method_value(&self) -> usize {
self.n
}
#[must_use]
fn need_to_use_this_associated_function_value() -> isize {
-1
}
}
trait EvenNature {
......@@ -66,6 +71,9 @@ fn main() {
m.is_even(); // trait method!
//~^ WARN unused return value
MyStruct::need_to_use_this_associated_function_value();
//~^ WARN unused return value
m.replace(3); // won't warn (annotation needs to be in trait definition)
// comparison methods are `must_use`
......
warning: unused return value of `need_to_use_this_value` which must be used
--> $DIR/fn_must_use.rs:60:5
--> $DIR/fn_must_use.rs:65:5
|
LL | need_to_use_this_value(); //~ WARN unused return value
| ^^^^^^^^^^^^^^^^^^^^^^^^^
......@@ -12,39 +12,45 @@ LL | #![warn(unused_must_use)]
= note: it's important
warning: unused return value of `MyStruct::need_to_use_this_method_value` which must be used
--> $DIR/fn_must_use.rs:65:5
--> $DIR/fn_must_use.rs:70:5
|
LL | m.need_to_use_this_method_value(); //~ WARN unused return value
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: unused return value of `EvenNature::is_even` which must be used
--> $DIR/fn_must_use.rs:66:5
--> $DIR/fn_must_use.rs:71:5
|
LL | m.is_even(); // trait method!
| ^^^^^^^^^^^^
|
= note: no side effects
warning: unused return value of `MyStruct::need_to_use_this_associated_function_value` which must be used
--> $DIR/fn_must_use.rs:74:5
|
LL | MyStruct::need_to_use_this_associated_function_value();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: unused return value of `std::cmp::PartialEq::eq` which must be used
--> $DIR/fn_must_use.rs:72:5
--> $DIR/fn_must_use.rs:80:5
|
LL | 2.eq(&3); //~ WARN unused return value
| ^^^^^^^^^
warning: unused return value of `std::cmp::PartialEq::eq` which must be used
--> $DIR/fn_must_use.rs:73:5
--> $DIR/fn_must_use.rs:81:5
|
LL | m.eq(&n); //~ WARN unused return value
| ^^^^^^^^^
warning: unused comparison which must be used
--> $DIR/fn_must_use.rs:76:5
--> $DIR/fn_must_use.rs:84:5
|
LL | 2 == 3; //~ WARN unused comparison
| ^^^^^^
warning: unused comparison which must be used
--> $DIR/fn_must_use.rs:77:5
--> $DIR/fn_must_use.rs:85:5
|
LL | m == n; //~ WARN unused comparison
| ^^^^^^
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册