Skip to content

Commit d3b4149

Browse files
committed
Also apply panic_fmt lint suggestions to debug_assert!().
1 parent 0a9330c commit d3b4149

File tree

5 files changed

+23
-10
lines changed

5 files changed

+23
-10
lines changed

compiler/rustc_lint/src/panic_fmt.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl<'tcx> LateLintPass<'tcx> for PanicFmt {
4747
fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tcx hir::Expr<'tcx>) {
4848
if let hir::ExprKind::Lit(lit) = &arg.kind {
4949
if let ast::LitKind::Str(sym, _) = lit.node {
50-
let expn = f.span.ctxt().outer_expn_data();
50+
let mut expn = f.span.ctxt().outer_expn_data();
5151
if let Some(id) = expn.macro_def_id {
5252
if cx.tcx.is_diagnostic_item(sym::std_panic_macro, id)
5353
|| cx.tcx.is_diagnostic_item(sym::core_panic_macro, id)
@@ -59,19 +59,17 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
5959
let s = s.replace("{{", "").replace("}}", "");
6060
let looks_like_placeholder =
6161
s.find('{').map_or(false, |i| s[i + 1..].contains('}'));
62-
let expn = {
63-
// Unwrap another level of macro expansion if this
64-
// panic!() was expanded from assert!().
62+
// Unwrap another level of macro expansion if this panic!()
63+
// was expanded from assert!() or debug_assert!().
64+
for &assert in &[sym::assert_macro, sym::debug_assert_macro] {
6565
let parent = expn.call_site.ctxt().outer_expn_data();
6666
if parent
6767
.macro_def_id
68-
.map_or(false, |id| cx.tcx.is_diagnostic_item(sym::assert_macro, id))
68+
.map_or(false, |id| cx.tcx.is_diagnostic_item(assert, id))
6969
{
70-
parent
71-
} else {
72-
expn
70+
expn = parent;
7371
}
74-
};
72+
}
7573
if looks_like_placeholder {
7674
cx.struct_span_lint(PANIC_FMT, arg.span.source_callsite(), |lint| {
7775
let mut l = lint.build("Panic message contains an unused formatting placeholder");

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ symbols! {
418418
dead_code,
419419
dealloc,
420420
debug,
421+
debug_assert_macro,
421422
debug_assertions,
422423
debug_struct,
423424
debug_trait,

library/core/src/macros/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ macro_rules! assert_ne {
163163
/// ```
164164
#[macro_export]
165165
#[stable(feature = "rust1", since = "1.0.0")]
166+
#[cfg_attr(not(bootstrap), rustc_diagnostic_item = "debug_assert_macro")]
166167
macro_rules! debug_assert {
167168
($($arg:tt)*) => (if $crate::cfg!(debug_assertions) { $crate::assert!($($arg)*); })
168169
}

src/test/ui/panic-brace.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ fn main() {
66
std::panic!("another one: }"); //~ WARN Panic message contains a brace
77
core::panic!("Hello {}"); //~ WARN Panic message contains an unused formatting placeholder
88
assert!(false, "{:03x} bla"); //~ WARN Panic message contains an unused formatting placeholder
9+
debug_assert!(false, "{{}} bla"); //~ WARN Panic message contains a brace
910
}

src/test/ui/panic-brace.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,17 @@ help: or add a "{}" format string to use the message literally
5555
LL | assert!(false, "{}", "{:03x} bla");
5656
| ^^^^^
5757

58-
warning: 4 warnings emitted
58+
warning: Panic message contains a brace
59+
--> $DIR/panic-brace.rs:9:5
60+
|
61+
LL | debug_assert!(false, "{{}} bla");
62+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
63+
|
64+
= note: This message is not used as a format string, but will be in a future Rust version
65+
help: add a "{}" format string to use the message literally
66+
|
67+
LL | debug_assert!(false, "{}", "{{}} bla");
68+
| ^^^^^
69+
70+
warning: 5 warnings emitted
5971

0 commit comments

Comments
 (0)