From 1559f8bf34a3cf4035228a6194369ee3f1829728 Mon Sep 17 00:00:00 2001 From: Michael Wright Date: Wed, 18 Dec 2019 21:54:37 +0200 Subject: [PATCH] Fix `expect_fun_call` false negative on references Closes #4912 --- clippy_lints/src/methods/mod.rs | 2 +- tests/ui/expect_fun_call.fixed | 7 +++++++ tests/ui/expect_fun_call.rs | 7 +++++++ tests/ui/expect_fun_call.stderr | 8 +++++++- 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index ca62e7ea9d2..8c4f8c56c76 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -1595,7 +1595,7 @@ fn lint_expect_fun_call(cx: &LateContext<'_, '_>, expr: &hir::Expr, method_span: return; } - let receiver_type = cx.tables.expr_ty(&args[0]); + let receiver_type = cx.tables.expr_ty_adjusted(&args[0]); let closure_args = if match_type(cx, receiver_type, &paths::OPTION) { "||" } else if match_type(cx, receiver_type, &paths::RESULT) { diff --git a/tests/ui/expect_fun_call.fixed b/tests/ui/expect_fun_call.fixed index e111ee3dfed..f3d8a941a92 100644 --- a/tests/ui/expect_fun_call.fixed +++ b/tests/ui/expect_fun_call.fixed @@ -84,4 +84,11 @@ fn main() { //Issue #3839 Some(true).unwrap_or_else(|| panic!("key {}, {}", 1, 2)); + + //Issue #4912 - the receiver is a &Option + { + let opt = Some(1); + let opt_ref = &opt; + opt_ref.unwrap_or_else(|| panic!("{:?}", opt_ref)); + } } diff --git a/tests/ui/expect_fun_call.rs b/tests/ui/expect_fun_call.rs index 891ec883120..60bbaa89d42 100644 --- a/tests/ui/expect_fun_call.rs +++ b/tests/ui/expect_fun_call.rs @@ -84,4 +84,11 @@ fn main() { //Issue #3839 Some(true).expect(&format!("key {}, {}", 1, 2)); + + //Issue #4912 - the receiver is a &Option + { + let opt = Some(1); + let opt_ref = &opt; + opt_ref.expect(&format!("{:?}", opt_ref)); + } } diff --git a/tests/ui/expect_fun_call.stderr b/tests/ui/expect_fun_call.stderr index bb16fabd973..a492e2df89d 100644 --- a/tests/ui/expect_fun_call.stderr +++ b/tests/ui/expect_fun_call.stderr @@ -66,5 +66,11 @@ error: use of `expect` followed by a function call LL | Some(true).expect(&format!("key {}, {}", 1, 2)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!("key {}, {}", 1, 2))` -error: aborting due to 11 previous errors +error: use of `expect` followed by a function call + --> $DIR/expect_fun_call.rs:92:17 + | +LL | opt_ref.expect(&format!("{:?}", opt_ref)); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!("{:?}", opt_ref))` + +error: aborting due to 12 previous errors