Skip to content

Commit fe8c9d5

Browse files
author
Donald Robertson
committed
Ensuring correct lint message is output for Option and Result type
1 parent 2b36017 commit fe8c9d5

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

clippy_lints/src/methods.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -998,13 +998,20 @@ fn lint_expect_fun_call(cx: &LateContext, expr: &hir::Expr, method_span: Span, n
998998
cx: &LateContext,
999999
name: &str,
10001000
method_span: Span,
1001+
self_expr: &hir::Expr,
10011002
arg: &hir::Expr,
10021003
span: Span,
10031004
) {
10041005
if name != "expect" {
10051006
return;
10061007
}
10071008

1009+
let self_ty = cx.tables.expr_ty(self_expr);
1010+
let closure = match match_type(cx, self_ty, &paths::OPTION) {
1011+
true => "||",
1012+
false => "|_|",
1013+
};
1014+
10081015
// don't lint for constant values
10091016
let owner_def = cx.tcx.hir.get_parent_did(arg.id);
10101017
let promotable = cx.tcx.rvalue_promotable_map(owner_def).contains(&arg.hir_id.local_id);
@@ -1021,14 +1028,14 @@ fn lint_expect_fun_call(cx: &LateContext, expr: &hir::Expr, method_span: Span, n
10211028
span_replace_word,
10221029
&format!("use of `{}` followed by a function call", name),
10231030
"try this",
1024-
format!("unwrap_or_else(|_| panic!({}))", sugg),
1031+
format!("unwrap_or_else({} panic!({}))", closure, sugg),
10251032
);
10261033
}
10271034

10281035
if args.len() == 2 {
10291036
match args[1].node {
10301037
hir::ExprLit(_) => {},
1031-
_ => check_general_case(cx, name, method_span, &args[1], expr.span),
1038+
_ => check_general_case(cx, name, method_span, &args[0], &args[1], expr.span),
10321039
}
10331040
}
10341041
}

tests/ui/methods.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,15 +427,15 @@ error: use of `expect` followed by a function call
427427
--> $DIR/methods.rs:355:26
428428
|
429429
355 | with_none_and_format.expect(&format!("Error {}: fake error", error_code));
430-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| panic!(&format!("Error {}: fake error", error_code)))`
430+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!(&format!("Error {}: fake error", error_code)))`
431431
|
432432
= note: `-D expect-fun-call` implied by `-D warnings`
433433

434434
error: use of `expect` followed by a function call
435435
--> $DIR/methods.rs:358:26
436436
|
437437
358 | with_none_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
438-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| panic!(format!("Error {}: fake error", error_code).as_str()))`
438+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!(format!("Error {}: fake error", error_code).as_str()))`
439439

440440
error: use of `expect` followed by a function call
441441
--> $DIR/methods.rs:368:25

0 commit comments

Comments
 (0)