Merge pull request #1444 from Manishearth/or_else_method

Extend or_fun_call to also cover methods
This commit is contained in:
Oliver Schneider 2017-01-15 14:38:51 +01:00 committed by GitHub
commit 219da6d736
3 changed files with 28 additions and 15 deletions

View File

@ -724,7 +724,7 @@ fn lint_or_fun_call(cx: &LateContext, expr: &hir::Expr, name: &str, args: &[hir:
fn check_general_case(
cx: &LateContext,
name: &str,
fun: &hir::Expr,
fun_span: Span,
self_expr: &hir::Expr,
arg: &hir::Expr,
or_has_args: bool,
@ -765,7 +765,7 @@ fn lint_or_fun_call(cx: &LateContext, expr: &hir::Expr, name: &str, args: &[hir:
let sugg: Cow<_> = match (fn_has_arguments, !or_has_args) {
(true, _) => format!("|_| {}", snippet(cx, arg.span, "..")).into(),
(false, false) => format!("|| {}", snippet(cx, arg.span, "..")).into(),
(false, true) => snippet(cx, fun.span, ".."),
(false, true) => snippet(cx, fun_span, ".."),
};
span_lint_and_then(cx,
@ -780,11 +780,17 @@ fn lint_or_fun_call(cx: &LateContext, expr: &hir::Expr, name: &str, args: &[hir:
}
if args.len() == 2 {
if let hir::ExprCall(ref fun, ref or_args) = args[1].node {
let or_has_args = !or_args.is_empty();
if !check_unwrap_or_default(cx, name, fun, &args[0], &args[1], or_has_args, expr.span) {
check_general_case(cx, name, fun, &args[0], &args[1], or_has_args, expr.span);
}
match args[1].node {
hir::ExprCall(ref fun, ref or_args) => {
let or_has_args = !or_args.is_empty();
if !check_unwrap_or_default(cx, name, fun, &args[0], &args[1], or_has_args, expr.span) {
check_general_case(cx, name, fun.span, &args[0], &args[1], or_has_args, expr.span);
}
},
hir::ExprMethodCall(fun, _, ref or_args) => {
check_general_case(cx, name, fun.span, &args[0], &args[1], !or_args.is_empty(), expr.span)
},
_ => {},
}
}
}

View File

@ -234,13 +234,15 @@ pub fn main() {
} else {
option_env!("SYSROOT")
.map(|s| s.to_owned())
.or(Command::new("rustc")
.arg("--print")
.arg("sysroot")
.output()
.ok()
.and_then(|out| String::from_utf8(out.stdout).ok())
.map(|s| s.trim().to_owned()))
.or_else(|| {
Command::new("rustc")
.arg("--print")
.arg("sysroot")
.output()
.ok()
.and_then(|out| String::from_utf8(out.stdout).ok())
.map(|s| s.trim().to_owned())
})
.expect("need to specify SYSROOT env var during clippy compilation, or use rustup or multirust")
};

View File

@ -127,7 +127,6 @@ fn option_methods() {
);
// macro case
let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0); // should not lint
}
/// Struct to generate false positives for things with .iter()
@ -340,6 +339,12 @@ fn or_fun_call() {
//~^ERROR use of `or_insert` followed by a function call
//~|HELP try this
//~|SUGGESTION btree.entry(42).or_insert_with(String::new);
let stringy = Some(String::from(""));
let _ = stringy.unwrap_or("".to_owned());
//~^ERROR use of `unwrap_or`
//~|HELP try this
//~|SUGGESTION stringy.unwrap_or_else(|| "".to_owned());
}
/// Checks implementation of `ITER_NTH` lint