Skip to content

Commit fe6ad91

Browse files
committed
Merge pull request #919 from oli-obk/fix/917
don't suggest closures over constants
2 parents 8fa68f1 + 610883b commit fe6ad91

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/methods.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use rustc::hir::*;
22
use rustc::lint::*;
33
use rustc::middle::const_val::ConstVal;
4+
use rustc::middle::const_qualif::ConstQualif;
45
use rustc::ty::subst::{Subst, TypeSpace};
56
use rustc::ty;
67
use rustc_const_eval::EvalHint::ExprTypeChecked;
@@ -502,6 +503,13 @@ fn lint_or_fun_call(cx: &LateContext, expr: &Expr, name: &str, args: &[P<Expr>])
502503
/// Check for `*or(foo())`.
503504
fn check_general_case(cx: &LateContext, name: &str, fun: &Expr, self_expr: &Expr, arg: &Expr, or_has_args: bool,
504505
span: Span) {
506+
// don't lint for constant values
507+
// FIXME: can we `expect` here instead of match?
508+
if let Some(qualif) = cx.tcx.const_qualif_map.borrow().get(&arg.id) {
509+
if !qualif.contains(ConstQualif::NOT_CONST) {
510+
return;
511+
}
512+
}
505513
// (path, fn_has_argument, methods)
506514
let know_types: &[(&[_], _, &[_], _)] = &[(&paths::BTREEMAP_ENTRY, false, &["or_insert"], "with"),
507515
(&paths::HASHMAP_ENTRY, false, &["or_insert"], "with"),

tests/compile-fail/methods.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![feature(plugin)]
2+
#![feature(const_fn)]
23
#![plugin(clippy)]
34

45
#![deny(clippy, clippy_pedantic)]
@@ -227,8 +228,20 @@ fn or_fun_call() {
227228
fn new() -> Foo { Foo }
228229
}
229230

231+
enum Enum {
232+
A(i32),
233+
}
234+
235+
const fn make_const(i: i32) -> i32 { i }
236+
230237
fn make<T>() -> T { unimplemented!(); }
231238

239+
let with_enum = Some(Enum::A(1));
240+
with_enum.unwrap_or(Enum::A(5));
241+
242+
let with_const_fn = Some(1);
243+
with_const_fn.unwrap_or(make_const(5));
244+
232245
let with_constructor = Some(vec![1]);
233246
with_constructor.unwrap_or(make());
234247
//~^ERROR use of `unwrap_or`

0 commit comments

Comments
 (0)