Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a466790

Browse files
committed
Add lint to warn about braces in a panic message.
1 parent 7e20323 commit a466790

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

compiler/rustc_lint/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ mod levels;
5555
mod methods;
5656
mod non_ascii_idents;
5757
mod nonstandard_style;
58+
mod panic_fmt;
5859
mod passes;
5960
mod redundant_semicolon;
6061
mod traits;
@@ -80,6 +81,7 @@ use internal::*;
8081
use methods::*;
8182
use non_ascii_idents::*;
8283
use nonstandard_style::*;
84+
use panic_fmt::PanicFmt;
8385
use redundant_semicolon::*;
8486
use traits::*;
8587
use types::*;
@@ -166,6 +168,7 @@ macro_rules! late_lint_passes {
166168
ClashingExternDeclarations: ClashingExternDeclarations::new(),
167169
DropTraitConstraints: DropTraitConstraints,
168170
TemporaryCStringAsPtr: TemporaryCStringAsPtr,
171+
PanicFmt: PanicFmt,
169172
]
170173
);
171174
};

compiler/rustc_lint/src/panic_fmt.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use crate::{LateContext, LateLintPass, LintContext};
2+
use rustc_ast as ast;
3+
use rustc_hir as hir;
4+
use rustc_middle::ty;
5+
6+
declare_lint! {
7+
/// The `panic_fmt` lint detects `panic!("..")` with `{` or `}` in the string literal.
8+
///
9+
/// ### Example
10+
///
11+
/// ```rust,no_run
12+
/// panic!("{}");
13+
/// ```
14+
///
15+
/// {{produces}}
16+
///
17+
/// ### Explanation
18+
///
19+
/// `panic!("{}")` panics with the message `"{}"`, as a `panic!()` invocation
20+
/// with a single argument does not use `format_args!()`.
21+
/// A future version of Rust will interpret this string as format string,
22+
/// which would break this.
23+
PANIC_FMT,
24+
Warn,
25+
"detect braces in single-argument panic!() invocations",
26+
report_in_external_macro
27+
}
28+
29+
declare_lint_pass!(PanicFmt => [PANIC_FMT]);
30+
31+
impl<'tcx> LateLintPass<'tcx> for PanicFmt {
32+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) {
33+
if let hir::ExprKind::Call(f, [arg]) = &expr.kind {
34+
if let &ty::FnDef(def_id, _) = cx.typeck_results().expr_ty(f).kind() {
35+
if Some(def_id) == cx.tcx.lang_items().begin_panic_fn()
36+
|| Some(def_id) == cx.tcx.lang_items().panic_fn()
37+
{
38+
check_panic(cx, f, arg);
39+
}
40+
}
41+
}
42+
}
43+
}
44+
45+
fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tcx hir::Expr<'tcx>) {
46+
if let hir::ExprKind::Lit(lit) = &arg.kind {
47+
if let ast::LitKind::Str(sym, _) = lit.node {
48+
if sym.as_str().contains(&['{', '}'][..]) {
49+
cx.struct_span_lint(PANIC_FMT, f.span, |lint| {
50+
lint.build("Panic message contains a brace")
51+
.note("This message is not used as a format string, but will be in a future Rust version")
52+
.emit();
53+
});
54+
}
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)