]> git.lizzy.rs Git - rust.git/commitdiff
don't suggest closures over constants
authorOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Thu, 12 May 2016 08:23:06 +0000 (10:23 +0200)
committerOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Thu, 12 May 2016 08:23:06 +0000 (10:23 +0200)
fixes #917

src/methods.rs
tests/compile-fail/methods.rs

index c9cc81484cd2295be98858473a2b3d98a7d839b8..b47ae924bf012440a34fcf0e0fca0a5b2a2fa5ae 100644 (file)
@@ -1,6 +1,7 @@
 use rustc::hir::*;
 use rustc::lint::*;
 use rustc::middle::const_val::ConstVal;
+use rustc::middle::const_qualif::ConstQualif;
 use rustc::ty::subst::{Subst, TypeSpace};
 use rustc::ty;
 use rustc_const_eval::EvalHint::ExprTypeChecked;
@@ -502,6 +503,13 @@ fn check_unwrap_or_default(cx: &LateContext, name: &str, fun: &Expr, self_expr:
     /// Check for `*or(foo())`.
     fn check_general_case(cx: &LateContext, name: &str, fun: &Expr, self_expr: &Expr, arg: &Expr, or_has_args: bool,
                           span: Span) {
+        // don't lint for constant values
+        // FIXME: can we `expect` here instead of match?
+        if let Some(qualif) = cx.tcx.const_qualif_map.borrow().get(&arg.id) {
+            if !qualif.contains(ConstQualif::NOT_CONST) {
+                return;
+            }
+        }
         // (path, fn_has_argument, methods)
         let know_types: &[(&[_], _, &[_], _)] = &[(&paths::BTREEMAP_ENTRY, false, &["or_insert"], "with"),
                                                   (&paths::HASHMAP_ENTRY, false, &["or_insert"], "with"),
index 7503cb50746148bee11cd257aa934d5965f4c014..0a943840e17a100102c8ac8b8ce2284867c14331 100644 (file)
@@ -1,4 +1,5 @@
 #![feature(plugin)]
+#![feature(const_fn)]
 #![plugin(clippy)]
 
 #![deny(clippy, clippy_pedantic)]
@@ -227,8 +228,20 @@ impl Foo {
         fn new() -> Foo { Foo }
     }
 
+    enum Enum {
+        A(i32),
+    }
+
+    const fn make_const(i: i32) -> i32 { i }
+
     fn make<T>() -> T { unimplemented!(); }
 
+    let with_enum = Some(Enum::A(1));
+    with_enum.unwrap_or(Enum::A(5));
+
+    let with_const_fn = Some(1);
+    with_const_fn.unwrap_or(make_const(5));
+
     let with_constructor = Some(vec![1]);
     with_constructor.unwrap_or(make());
     //~^ERROR use of `unwrap_or`