Skip to content

Commit c6e44a5

Browse files
committed
Auto merge of rust-lang#78088 - fusion-engineering-forks:panic-fmt-lint, r=estebank
Add lint for panic!("{}") This adds a lint that warns about `panic!("{}")`. `panic!(msg)` invocations with a single argument use their argument as panic payload literally, without using it as a format string. The same holds for `assert!(expr, msg)`. This lints checks if `msg` is a string literal (after expansion), and warns in case it contained braces. It suggests to insert `"{}", ` to use the message literally, or to add arguments to use it as a format string. ![image](https://user-images.githubusercontent.com/783247/96643867-79eb1080-1328-11eb-8d4e-a5586837c70a.png) This lint is also a good starting point for adding warnings about `panic!(not_a_string)` later, once [`panic_any()`](rust-lang#74622) becomes a stable alternative.
2 parents 7e95740 + 5c5af40 commit c6e44a5

File tree

3 files changed

+6
-1
lines changed

3 files changed

+6
-1
lines changed

core/src/macros/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#[macro_export]
33
#[allow_internal_unstable(core_panic, const_caller_location)]
44
#[stable(feature = "core", since = "1.6.0")]
5+
#[cfg_attr(not(bootstrap), rustc_diagnostic_item = "core_panic_macro")]
56
macro_rules! panic {
67
() => (
78
$crate::panic!("explicit panic")
@@ -162,6 +163,7 @@ macro_rules! assert_ne {
162163
/// ```
163164
#[macro_export]
164165
#[stable(feature = "rust1", since = "1.0.0")]
166+
#[cfg_attr(not(bootstrap), rustc_diagnostic_item = "debug_assert_macro")]
165167
macro_rules! debug_assert {
166168
($($arg:tt)*) => (if $crate::cfg!(debug_assertions) { $crate::assert!($($arg)*); })
167169
}
@@ -1215,6 +1217,8 @@ pub(crate) mod builtin {
12151217
#[stable(feature = "rust1", since = "1.0.0")]
12161218
#[rustc_builtin_macro]
12171219
#[macro_export]
1220+
#[cfg_attr(not(bootstrap), rustc_diagnostic_item = "assert_macro")]
1221+
#[allow_internal_unstable(core_panic)]
12181222
macro_rules! assert {
12191223
($cond:expr $(,)?) => {{ /* compiler built-in */ }};
12201224
($cond:expr, $($arg:tt)+) => {{ /* compiler built-in */ }};

core/tests/nonzero.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ fn test_match_option_string() {
8585
let five = "Five".to_string();
8686
match Some(five) {
8787
Some(s) => assert_eq!(s, "Five"),
88-
None => panic!("unexpected None while matching on Some(String { ... })"),
88+
None => panic!("{}", "unexpected None while matching on Some(String { ... })"),
8989
}
9090
}
9191

std/src/macros.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#[macro_export]
99
#[stable(feature = "rust1", since = "1.0.0")]
1010
#[allow_internal_unstable(libstd_sys_internals)]
11+
#[cfg_attr(not(any(bootstrap, test)), rustc_diagnostic_item = "std_panic_macro")]
1112
macro_rules! panic {
1213
() => ({ $crate::panic!("explicit panic") });
1314
($msg:expr $(,)?) => ({ $crate::rt::begin_panic($msg) });

0 commit comments

Comments
 (0)