Skip to content

Commit 55b4991

Browse files
committed
Tag all format-like macros with #[clippy::format_args]
Since 1.85, Clippy [supports](See https://doc.rust-lang.org/nightly/clippy/attribs.html#clippyformat_args) attribute-based discovery of the `format!`-compatible macros, i.e. macros whose trailing arguments can be passed to `format_args!` as is. Tagging core library with the same attribute will allow clippy to stop maintaining a separate list of format-like macros - allowing Clippy to validate macro usage for all lints that work on format arguments. See also rust-lang/rust-clippy#14267
1 parent a18bd8a commit 55b4991

File tree

5 files changed

+22
-0
lines changed

5 files changed

+22
-0
lines changed

library/alloc/src/macros.rs

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ macro_rules! vec {
121121
#[stable(feature = "rust1", since = "1.0.0")]
122122
#[allow_internal_unstable(hint_must_use, liballoc_internals)]
123123
#[cfg_attr(not(test), rustc_diagnostic_item = "format_macro")]
124+
#[clippy::format_args]
124125
macro_rules! format {
125126
($($arg:tt)*) => {
126127
$crate::__export::must_use({

library/core/src/macros/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ macro_rules! panic {
3939
#[stable(feature = "rust1", since = "1.0.0")]
4040
#[cfg_attr(not(test), rustc_diagnostic_item = "assert_eq_macro")]
4141
#[allow_internal_unstable(panic_internals)]
42+
#[clippy::format_args]
4243
macro_rules! assert_eq {
4344
($left:expr, $right:expr $(,)?) => {
4445
match (&$left, &$right) {
@@ -95,6 +96,7 @@ macro_rules! assert_eq {
9596
#[stable(feature = "assert_ne", since = "1.13.0")]
9697
#[cfg_attr(not(test), rustc_diagnostic_item = "assert_ne_macro")]
9798
#[allow_internal_unstable(panic_internals)]
99+
#[clippy::format_args]
98100
macro_rules! assert_ne {
99101
($left:expr, $right:expr $(,)?) => {
100102
match (&$left, &$right) {
@@ -169,6 +171,7 @@ macro_rules! assert_ne {
169171
#[unstable(feature = "assert_matches", issue = "82775")]
170172
#[allow_internal_unstable(panic_internals)]
171173
#[rustc_macro_transparency = "semitransparent"]
174+
#[clippy::format_args]
172175
pub macro assert_matches {
173176
($left:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )? $(,)?) => {
174177
match $left {
@@ -392,6 +395,7 @@ pub macro cfg_match {
392395
#[stable(feature = "rust1", since = "1.0.0")]
393396
#[rustc_diagnostic_item = "debug_assert_macro"]
394397
#[allow_internal_unstable(edition_panic)]
398+
#[clippy::format_args]
395399
macro_rules! debug_assert {
396400
($($arg:tt)*) => {
397401
if $crate::cfg!(debug_assertions) {
@@ -422,6 +426,7 @@ macro_rules! debug_assert {
422426
#[macro_export]
423427
#[stable(feature = "rust1", since = "1.0.0")]
424428
#[cfg_attr(not(test), rustc_diagnostic_item = "debug_assert_eq_macro")]
429+
#[clippy::format_args]
425430
macro_rules! debug_assert_eq {
426431
($($arg:tt)*) => {
427432
if $crate::cfg!(debug_assertions) {
@@ -452,6 +457,7 @@ macro_rules! debug_assert_eq {
452457
#[macro_export]
453458
#[stable(feature = "assert_ne", since = "1.13.0")]
454459
#[cfg_attr(not(test), rustc_diagnostic_item = "debug_assert_ne_macro")]
460+
#[clippy::format_args]
455461
macro_rules! debug_assert_ne {
456462
($($arg:tt)*) => {
457463
if $crate::cfg!(debug_assertions) {
@@ -708,6 +714,7 @@ macro_rules! r#try {
708714
#[macro_export]
709715
#[stable(feature = "rust1", since = "1.0.0")]
710716
#[cfg_attr(not(test), rustc_diagnostic_item = "write_macro")]
717+
#[clippy::format_args]
711718
macro_rules! write {
712719
($dst:expr, $($arg:tt)*) => {
713720
$dst.write_fmt($crate::format_args!($($arg)*))
@@ -743,6 +750,7 @@ macro_rules! write {
743750
#[stable(feature = "rust1", since = "1.0.0")]
744751
#[cfg_attr(not(test), rustc_diagnostic_item = "writeln_macro")]
745752
#[allow_internal_unstable(format_args_nl)]
753+
#[clippy::format_args]
746754
macro_rules! writeln {
747755
($dst:expr $(,)?) => {
748756
$crate::write!($dst, "\n")
@@ -895,6 +903,7 @@ macro_rules! unreachable {
895903
#[stable(feature = "rust1", since = "1.0.0")]
896904
#[cfg_attr(not(test), rustc_diagnostic_item = "unimplemented_macro")]
897905
#[allow_internal_unstable(panic_internals)]
906+
#[clippy::format_args]
898907
macro_rules! unimplemented {
899908
() => {
900909
$crate::panicking::panic("not implemented")
@@ -975,6 +984,7 @@ macro_rules! unimplemented {
975984
#[stable(feature = "todo_macro", since = "1.40.0")]
976985
#[cfg_attr(not(test), rustc_diagnostic_item = "todo_macro")]
977986
#[allow_internal_unstable(panic_internals)]
987+
#[clippy::format_args]
978988
macro_rules! todo {
979989
() => {
980990
$crate::panicking::panic("not yet implemented")
@@ -1090,6 +1100,7 @@ pub(crate) mod builtin {
10901100
#[allow_internal_unstable(fmt_internals)]
10911101
#[rustc_builtin_macro]
10921102
#[macro_export]
1103+
#[clippy::format_args]
10931104
macro_rules! format_args {
10941105
($fmt:expr) => {{ /* compiler built-in */ }};
10951106
($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }};
@@ -1675,6 +1686,7 @@ pub(crate) mod builtin {
16751686
edition_panic,
16761687
generic_assert_internals
16771688
)]
1689+
#[clippy::format_args]
16781690
macro_rules! assert {
16791691
($cond:expr $(,)?) => {{ /* compiler built-in */ }};
16801692
($cond:expr, $($arg:tt)+) => {{ /* compiler built-in */ }};

library/core/src/panic.rs

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pub macro panic_2015 {
4949
#[rustc_diagnostic_item = "core_panic_2021_macro"]
5050
#[rustc_macro_transparency = "semitransparent"]
5151
#[cfg(feature = "panic_immediate_abort")]
52+
#[clippy::format_args]
5253
pub macro panic_2021 {
5354
() => (
5455
$crate::panicking::panic("explicit panic")
@@ -77,6 +78,7 @@ pub macro panic_2021 {
7778
#[rustc_diagnostic_item = "core_panic_2021_macro"]
7879
#[rustc_macro_transparency = "semitransparent"]
7980
#[cfg(not(feature = "panic_immediate_abort"))]
81+
#[clippy::format_args]
8082
pub macro panic_2021 {
8183
() => ({
8284
// Create a function so that the argument for `track_caller`
@@ -131,6 +133,7 @@ pub macro unreachable_2015 {
131133
#[unstable(feature = "edition_panic", issue = "none", reason = "use unreachable!() instead")]
132134
#[allow_internal_unstable(panic_internals)]
133135
#[rustc_macro_transparency = "semitransparent"]
136+
#[clippy::format_args]
134137
pub macro unreachable_2021 {
135138
() => (
136139
$crate::panicking::panic("internal error: entered unreachable code")

library/std/src/macros.rs

+4
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ macro_rules! panic {
7979
#[stable(feature = "rust1", since = "1.0.0")]
8080
#[cfg_attr(not(test), rustc_diagnostic_item = "print_macro")]
8181
#[allow_internal_unstable(print_internals)]
82+
#[clippy::format_args]
8283
macro_rules! print {
8384
($($arg:tt)*) => {{
8485
$crate::io::_print($crate::format_args!($($arg)*));
@@ -135,6 +136,7 @@ macro_rules! print {
135136
#[stable(feature = "rust1", since = "1.0.0")]
136137
#[cfg_attr(not(test), rustc_diagnostic_item = "println_macro")]
137138
#[allow_internal_unstable(print_internals, format_args_nl)]
139+
#[clippy::format_args]
138140
macro_rules! println {
139141
() => {
140142
$crate::print!("\n")
@@ -175,6 +177,7 @@ macro_rules! println {
175177
#[stable(feature = "eprint", since = "1.19.0")]
176178
#[cfg_attr(not(test), rustc_diagnostic_item = "eprint_macro")]
177179
#[allow_internal_unstable(print_internals)]
180+
#[clippy::format_args]
178181
macro_rules! eprint {
179182
($($arg:tt)*) => {{
180183
$crate::io::_eprint($crate::format_args!($($arg)*));
@@ -213,6 +216,7 @@ macro_rules! eprint {
213216
#[stable(feature = "eprint", since = "1.19.0")]
214217
#[cfg_attr(not(test), rustc_diagnostic_item = "eprintln_macro")]
215218
#[allow_internal_unstable(print_internals, format_args_nl)]
219+
#[clippy::format_args]
216220
macro_rules! eprintln {
217221
() => {
218222
$crate::eprint!("\n")

library/std/src/rt.rs

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use crate::{mem, panic, sys};
3030
// - the standard error output
3131
// - some dedicated platform specific output
3232
// - nothing (so this macro is a no-op)
33+
#[clippy::format_args]
3334
macro_rules! rtprintpanic {
3435
($($t:tt)*) => {
3536
#[cfg(not(feature = "panic_immediate_abort"))]
@@ -43,6 +44,7 @@ macro_rules! rtprintpanic {
4344
}
4445
}
4546

47+
#[clippy::format_args]
4648
macro_rules! rtabort {
4749
($($t:tt)*) => {
4850
{

0 commit comments

Comments
 (0)