Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
m-ou-se committed Feb 14, 2021
commit 37c532c010cddb8f9283fb50318d8c3816646063
48 changes: 33 additions & 15 deletions compiler/rustc_lint/src/non_fmt_panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
// A case of `panic!(format!(..))`.
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(
"remove the `format!(..)` macro call",
vec![
(arg_span.until(inner), "".into()),
(inner.between(arg_span.shrink_to_hi()), "".into()),
(arg_span.until(open.shrink_to_hi()), "".into()),
(close.until(arg_span.shrink_to_hi()), "".into()),
],
Applicability::MachineApplicable,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this doesn't actually work due to a bug in cargo: rust-lang/rustfix#141

);
Expand All @@ -111,12 +111,20 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
Applicability::MaybeIncorrect,
);
if panic == sym::std_panic_macro {
l.span_suggestion_verbose(
span.until(arg_span),
"or use std::panic::panic_any instead",
"std::panic::panic_any(".into(),
Applicability::MachineApplicable,
);
if let Some((open, close, del)) = find_delimiters(cx, span) {
l.multipart_suggestion(
"or use std::panic::panic_any instead",
if del == '(' {
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();
Expand Down Expand Up @@ -206,13 +214,23 @@ fn check_panic_str<'tcx>(
}
}

/// Given the span of `some_macro!(args)`, gives the span of `args`.
fn find_inner_span<'tcx>(cx: &LateContext<'tcx>, span: Span) -> Option<Span> {
/// Given the span of `some_macro!(args);`, gives the span of `(` and `)`,
/// 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()?;
Some(span.from_inner(InnerSpan {
start: snippet.find(&['(', '{', '['][..])? + 1,
end: snippet.rfind(&[')', '}', ']'][..])?,
}))
let (open, open_ch) = snippet.char_indices().find(|&(_, c)| "([{".contains(c))?;
let close = snippet.rfind(|c| ")]}".contains(c))?;
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) {
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/non-fmt-panic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ LL | panic!("{}", C);
help: or use std::panic::panic_any instead
|
LL | std::panic::panic_any(C);
| ^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^

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

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

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

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