Skip to content

Commit cf98080

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 cf98080

File tree

5 files changed

+22
-13
lines changed

5 files changed

+22
-13
lines changed

library/alloc/src/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ macro_rules! vec {
120120
#[macro_export]
121121
#[stable(feature = "rust1", since = "1.0.0")]
122122
#[allow_internal_unstable(hint_must_use, liballoc_internals)]
123-
#[cfg_attr(not(test), rustc_diagnostic_item = "format_macro")]
123+
#[cfg_attr(not(test), rustc_diagnostic_item = "format_macro", clippy::format_args)]
124124
macro_rules! format {
125125
($($arg:tt)*) => {
126126
$crate::__export::must_use({

library/core/src/macros/mod.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ macro_rules! panic {
3737
/// ```
3838
#[macro_export]
3939
#[stable(feature = "rust1", since = "1.0.0")]
40-
#[cfg_attr(not(test), rustc_diagnostic_item = "assert_eq_macro")]
40+
#[cfg_attr(not(test), rustc_diagnostic_item = "assert_eq_macro", clippy::format_args)]
4141
#[allow_internal_unstable(panic_internals)]
4242
macro_rules! assert_eq {
4343
($left:expr, $right:expr $(,)?) => {
@@ -93,7 +93,7 @@ macro_rules! assert_eq {
9393
/// ```
9494
#[macro_export]
9595
#[stable(feature = "assert_ne", since = "1.13.0")]
96-
#[cfg_attr(not(test), rustc_diagnostic_item = "assert_ne_macro")]
96+
#[cfg_attr(not(test), rustc_diagnostic_item = "assert_ne_macro", clippy::format_args)]
9797
#[allow_internal_unstable(panic_internals)]
9898
macro_rules! assert_ne {
9999
($left:expr, $right:expr $(,)?) => {
@@ -169,6 +169,7 @@ macro_rules! assert_ne {
169169
#[unstable(feature = "assert_matches", issue = "82775")]
170170
#[allow_internal_unstable(panic_internals)]
171171
#[rustc_macro_transparency = "semitransparent"]
172+
#[cfg_attr(not(test), clippy::format_args)]
172173
pub macro assert_matches {
173174
($left:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )? $(,)?) => {
174175
match $left {
@@ -392,6 +393,7 @@ pub macro cfg_match {
392393
#[stable(feature = "rust1", since = "1.0.0")]
393394
#[rustc_diagnostic_item = "debug_assert_macro"]
394395
#[allow_internal_unstable(edition_panic)]
396+
#[cfg_attr(not(test), clippy::format_args)]
395397
macro_rules! debug_assert {
396398
($($arg:tt)*) => {
397399
if $crate::cfg!(debug_assertions) {
@@ -421,7 +423,7 @@ macro_rules! debug_assert {
421423
/// ```
422424
#[macro_export]
423425
#[stable(feature = "rust1", since = "1.0.0")]
424-
#[cfg_attr(not(test), rustc_diagnostic_item = "debug_assert_eq_macro")]
426+
#[cfg_attr(not(test), rustc_diagnostic_item = "debug_assert_eq_macro", clippy::format_args)]
425427
macro_rules! debug_assert_eq {
426428
($($arg:tt)*) => {
427429
if $crate::cfg!(debug_assertions) {
@@ -451,7 +453,7 @@ macro_rules! debug_assert_eq {
451453
/// ```
452454
#[macro_export]
453455
#[stable(feature = "assert_ne", since = "1.13.0")]
454-
#[cfg_attr(not(test), rustc_diagnostic_item = "debug_assert_ne_macro")]
456+
#[cfg_attr(not(test), rustc_diagnostic_item = "debug_assert_ne_macro", clippy::format_args)]
455457
macro_rules! debug_assert_ne {
456458
($($arg:tt)*) => {
457459
if $crate::cfg!(debug_assertions) {
@@ -707,7 +709,7 @@ macro_rules! r#try {
707709
/// ```
708710
#[macro_export]
709711
#[stable(feature = "rust1", since = "1.0.0")]
710-
#[cfg_attr(not(test), rustc_diagnostic_item = "write_macro")]
712+
#[cfg_attr(not(test), rustc_diagnostic_item = "write_macro", clippy::format_args)]
711713
macro_rules! write {
712714
($dst:expr, $($arg:tt)*) => {
713715
$dst.write_fmt($crate::format_args!($($arg)*))
@@ -741,7 +743,7 @@ macro_rules! write {
741743
/// ```
742744
#[macro_export]
743745
#[stable(feature = "rust1", since = "1.0.0")]
744-
#[cfg_attr(not(test), rustc_diagnostic_item = "writeln_macro")]
746+
#[cfg_attr(not(test), rustc_diagnostic_item = "writeln_macro", clippy::format_args)]
745747
#[allow_internal_unstable(format_args_nl)]
746748
macro_rules! writeln {
747749
($dst:expr $(,)?) => {
@@ -895,6 +897,7 @@ macro_rules! unreachable {
895897
#[stable(feature = "rust1", since = "1.0.0")]
896898
#[cfg_attr(not(test), rustc_diagnostic_item = "unimplemented_macro")]
897899
#[allow_internal_unstable(panic_internals)]
900+
#[clippy::format_args]
898901
macro_rules! unimplemented {
899902
() => {
900903
$crate::panicking::panic("not implemented")
@@ -973,7 +976,7 @@ macro_rules! unimplemented {
973976
/// ```
974977
#[macro_export]
975978
#[stable(feature = "todo_macro", since = "1.40.0")]
976-
#[cfg_attr(not(test), rustc_diagnostic_item = "todo_macro")]
979+
#[cfg_attr(not(test), rustc_diagnostic_item = "todo_macro", clippy::format_args)]
977980
#[allow_internal_unstable(panic_internals)]
978981
macro_rules! todo {
979982
() => {
@@ -1085,7 +1088,7 @@ pub(crate) mod builtin {
10851088
/// and cannot be stored for later use.
10861089
/// This is a known limitation, see [#92698](https://github.com/rust-lang/rust/issues/92698).
10871090
#[stable(feature = "rust1", since = "1.0.0")]
1088-
#[cfg_attr(not(test), rustc_diagnostic_item = "format_args_macro")]
1091+
#[cfg_attr(not(test), rustc_diagnostic_item = "format_args_macro", clippy::format_args)]
10891092
#[allow_internal_unsafe]
10901093
#[allow_internal_unstable(fmt_internals)]
10911094
#[rustc_builtin_macro]
@@ -1675,6 +1678,7 @@ pub(crate) mod builtin {
16751678
edition_panic,
16761679
generic_assert_internals
16771680
)]
1681+
#[cfg_attr(not(test), clippy::format_args)]
16781682
macro_rules! assert {
16791683
($cond:expr $(,)?) => {{ /* compiler built-in */ }};
16801684
($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+
#[cfg_attr(not(test), 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+
#[cfg_attr(not(test), 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+
#[cfg_attr(not(test), clippy::format_args)]
134137
pub macro unreachable_2021 {
135138
() => (
136139
$crate::panicking::panic("internal error: entered unreachable code")

library/std/src/macros.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ macro_rules! panic {
7777
/// ```
7878
#[macro_export]
7979
#[stable(feature = "rust1", since = "1.0.0")]
80-
#[cfg_attr(not(test), rustc_diagnostic_item = "print_macro")]
80+
#[cfg_attr(not(test), rustc_diagnostic_item = "print_macro", clippy::format_args)]
8181
#[allow_internal_unstable(print_internals)]
8282
macro_rules! print {
8383
($($arg:tt)*) => {{
@@ -133,7 +133,7 @@ macro_rules! print {
133133
/// ```
134134
#[macro_export]
135135
#[stable(feature = "rust1", since = "1.0.0")]
136-
#[cfg_attr(not(test), rustc_diagnostic_item = "println_macro")]
136+
#[cfg_attr(not(test), rustc_diagnostic_item = "println_macro", clippy::format_args)]
137137
#[allow_internal_unstable(print_internals, format_args_nl)]
138138
macro_rules! println {
139139
() => {
@@ -173,7 +173,7 @@ macro_rules! println {
173173
/// ```
174174
#[macro_export]
175175
#[stable(feature = "eprint", since = "1.19.0")]
176-
#[cfg_attr(not(test), rustc_diagnostic_item = "eprint_macro")]
176+
#[cfg_attr(not(test), rustc_diagnostic_item = "eprint_macro", clippy::format_args)]
177177
#[allow_internal_unstable(print_internals)]
178178
macro_rules! eprint {
179179
($($arg:tt)*) => {{
@@ -211,7 +211,7 @@ macro_rules! eprint {
211211
/// ```
212212
#[macro_export]
213213
#[stable(feature = "eprint", since = "1.19.0")]
214-
#[cfg_attr(not(test), rustc_diagnostic_item = "eprintln_macro")]
214+
#[cfg_attr(not(test), rustc_diagnostic_item = "eprintln_macro", clippy::format_args)]
215215
#[allow_internal_unstable(print_internals, format_args_nl)]
216216
macro_rules! eprintln {
217217
() => {

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+
#[cfg_attr(not(test), 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+
#[cfg_attr(not(test), clippy::format_args)]
4648
macro_rules! rtabort {
4749
($($t:tt)*) => {
4850
{

0 commit comments

Comments
 (0)