From d2d62de841404e598c77dd46bdff79a0545e619f Mon Sep 17 00:00:00 2001 From: flip1995 Date: Mon, 25 Nov 2019 13:56:24 +0100 Subject: [PATCH] Rustup to rust-lang/rust#64856 --- clippy_lints/src/methods/mod.rs | 43 ++++++++++++++++++--------------- tests/ui/or_fun_call.fixed | 2 +- tests/ui/or_fun_call.stderr | 8 +----- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index d2f71760a05..d1d5b962bdd 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -1610,30 +1610,35 @@ fn lint_expect_fun_call(cx: &LateContext<'_, '_>, expr: &hir::Expr, method_span: let mut applicability = Applicability::MachineApplicable; //Special handling for `format!` as arg_root - if let hir::ExprKind::Call(ref inner_fun, ref inner_args) = arg_root.kind { - if is_expn_of(inner_fun.span, "format").is_some() && inner_args.len() == 1 { - if let hir::ExprKind::Call(_, format_args) = &inner_args[0].kind { - let fmt_spec = &format_args[0]; - let fmt_args = &format_args[1]; + if_chain! { + if let hir::ExprKind::Block(block, None) = &arg_root.kind; + if block.stmts.len() == 1; + if let hir::StmtKind::Local(local) = &block.stmts[0].kind; + if let Some(arg_root) = &local.init; + if let hir::ExprKind::Call(ref inner_fun, ref inner_args) = arg_root.kind; + if is_expn_of(inner_fun.span, "format").is_some() && inner_args.len() == 1; + if let hir::ExprKind::Call(_, format_args) = &inner_args[0].kind; + then { + let fmt_spec = &format_args[0]; + let fmt_args = &format_args[1]; - let mut args = vec![snippet(cx, fmt_spec.span, "..").into_owned()]; + let mut args = vec![snippet(cx, fmt_spec.span, "..").into_owned()]; - args.extend(generate_format_arg_snippet(cx, fmt_args, &mut applicability)); + args.extend(generate_format_arg_snippet(cx, fmt_args, &mut applicability)); - let sugg = args.join(", "); + let sugg = args.join(", "); - span_lint_and_sugg( - cx, - EXPECT_FUN_CALL, - span_replace_word, - &format!("use of `{}` followed by a function call", name), - "try this", - format!("unwrap_or_else({} panic!({}))", closure_args, sugg), - applicability, - ); + span_lint_and_sugg( + cx, + EXPECT_FUN_CALL, + span_replace_word, + &format!("use of `{}` followed by a function call", name), + "try this", + format!("unwrap_or_else({} panic!({}))", closure_args, sugg), + applicability, + ); - return; - } + return; } } diff --git a/tests/ui/or_fun_call.fixed b/tests/ui/or_fun_call.fixed index 198d9862e3f..cf2a42fa485 100644 --- a/tests/ui/or_fun_call.fixed +++ b/tests/ui/or_fun_call.fixed @@ -71,7 +71,7 @@ fn or_fun_call() { let opt = Some(1); let hello = "Hello"; - let _ = opt.ok_or_else(|| format!("{} world.", hello)); + let _ = opt.ok_or(format!("{} world.", hello)); } struct Foo(u8); diff --git a/tests/ui/or_fun_call.stderr b/tests/ui/or_fun_call.stderr index 7d60cba3831..cb92892b8e1 100644 --- a/tests/ui/or_fun_call.stderr +++ b/tests/ui/or_fun_call.stderr @@ -72,17 +72,11 @@ error: use of `unwrap_or` followed by a function call LL | let _ = stringy.unwrap_or("".to_owned()); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| "".to_owned())` -error: use of `ok_or` followed by a function call - --> $DIR/or_fun_call.rs:74:17 - | -LL | let _ = opt.ok_or(format!("{} world.", hello)); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `ok_or_else(|| format!("{} world.", hello))` - error: use of `or` followed by a function call --> $DIR/or_fun_call.rs:95:35 | LL | let _ = Some("a".to_string()).or(Some("b".to_string())); | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_else(|| Some("b".to_string()))` -error: aborting due to 14 previous errors +error: aborting due to 13 previous errors