From fe8c9d596543955e0513b239669916b55693dff4 Mon Sep 17 00:00:00 2001 From: Donald Robertson Date: Tue, 29 May 2018 09:46:14 +0200 Subject: [PATCH] Ensuring correct lint message is output for Option and Result type --- clippy_lints/src/methods.rs | 11 +++++++++-- tests/ui/methods.stderr | 4 ++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/clippy_lints/src/methods.rs b/clippy_lints/src/methods.rs index b3c4bad251b..696bd02cf55 100644 --- a/clippy_lints/src/methods.rs +++ b/clippy_lints/src/methods.rs @@ -998,6 +998,7 @@ fn check_general_case( cx: &LateContext, name: &str, method_span: Span, + self_expr: &hir::Expr, arg: &hir::Expr, span: Span, ) { @@ -1005,6 +1006,12 @@ fn check_general_case( return; } + let self_ty = cx.tables.expr_ty(self_expr); + let closure = match match_type(cx, self_ty, &paths::OPTION) { + true => "||", + false => "|_|", + }; + // don't lint for constant values let owner_def = cx.tcx.hir.get_parent_did(arg.id); let promotable = cx.tcx.rvalue_promotable_map(owner_def).contains(&arg.hir_id.local_id); @@ -1021,14 +1028,14 @@ fn check_general_case( span_replace_word, &format!("use of `{}` followed by a function call", name), "try this", - format!("unwrap_or_else(|_| panic!({}))", sugg), + format!("unwrap_or_else({} panic!({}))", closure, sugg), ); } if args.len() == 2 { match args[1].node { hir::ExprLit(_) => {}, - _ => check_general_case(cx, name, method_span, &args[1], expr.span), + _ => check_general_case(cx, name, method_span, &args[0], &args[1], expr.span), } } } diff --git a/tests/ui/methods.stderr b/tests/ui/methods.stderr index 2fe374f3541..f3c608cb715 100644 --- a/tests/ui/methods.stderr +++ b/tests/ui/methods.stderr @@ -427,7 +427,7 @@ error: use of `expect` followed by a function call --> $DIR/methods.rs:355:26 | 355 | with_none_and_format.expect(&format!("Error {}: fake error", error_code)); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| panic!(&format!("Error {}: fake error", error_code)))` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!(&format!("Error {}: fake error", error_code)))` | = note: `-D expect-fun-call` implied by `-D warnings` @@ -435,7 +435,7 @@ error: use of `expect` followed by a function call --> $DIR/methods.rs:358:26 | 358 | with_none_and_as_str.expect(format!("Error {}: fake error", error_code).as_str()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| panic!(format!("Error {}: fake error", error_code).as_str()))` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!(format!("Error {}: fake error", error_code).as_str()))` error: use of `expect` followed by a function call --> $DIR/methods.rs:368:25