Skip to content

Commit

Permalink
Fix expect_fun_call false negative on references
Browse files Browse the repository at this point in the history
Closes #4912
  • Loading branch information
Michael Wright committed Dec 19, 2019
1 parent c62396d commit 1559f8b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/expect_fun_call.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
7 changes: 7 additions & 0 deletions tests/ui/expect_fun_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
8 changes: 7 additions & 1 deletion tests/ui/expect_fun_call.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1559f8b

Please sign in to comment.