Skip to content

Commit 584f95f

Browse files
committed
Auto merge of #4915 - mikerite:fix-4912-2, r=phansch
Fix `expect_fun_call` false negative on references Closes #4912 changelog: Fix `expect_fun_call` false negative on references
2 parents d7c7056 + 1559f8b commit 584f95f

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1595,7 +1595,7 @@ fn lint_expect_fun_call(cx: &LateContext<'_, '_>, expr: &hir::Expr, method_span:
15951595
return;
15961596
}
15971597

1598-
let receiver_type = cx.tables.expr_ty(&args[0]);
1598+
let receiver_type = cx.tables.expr_ty_adjusted(&args[0]);
15991599
let closure_args = if match_type(cx, receiver_type, &paths::OPTION) {
16001600
"||"
16011601
} else if match_type(cx, receiver_type, &paths::RESULT) {

tests/ui/expect_fun_call.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,11 @@ fn main() {
8484

8585
//Issue #3839
8686
Some(true).unwrap_or_else(|| panic!("key {}, {}", 1, 2));
87+
88+
//Issue #4912 - the receiver is a &Option
89+
{
90+
let opt = Some(1);
91+
let opt_ref = &opt;
92+
opt_ref.unwrap_or_else(|| panic!("{:?}", opt_ref));
93+
}
8794
}

tests/ui/expect_fun_call.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,11 @@ fn main() {
8484

8585
//Issue #3839
8686
Some(true).expect(&format!("key {}, {}", 1, 2));
87+
88+
//Issue #4912 - the receiver is a &Option
89+
{
90+
let opt = Some(1);
91+
let opt_ref = &opt;
92+
opt_ref.expect(&format!("{:?}", opt_ref));
93+
}
8794
}

tests/ui/expect_fun_call.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,11 @@ error: use of `expect` followed by a function call
6666
LL | Some(true).expect(&format!("key {}, {}", 1, 2));
6767
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!("key {}, {}", 1, 2))`
6868

69-
error: aborting due to 11 previous errors
69+
error: use of `expect` followed by a function call
70+
--> $DIR/expect_fun_call.rs:92:17
71+
|
72+
LL | opt_ref.expect(&format!("{:?}", opt_ref));
73+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!("{:?}", opt_ref))`
74+
75+
error: aborting due to 12 previous errors
7076

0 commit comments

Comments
 (0)