Suggest correct replacement for panic![123].
Before this change, the suggestion was `std::panic::panic_any(123]`, changing the opening brace but not the closing one.
This commit is contained in:
parent
9f97a0b364
commit
37c532c010
2 changed files with 37 additions and 19 deletions
|
@ -93,12 +93,12 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
|
||||||
if arg_macro.map_or(false, |id| cx.tcx.is_diagnostic_item(sym::format_macro, id)) {
|
if arg_macro.map_or(false, |id| cx.tcx.is_diagnostic_item(sym::format_macro, id)) {
|
||||||
// A case of `panic!(format!(..))`.
|
// A case of `panic!(format!(..))`.
|
||||||
l.note("the panic!() macro supports formatting, so there's no need for the format!() macro here");
|
l.note("the panic!() macro supports formatting, so there's no need for the format!() macro here");
|
||||||
if let Some(inner) = find_inner_span(cx, arg_span) {
|
if let Some((open, close, _)) = find_delimiters(cx, arg_span) {
|
||||||
l.multipart_suggestion(
|
l.multipart_suggestion(
|
||||||
"remove the `format!(..)` macro call",
|
"remove the `format!(..)` macro call",
|
||||||
vec![
|
vec![
|
||||||
(arg_span.until(inner), "".into()),
|
(arg_span.until(open.shrink_to_hi()), "".into()),
|
||||||
(inner.between(arg_span.shrink_to_hi()), "".into()),
|
(close.until(arg_span.shrink_to_hi()), "".into()),
|
||||||
],
|
],
|
||||||
Applicability::MachineApplicable,
|
Applicability::MachineApplicable,
|
||||||
);
|
);
|
||||||
|
@ -111,12 +111,20 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
|
||||||
Applicability::MaybeIncorrect,
|
Applicability::MaybeIncorrect,
|
||||||
);
|
);
|
||||||
if panic == sym::std_panic_macro {
|
if panic == sym::std_panic_macro {
|
||||||
l.span_suggestion_verbose(
|
if let Some((open, close, del)) = find_delimiters(cx, span) {
|
||||||
span.until(arg_span),
|
l.multipart_suggestion(
|
||||||
"or use std::panic::panic_any instead",
|
"or use std::panic::panic_any instead",
|
||||||
"std::panic::panic_any(".into(),
|
if del == '(' {
|
||||||
Applicability::MachineApplicable,
|
vec![(span.until(open), "std::panic::panic_any".into())]
|
||||||
);
|
} else {
|
||||||
|
vec![
|
||||||
|
(span.until(open.shrink_to_hi()), "std::panic::panic_any(".into()),
|
||||||
|
(close, ")".into()),
|
||||||
|
]
|
||||||
|
},
|
||||||
|
Applicability::MachineApplicable,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
l.emit();
|
l.emit();
|
||||||
|
@ -206,13 +214,23 @@ fn check_panic_str<'tcx>(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Given the span of `some_macro!(args)`, gives the span of `args`.
|
/// Given the span of `some_macro!(args);`, gives the span of `(` and `)`,
|
||||||
fn find_inner_span<'tcx>(cx: &LateContext<'tcx>, span: Span) -> Option<Span> {
|
/// and the type of (opening) delimiter used.
|
||||||
|
fn find_delimiters<'tcx>(cx: &LateContext<'tcx>, span: Span) -> Option<(Span, Span, char)> {
|
||||||
let snippet = cx.sess().parse_sess.source_map().span_to_snippet(span).ok()?;
|
let snippet = cx.sess().parse_sess.source_map().span_to_snippet(span).ok()?;
|
||||||
Some(span.from_inner(InnerSpan {
|
let (open, open_ch) = snippet.char_indices().find(|&(_, c)| "([{".contains(c))?;
|
||||||
start: snippet.find(&['(', '{', '['][..])? + 1,
|
let close = snippet.rfind(|c| ")]}".contains(c))?;
|
||||||
end: snippet.rfind(&[')', '}', ']'][..])?,
|
Some((
|
||||||
}))
|
span.from_inner(InnerSpan {
|
||||||
|
start: open,
|
||||||
|
end: open + 1,
|
||||||
|
}),
|
||||||
|
span.from_inner(InnerSpan {
|
||||||
|
start: close,
|
||||||
|
end: close + 1,
|
||||||
|
}),
|
||||||
|
open_ch,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn panic_call<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>) -> (Span, Symbol) {
|
fn panic_call<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>) -> (Span, Symbol) {
|
||||||
|
|
|
@ -93,7 +93,7 @@ LL | panic!("{}", C);
|
||||||
help: or use std::panic::panic_any instead
|
help: or use std::panic::panic_any instead
|
||||||
|
|
|
|
||||||
LL | std::panic::panic_any(C);
|
LL | std::panic::panic_any(C);
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
warning: panic message is not a string literal
|
warning: panic message is not a string literal
|
||||||
--> $DIR/non-fmt-panic.rs:20:12
|
--> $DIR/non-fmt-panic.rs:20:12
|
||||||
|
@ -109,7 +109,7 @@ LL | panic!("{}", S);
|
||||||
help: or use std::panic::panic_any instead
|
help: or use std::panic::panic_any instead
|
||||||
|
|
|
|
||||||
LL | std::panic::panic_any(S);
|
LL | std::panic::panic_any(S);
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
warning: panic message is not a string literal
|
warning: panic message is not a string literal
|
||||||
--> $DIR/non-fmt-panic.rs:21:17
|
--> $DIR/non-fmt-panic.rs:21:17
|
||||||
|
@ -125,7 +125,7 @@ LL | std::panic!("{}", 123);
|
||||||
help: or use std::panic::panic_any instead
|
help: or use std::panic::panic_any instead
|
||||||
|
|
|
|
||||||
LL | std::panic::panic_any(123);
|
LL | std::panic::panic_any(123);
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
warning: panic message is not a string literal
|
warning: panic message is not a string literal
|
||||||
--> $DIR/non-fmt-panic.rs:22:18
|
--> $DIR/non-fmt-panic.rs:22:18
|
||||||
|
@ -197,7 +197,7 @@ LL | panic!("{}", a!());
|
||||||
help: or use std::panic::panic_any instead
|
help: or use std::panic::panic_any instead
|
||||||
|
|
|
|
||||||
LL | std::panic::panic_any(a!());
|
LL | std::panic::panic_any(a!());
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
warning: panic message is not a string literal
|
warning: panic message is not a string literal
|
||||||
--> $DIR/non-fmt-panic.rs:38:12
|
--> $DIR/non-fmt-panic.rs:38:12
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue