Small cleanups in assert!() and panic_fmt lint.

(From the PR feedback.)

Co-authored-by: Esteban Küber <esteban@kuber.com.ar>
This commit is contained in:
Mara Bos 2020-10-19 21:14:05 +02:00
parent ff8df0bbe3
commit 0f193d1a62
4 changed files with 48 additions and 50 deletions

View file

@ -27,8 +27,7 @@ pub fn expand_assert<'cx>(
// context to pick up whichever is currently in scope. // context to pick up whichever is currently in scope.
let sp = cx.with_call_site_ctxt(sp); let sp = cx.with_call_site_ctxt(sp);
let panic_call = { let panic_call = if let Some(tokens) = custom_message {
if let Some(tokens) = custom_message {
// Pass the custom message to panic!(). // Pass the custom message to panic!().
cx.expr( cx.expr(
sp, sp,
@ -57,7 +56,6 @@ pub fn expand_assert<'cx>(
)), )),
)], )],
) )
}
}; };
let if_expr = let if_expr =
cx.expr_if(sp, cx.expr(sp, ExprKind::Unary(UnOp::Not, cond_expr)), panic_call, None); cx.expr_if(sp, cx.expr(sp, ExprKind::Unary(UnOp::Not, cond_expr)), panic_call, None);

View file

@ -72,8 +72,8 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
} }
if looks_like_placeholder { if looks_like_placeholder {
cx.struct_span_lint(PANIC_FMT, arg.span.source_callsite(), |lint| { cx.struct_span_lint(PANIC_FMT, arg.span.source_callsite(), |lint| {
let mut l = lint.build("Panic message contains an unused formatting placeholder"); let mut l = lint.build("panic message contains an unused formatting placeholder");
l.note("This message is not used as a format string when given without arguments, but will be in a future Rust version"); l.note("this message is not used as a format string when given without arguments, but will be in a future Rust version");
if expn.call_site.contains(arg.span) { if expn.call_site.contains(arg.span) {
l.span_suggestion( l.span_suggestion(
arg.span.shrink_to_hi(), arg.span.shrink_to_hi(),
@ -92,8 +92,8 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
}); });
} else { } else {
cx.struct_span_lint(PANIC_FMT, expn.call_site, |lint| { cx.struct_span_lint(PANIC_FMT, expn.call_site, |lint| {
let mut l = lint.build("Panic message contains a brace"); let mut l = lint.build("panic message contains a brace");
l.note("This message is not used as a format string, but will be in a future Rust version"); l.note("this message is not used as a format string, but will be in a future Rust version");
if expn.call_site.contains(arg.span) { if expn.call_site.contains(arg.span) {
l.span_suggestion( l.span_suggestion(
arg.span.shrink_to_lo(), arg.span.shrink_to_lo(),

View file

@ -2,9 +2,9 @@
#[allow(unreachable_code)] #[allow(unreachable_code)]
fn main() { fn main() {
panic!("here's a brace: {"); //~ WARN Panic message contains a brace panic!("here's a brace: {"); //~ WARN panic message contains a brace
std::panic!("another one: }"); //~ WARN Panic message contains a brace std::panic!("another one: }"); //~ WARN panic message contains a brace
core::panic!("Hello {}"); //~ WARN Panic message contains an unused formatting placeholder core::panic!("Hello {}"); //~ WARN panic message contains an unused formatting placeholder
assert!(false, "{:03x} bla"); //~ WARN Panic message contains an unused formatting placeholder assert!(false, "{:03x} bla"); //~ WARN panic message contains an unused formatting placeholder
debug_assert!(false, "{{}} bla"); //~ WARN Panic message contains a brace debug_assert!(false, "{{}} bla"); //~ WARN panic message contains a brace
} }

View file

@ -1,35 +1,35 @@
warning: Panic message contains a brace warning: panic message contains a brace
--> $DIR/panic-brace.rs:5:5 --> $DIR/panic-brace.rs:5:5
| |
LL | panic!("here's a brace: {"); LL | panic!("here's a brace: {");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
= note: `#[warn(panic_fmt)]` on by default = note: `#[warn(panic_fmt)]` on by default
= note: This message is not used as a format string, but will be in a future Rust version = note: this message is not used as a format string, but will be in a future Rust version
help: add a "{}" format string to use the message literally help: add a "{}" format string to use the message literally
| |
LL | panic!("{}", "here's a brace: {"); LL | panic!("{}", "here's a brace: {");
| ^^^^^ | ^^^^^
warning: Panic message contains a brace warning: panic message contains a brace
--> $DIR/panic-brace.rs:6:5 --> $DIR/panic-brace.rs:6:5
| |
LL | std::panic!("another one: }"); LL | std::panic!("another one: }");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
= note: This message is not used as a format string, but will be in a future Rust version = note: this message is not used as a format string, but will be in a future Rust version
help: add a "{}" format string to use the message literally help: add a "{}" format string to use the message literally
| |
LL | std::panic!("{}", "another one: }"); LL | std::panic!("{}", "another one: }");
| ^^^^^ | ^^^^^
warning: Panic message contains an unused formatting placeholder warning: panic message contains an unused formatting placeholder
--> $DIR/panic-brace.rs:7:18 --> $DIR/panic-brace.rs:7:18
| |
LL | core::panic!("Hello {}"); LL | core::panic!("Hello {}");
| ^^^^^^^^^^ | ^^^^^^^^^^
| |
= note: This message is not used as a format string when given without arguments, but will be in a future Rust version = note: this message is not used as a format string when given without arguments, but will be in a future Rust version
help: add the missing argument(s) help: add the missing argument(s)
| |
LL | core::panic!("Hello {}", argument); LL | core::panic!("Hello {}", argument);
@ -39,13 +39,13 @@ help: or add a "{}" format string to use the message literally
LL | core::panic!("{}", "Hello {}"); LL | core::panic!("{}", "Hello {}");
| ^^^^^ | ^^^^^
warning: Panic message contains an unused formatting placeholder warning: panic message contains an unused formatting placeholder
--> $DIR/panic-brace.rs:8:20 --> $DIR/panic-brace.rs:8:20
| |
LL | assert!(false, "{:03x} bla"); LL | assert!(false, "{:03x} bla");
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
| |
= note: This message is not used as a format string when given without arguments, but will be in a future Rust version = note: this message is not used as a format string when given without arguments, but will be in a future Rust version
help: add the missing argument(s) help: add the missing argument(s)
| |
LL | assert!(false, "{:03x} bla", argument); LL | assert!(false, "{:03x} bla", argument);
@ -55,13 +55,13 @@ help: or add a "{}" format string to use the message literally
LL | assert!(false, "{}", "{:03x} bla"); LL | assert!(false, "{}", "{:03x} bla");
| ^^^^^ | ^^^^^
warning: Panic message contains a brace warning: panic message contains a brace
--> $DIR/panic-brace.rs:9:5 --> $DIR/panic-brace.rs:9:5
| |
LL | debug_assert!(false, "{{}} bla"); LL | debug_assert!(false, "{{}} bla");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
= note: This message is not used as a format string, but will be in a future Rust version = note: this message is not used as a format string, but will be in a future Rust version
help: add a "{}" format string to use the message literally help: add a "{}" format string to use the message literally
| |
LL | debug_assert!(false, "{}", "{{}} bla"); LL | debug_assert!(false, "{}", "{{}} bla");