Skip to content

Commit 451fd5f

Browse files
author
Donald Robertson
committed
Extracting arguments to format to pass directly to panic when appropriate
1 parent 3240474 commit 451fd5f

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

clippy_lints/src/methods.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,9 +1022,47 @@ fn lint_expect_fun_call(cx: &LateContext, expr: &hir::Expr, method_span: Span, n
10221022
}
10231023

10241024
let closure = if match_type(cx, self_type, &paths::OPTION) { "||" } else { "|_|" };
1025+
let span_replace_word = method_span.with_hi(span.hi());
1026+
1027+
if let hir::ExprAddrOf(_, ref addr_of) = arg.node {
1028+
if let hir::ExprCall(ref _inner_fun, ref inner_args) = addr_of.node {
1029+
// TODO: check if inner_fun is call to format!
1030+
if inner_args.len() == 1 {
1031+
if let hir::ExprCall(_, ref format_args) = inner_args[0].node {
1032+
let args_len = format_args.len();
1033+
let args: Vec<String> = format_args
1034+
.into_iter()
1035+
.take(args_len - 1)
1036+
.map(|a| {
1037+
if let hir::ExprAddrOf(_, ref format_arg) = a.node {
1038+
if let hir::ExprMatch(ref format_arg_expr, _, _) = format_arg.node {
1039+
if let hir::ExprTup(ref format_arg_expr_tup) = format_arg_expr.node {
1040+
return snippet(cx, format_arg_expr_tup[0].span, "..").into_owned();
1041+
}
1042+
}
1043+
};
1044+
snippet(cx, a.span, "..").into_owned()
1045+
})
1046+
.collect();
1047+
1048+
let sugg = args.join(", ");
1049+
1050+
span_lint_and_sugg(
1051+
cx,
1052+
EXPECT_FUN_CALL,
1053+
span_replace_word,
1054+
&format!("use of `{}` followed by a function call", name),
1055+
"try this",
1056+
format!("unwrap_or_else({} panic!({}))", closure, sugg),
1057+
);
1058+
1059+
return;
1060+
}
1061+
}
1062+
}
1063+
}
10251064

10261065
let sugg: Cow<_> = snippet(cx, arg.span, "..");
1027-
let span_replace_word = method_span.with_hi(span.hi());
10281066

10291067
span_lint_and_sugg(
10301068
cx,

tests/ui/methods.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ error: use of `expect` followed by a function call
427427
--> $DIR/methods.rs:365:26
428428
|
429429
365 | 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!("Error {}: fake error", error_code))`
431431
|
432432
= note: `-D expect-fun-call` implied by `-D warnings`
433433

@@ -441,7 +441,7 @@ error: use of `expect` followed by a function call
441441
--> $DIR/methods.rs:378:25
442442
|
443443
378 | with_err_and_format.expect(&format!("Error {}: fake error", error_code));
444-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| panic!(&format!("Error {}: fake error", error_code)))`
444+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| panic!("Error {}: fake error", error_code))`
445445

446446
error: use of `expect` followed by a function call
447447
--> $DIR/methods.rs:381:25

0 commit comments

Comments
 (0)