]> git.lizzy.rs Git - rust.git/commitdiff
in which the fn-must-use codepath is prevented from panicking on closure
authorZack M. Davis <code@zackmdavis.net>
Sat, 10 Mar 2018 22:26:33 +0000 (14:26 -0800)
committerZack M. Davis <code@zackmdavis.net>
Sun, 29 Apr 2018 03:32:49 +0000 (20:32 -0700)
The must-use lint needs the DefId of called functions and method
receivers in order to look for a `#[must_use]` attribute, but this would
ICE (!) if a called function was actually a closure (with a non-unit
return value). Instead, let's be specific that we want a `Def::Fn`,
rather than blithely assuming that we can get the DefId of a qpath.

Supporting must-use closures doesn't seem like a priority, but could
conceivably be added in the future if desired (conditional on the
statement and expression attributes (#15701) story being amicable).

src/librustc_lint/unused.rs

index 5ec8305de788c3ecb4fa4fa84ca4a05b6466c47c..c32e9cdce0e61409ed46f52d28bcf1420290fdb5 100644 (file)
@@ -8,6 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+use rustc::hir::def::Def;
 use rustc::hir::def_id::DefId;
 use rustc::ty;
 use rustc::ty::adjustment;
@@ -77,7 +78,12 @@ fn check_stmt(&mut self, cx: &LateContext, s: &hir::Stmt) {
                 hir::ExprCall(ref callee, _) => {
                     match callee.node {
                         hir::ExprPath(ref qpath) => {
-                            Some(cx.tables.qpath_def(qpath, callee.hir_id))
+                            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
+                            }
                         },
                         _ => None
                     }