Skip to content

Commit 37c532c

Browse files
committed
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.
1 parent 9f97a0b commit 37c532c

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

compiler/rustc_lint/src/non_fmt_panic.rs

+33-15
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
9393
if arg_macro.map_or(false, |id| cx.tcx.is_diagnostic_item(sym::format_macro, id)) {
9494
// A case of `panic!(format!(..))`.
9595
l.note("the panic!() macro supports formatting, so there's no need for the format!() macro here");
96-
if let Some(inner) = find_inner_span(cx, arg_span) {
96+
if let Some((open, close, _)) = find_delimiters(cx, arg_span) {
9797
l.multipart_suggestion(
9898
"remove the `format!(..)` macro call",
9999
vec![
100-
(arg_span.until(inner), "".into()),
101-
(inner.between(arg_span.shrink_to_hi()), "".into()),
100+
(arg_span.until(open.shrink_to_hi()), "".into()),
101+
(close.until(arg_span.shrink_to_hi()), "".into()),
102102
],
103103
Applicability::MachineApplicable,
104104
);
@@ -111,12 +111,20 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
111111
Applicability::MaybeIncorrect,
112112
);
113113
if panic == sym::std_panic_macro {
114-
l.span_suggestion_verbose(
115-
span.until(arg_span),
116-
"or use std::panic::panic_any instead",
117-
"std::panic::panic_any(".into(),
118-
Applicability::MachineApplicable,
119-
);
114+
if let Some((open, close, del)) = find_delimiters(cx, span) {
115+
l.multipart_suggestion(
116+
"or use std::panic::panic_any instead",
117+
if del == '(' {
118+
vec![(span.until(open), "std::panic::panic_any".into())]
119+
} else {
120+
vec![
121+
(span.until(open.shrink_to_hi()), "std::panic::panic_any(".into()),
122+
(close, ")".into()),
123+
]
124+
},
125+
Applicability::MachineApplicable,
126+
);
127+
}
120128
}
121129
}
122130
l.emit();
@@ -206,13 +214,23 @@ fn check_panic_str<'tcx>(
206214
}
207215
}
208216

209-
/// Given the span of `some_macro!(args)`, gives the span of `args`.
210-
fn find_inner_span<'tcx>(cx: &LateContext<'tcx>, span: Span) -> Option<Span> {
217+
/// Given the span of `some_macro!(args);`, gives the span of `(` and `)`,
218+
/// and the type of (opening) delimiter used.
219+
fn find_delimiters<'tcx>(cx: &LateContext<'tcx>, span: Span) -> Option<(Span, Span, char)> {
211220
let snippet = cx.sess().parse_sess.source_map().span_to_snippet(span).ok()?;
212-
Some(span.from_inner(InnerSpan {
213-
start: snippet.find(&['(', '{', '['][..])? + 1,
214-
end: snippet.rfind(&[')', '}', ']'][..])?,
215-
}))
221+
let (open, open_ch) = snippet.char_indices().find(|&(_, c)| "([{".contains(c))?;
222+
let close = snippet.rfind(|c| ")]}".contains(c))?;
223+
Some((
224+
span.from_inner(InnerSpan {
225+
start: open,
226+
end: open + 1,
227+
}),
228+
span.from_inner(InnerSpan {
229+
start: close,
230+
end: close + 1,
231+
}),
232+
open_ch,
233+
))
216234
}
217235

218236
fn panic_call<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>) -> (Span, Symbol) {

src/test/ui/non-fmt-panic.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ LL | panic!("{}", C);
9393
help: or use std::panic::panic_any instead
9494
|
9595
LL | std::panic::panic_any(C);
96-
| ^^^^^^^^^^^^^^^^^^^^^^
96+
| ^^^^^^^^^^^^^^^^^^^^^
9797

9898
warning: panic message is not a string literal
9999
--> $DIR/non-fmt-panic.rs:20:12
@@ -109,7 +109,7 @@ LL | panic!("{}", S);
109109
help: or use std::panic::panic_any instead
110110
|
111111
LL | std::panic::panic_any(S);
112-
| ^^^^^^^^^^^^^^^^^^^^^^
112+
| ^^^^^^^^^^^^^^^^^^^^^
113113

114114
warning: panic message is not a string literal
115115
--> $DIR/non-fmt-panic.rs:21:17
@@ -125,7 +125,7 @@ LL | std::panic!("{}", 123);
125125
help: or use std::panic::panic_any instead
126126
|
127127
LL | std::panic::panic_any(123);
128-
| ^^^^^^^^^^^^^^^^^^^^^^
128+
| ^^^^^^^^^^^^^^^^^^^^^
129129

130130
warning: panic message is not a string literal
131131
--> $DIR/non-fmt-panic.rs:22:18
@@ -197,7 +197,7 @@ LL | panic!("{}", a!());
197197
help: or use std::panic::panic_any instead
198198
|
199199
LL | std::panic::panic_any(a!());
200-
| ^^^^^^^^^^^^^^^^^^^^^^
200+
| ^^^^^^^^^^^^^^^^^^^^^
201201

202202
warning: panic message is not a string literal
203203
--> $DIR/non-fmt-panic.rs:38:12

0 commit comments

Comments
 (0)