Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't suggest closures over constants #919

Merged
merged 1 commit into from
May 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/methods.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -502,6 +503,13 @@ fn lint_or_fun_call(cx: &LateContext, expr: &Expr, name: &str, args: &[P<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"),
Expand Down
13 changes: 13 additions & 0 deletions tests/compile-fail/methods.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![feature(plugin)]
#![feature(const_fn)]
#![plugin(clippy)]

#![deny(clippy, clippy_pedantic)]
Expand Down Expand Up @@ -227,8 +228,20 @@ fn or_fun_call() {
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`
Expand Down