diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index ca62e7ea9d2b..8c4f8c56c766 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 e111ee3dfeda..f3d8a941a92b 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 891ec883120c..60bbaa89d428 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 bb16fabd973b..a492e2df89d4 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