Skip to content

Commit 0f193d1

Browse files
m-ou-seestebank
andcommitted
Small cleanups in assert!() and panic_fmt lint.
(From the PR feedback.) Co-authored-by: Esteban Küber <[email protected]>
1 parent ff8df0b commit 0f193d1

File tree

4 files changed

+48
-50
lines changed

4 files changed

+48
-50
lines changed

compiler/rustc_builtin_macros/src/assert.rs

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,35 @@ pub fn expand_assert<'cx>(
2727
// context to pick up whichever is currently in scope.
2828
let sp = cx.with_call_site_ctxt(sp);
2929

30-
let panic_call = {
31-
if let Some(tokens) = custom_message {
32-
// Pass the custom message to panic!().
33-
cx.expr(
34-
sp,
35-
ExprKind::MacCall(MacCall {
36-
path: Path::from_ident(Ident::new(sym::panic, sp)),
37-
args: P(MacArgs::Delimited(
38-
DelimSpan::from_single(sp),
39-
MacDelimiter::Parenthesis,
40-
tokens,
41-
)),
42-
prior_type_ascription: None,
43-
}),
44-
)
45-
} else {
46-
// Pass our own message directly to $crate::panicking::panic(),
47-
// because it might contain `{` and `}` that should always be
48-
// passed literally.
49-
cx.expr_call_global(
50-
sp,
51-
cx.std_path(&[sym::panicking, sym::panic]),
52-
vec![cx.expr_str(
53-
DUMMY_SP,
54-
Symbol::intern(&format!(
55-
"assertion failed: {}",
56-
pprust::expr_to_string(&cond_expr).escape_debug()
57-
)),
58-
)],
59-
)
60-
}
30+
let panic_call = if let Some(tokens) = custom_message {
31+
// Pass the custom message to panic!().
32+
cx.expr(
33+
sp,
34+
ExprKind::MacCall(MacCall {
35+
path: Path::from_ident(Ident::new(sym::panic, sp)),
36+
args: P(MacArgs::Delimited(
37+
DelimSpan::from_single(sp),
38+
MacDelimiter::Parenthesis,
39+
tokens,
40+
)),
41+
prior_type_ascription: None,
42+
}),
43+
)
44+
} else {
45+
// Pass our own message directly to $crate::panicking::panic(),
46+
// because it might contain `{` and `}` that should always be
47+
// passed literally.
48+
cx.expr_call_global(
49+
sp,
50+
cx.std_path(&[sym::panicking, sym::panic]),
51+
vec![cx.expr_str(
52+
DUMMY_SP,
53+
Symbol::intern(&format!(
54+
"assertion failed: {}",
55+
pprust::expr_to_string(&cond_expr).escape_debug()
56+
)),
57+
)],
58+
)
6159
};
6260
let if_expr =
6361
cx.expr_if(sp, cx.expr(sp, ExprKind::Unary(UnOp::Not, cond_expr)), panic_call, None);

compiler/rustc_lint/src/panic_fmt.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
7272
}
7373
if looks_like_placeholder {
7474
cx.struct_span_lint(PANIC_FMT, arg.span.source_callsite(), |lint| {
75-
let mut l = lint.build("Panic message contains an unused formatting placeholder");
76-
l.note("This message is not used as a format string when given without arguments, but will be in a future Rust version");
75+
let mut l = lint.build("panic message contains an unused formatting placeholder");
76+
l.note("this message is not used as a format string when given without arguments, but will be in a future Rust version");
7777
if expn.call_site.contains(arg.span) {
7878
l.span_suggestion(
7979
arg.span.shrink_to_hi(),
@@ -92,8 +92,8 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
9292
});
9393
} else {
9494
cx.struct_span_lint(PANIC_FMT, expn.call_site, |lint| {
95-
let mut l = lint.build("Panic message contains a brace");
96-
l.note("This message is not used as a format string, but will be in a future Rust version");
95+
let mut l = lint.build("panic message contains a brace");
96+
l.note("this message is not used as a format string, but will be in a future Rust version");
9797
if expn.call_site.contains(arg.span) {
9898
l.span_suggestion(
9999
arg.span.shrink_to_lo(),

src/test/ui/panic-brace.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

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

src/test/ui/panic-brace.stderr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
warning: Panic message contains a brace
1+
warning: panic message contains a brace
22
--> $DIR/panic-brace.rs:5:5
33
|
44
LL | panic!("here's a brace: {");
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `#[warn(panic_fmt)]` on by default
8-
= note: This message is not used as a format string, but will be in a future Rust version
8+
= note: this message is not used as a format string, but will be in a future Rust version
99
help: add a "{}" format string to use the message literally
1010
|
1111
LL | panic!("{}", "here's a brace: {");
1212
| ^^^^^
1313

14-
warning: Panic message contains a brace
14+
warning: panic message contains a brace
1515
--> $DIR/panic-brace.rs:6:5
1616
|
1717
LL | std::panic!("another one: }");
1818
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1919
|
20-
= note: This message is not used as a format string, but will be in a future Rust version
20+
= note: this message is not used as a format string, but will be in a future Rust version
2121
help: add a "{}" format string to use the message literally
2222
|
2323
LL | std::panic!("{}", "another one: }");
2424
| ^^^^^
2525

26-
warning: Panic message contains an unused formatting placeholder
26+
warning: panic message contains an unused formatting placeholder
2727
--> $DIR/panic-brace.rs:7:18
2828
|
2929
LL | core::panic!("Hello {}");
3030
| ^^^^^^^^^^
3131
|
32-
= note: This message is not used as a format string when given without arguments, but will be in a future Rust version
32+
= note: this message is not used as a format string when given without arguments, but will be in a future Rust version
3333
help: add the missing argument(s)
3434
|
3535
LL | core::panic!("Hello {}", argument);
@@ -39,13 +39,13 @@ help: or add a "{}" format string to use the message literally
3939
LL | core::panic!("{}", "Hello {}");
4040
| ^^^^^
4141

42-
warning: Panic message contains an unused formatting placeholder
42+
warning: panic message contains an unused formatting placeholder
4343
--> $DIR/panic-brace.rs:8:20
4444
|
4545
LL | assert!(false, "{:03x} bla");
4646
| ^^^^^^^^^^^^
4747
|
48-
= note: This message is not used as a format string when given without arguments, but will be in a future Rust version
48+
= note: this message is not used as a format string when given without arguments, but will be in a future Rust version
4949
help: add the missing argument(s)
5050
|
5151
LL | assert!(false, "{:03x} bla", argument);
@@ -55,13 +55,13 @@ help: or add a "{}" format string to use the message literally
5555
LL | assert!(false, "{}", "{:03x} bla");
5656
| ^^^^^
5757

58-
warning: Panic message contains a brace
58+
warning: panic message contains a brace
5959
--> $DIR/panic-brace.rs:9:5
6060
|
6161
LL | debug_assert!(false, "{{}} bla");
6262
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6363
|
64-
= note: This message is not used as a format string, but will be in a future Rust version
64+
= note: this message is not used as a format string, but will be in a future Rust version
6565
help: add a "{}" format string to use the message literally
6666
|
6767
LL | debug_assert!(false, "{}", "{{}} bla");

0 commit comments

Comments
 (0)