Skip to content

Commit 8f0ba1f

Browse files
committed
Auto merge of #10563 - nyurik:handle-all-fmt2, r=llogiq
Partial no-op refactoring of #9948 This contains some prep work for #9948 to keep that change to the minimum, and make it easier to review it. This should be a noop, but it has some tests from that PR discussion, and should help in the future with the corner case format handling. cc: `@Alexendoo` `@llogiq` `@xFrednet` as the 3 people who reviewed the parent PR ---- changelog: none
2 parents 8d83c15 + 783879e commit 8f0ba1f

6 files changed

+239
-35
lines changed

clippy_dev/src/new_lint.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,7 @@ fn create_lint_for_ty(lint: &LintData<'_>, enable_msrv: bool, ty: &str) -> io::R
369369
}}
370370
todo!();
371371
}}
372-
"#,
373-
context_import = context_import,
374-
name_upper = name_upper,
372+
"#
375373
);
376374
} else {
377375
let _: fmt::Result = writedoc!(
@@ -385,9 +383,7 @@ fn create_lint_for_ty(lint: &LintData<'_>, enable_msrv: bool, ty: &str) -> io::R
385383
pub(super) fn check(cx: &{context_import}) {{
386384
todo!();
387385
}}
388-
"#,
389-
context_import = context_import,
390-
name_upper = name_upper,
386+
"#
391387
);
392388
}
393389

clippy_dev/src/update_lints.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -537,17 +537,13 @@ fn declare_deprecated(name: &str, path: &Path, reason: &str) -> io::Result<()> {
537537
/// Nothing. This lint has been deprecated.
538538
///
539539
/// ### Deprecation reason
540-
/// {}
541-
#[clippy::version = \"{}\"]
542-
pub {},
543-
\"{}\"
540+
/// {deprecation_reason}
541+
#[clippy::version = \"{version}\"]
542+
pub {name},
543+
\"{reason}\"
544544
}}
545545
546-
",
547-
deprecation_reason,
548-
version,
549-
name,
550-
reason,
546+
"
551547
)
552548
}
553549

tests/ui/format_args_unfixable.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![warn(clippy::format_in_format_args, clippy::to_string_in_format_args)]
2+
#![allow(unused)]
23
#![allow(clippy::assertions_on_constants, clippy::eq_op, clippy::uninlined_format_args)]
34

45
use std::io::{stdout, Error, ErrorKind, Write};
@@ -57,3 +58,46 @@ fn main() {
5758
my_macro!();
5859
println!("error: {}", my_other_macro!());
5960
}
61+
62+
macro_rules! _internal {
63+
($($args:tt)*) => {
64+
println!("{}", format_args!($($args)*))
65+
};
66+
}
67+
68+
macro_rules! my_println2 {
69+
($target:expr, $($args:tt)+) => {{
70+
if $target {
71+
_internal!($($args)+)
72+
}
73+
}};
74+
}
75+
76+
macro_rules! my_println2_args {
77+
($target:expr, $($args:tt)+) => {{
78+
if $target {
79+
_internal!("foo: {}", format_args!($($args)+))
80+
}
81+
}};
82+
}
83+
84+
fn test2() {
85+
let error = Error::new(ErrorKind::Other, "bad thing");
86+
87+
// None of these should be linted without the config change
88+
my_println2!(true, "error: {}", format!("something failed at {}", Location::caller()));
89+
my_println2!(
90+
true,
91+
"{}: {}",
92+
error,
93+
format!("something failed at {}", Location::caller())
94+
);
95+
96+
my_println2_args!(true, "error: {}", format!("something failed at {}", Location::caller()));
97+
my_println2_args!(
98+
true,
99+
"{}: {}",
100+
error,
101+
format!("something failed at {}", Location::caller())
102+
);
103+
}

tests/ui/format_args_unfixable.stderr

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: `format!` in `println!` args
2-
--> $DIR/format_args_unfixable.rs:25:5
2+
--> $DIR/format_args_unfixable.rs:26:5
33
|
44
LL | println!("error: {}", format!("something failed at {}", Location::caller()));
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -9,7 +9,7 @@ LL | println!("error: {}", format!("something failed at {}", Location::calle
99
= note: `-D clippy::format-in-format-args` implied by `-D warnings`
1010

1111
error: `format!` in `println!` args
12-
--> $DIR/format_args_unfixable.rs:26:5
12+
--> $DIR/format_args_unfixable.rs:27:5
1313
|
1414
LL | println!("{}: {}", error, format!("something failed at {}", Location::caller()));
1515
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -18,7 +18,7 @@ LL | println!("{}: {}", error, format!("something failed at {}", Location::c
1818
= help: or consider changing `format!` to `format_args!`
1919

2020
error: `format!` in `println!` args
21-
--> $DIR/format_args_unfixable.rs:27:5
21+
--> $DIR/format_args_unfixable.rs:28:5
2222
|
2323
LL | println!("{:?}: {}", error, format!("something failed at {}", Location::caller()));
2424
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -27,7 +27,7 @@ LL | println!("{:?}: {}", error, format!("something failed at {}", Location:
2727
= help: or consider changing `format!` to `format_args!`
2828

2929
error: `format!` in `println!` args
30-
--> $DIR/format_args_unfixable.rs:28:5
30+
--> $DIR/format_args_unfixable.rs:29:5
3131
|
3232
LL | println!("{{}}: {}", format!("something failed at {}", Location::caller()));
3333
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -36,7 +36,7 @@ LL | println!("{{}}: {}", format!("something failed at {}", Location::caller
3636
= help: or consider changing `format!` to `format_args!`
3737

3838
error: `format!` in `println!` args
39-
--> $DIR/format_args_unfixable.rs:29:5
39+
--> $DIR/format_args_unfixable.rs:30:5
4040
|
4141
LL | println!(r#"error: "{}""#, format!("something failed at {}", Location::caller()));
4242
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -45,7 +45,7 @@ LL | println!(r#"error: "{}""#, format!("something failed at {}", Location::
4545
= help: or consider changing `format!` to `format_args!`
4646

4747
error: `format!` in `println!` args
48-
--> $DIR/format_args_unfixable.rs:30:5
48+
--> $DIR/format_args_unfixable.rs:31:5
4949
|
5050
LL | println!("error: {}", format!(r#"something failed at "{}""#, Location::caller()));
5151
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -54,7 +54,7 @@ LL | println!("error: {}", format!(r#"something failed at "{}""#, Location::
5454
= help: or consider changing `format!` to `format_args!`
5555

5656
error: `format!` in `println!` args
57-
--> $DIR/format_args_unfixable.rs:31:5
57+
--> $DIR/format_args_unfixable.rs:32:5
5858
|
5959
LL | println!("error: {}", format!("something failed at {} {0}", Location::caller()));
6060
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -63,7 +63,7 @@ LL | println!("error: {}", format!("something failed at {} {0}", Location::c
6363
= help: or consider changing `format!` to `format_args!`
6464

6565
error: `format!` in `format!` args
66-
--> $DIR/format_args_unfixable.rs:32:13
66+
--> $DIR/format_args_unfixable.rs:33:13
6767
|
6868
LL | let _ = format!("error: {}", format!("something failed at {}", Location::caller()));
6969
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -72,7 +72,7 @@ LL | let _ = format!("error: {}", format!("something failed at {}", Location
7272
= help: or consider changing `format!` to `format_args!`
7373

7474
error: `format!` in `write!` args
75-
--> $DIR/format_args_unfixable.rs:33:13
75+
--> $DIR/format_args_unfixable.rs:34:13
7676
|
7777
LL | let _ = write!(
7878
| _____________^
@@ -86,7 +86,7 @@ LL | | );
8686
= help: or consider changing `format!` to `format_args!`
8787

8888
error: `format!` in `writeln!` args
89-
--> $DIR/format_args_unfixable.rs:38:13
89+
--> $DIR/format_args_unfixable.rs:39:13
9090
|
9191
LL | let _ = writeln!(
9292
| _____________^
@@ -100,7 +100,7 @@ LL | | );
100100
= help: or consider changing `format!` to `format_args!`
101101

102102
error: `format!` in `print!` args
103-
--> $DIR/format_args_unfixable.rs:43:5
103+
--> $DIR/format_args_unfixable.rs:44:5
104104
|
105105
LL | print!("error: {}", format!("something failed at {}", Location::caller()));
106106
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -109,7 +109,7 @@ LL | print!("error: {}", format!("something failed at {}", Location::caller(
109109
= help: or consider changing `format!` to `format_args!`
110110

111111
error: `format!` in `eprint!` args
112-
--> $DIR/format_args_unfixable.rs:44:5
112+
--> $DIR/format_args_unfixable.rs:45:5
113113
|
114114
LL | eprint!("error: {}", format!("something failed at {}", Location::caller()));
115115
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -118,7 +118,7 @@ LL | eprint!("error: {}", format!("something failed at {}", Location::caller
118118
= help: or consider changing `format!` to `format_args!`
119119

120120
error: `format!` in `eprintln!` args
121-
--> $DIR/format_args_unfixable.rs:45:5
121+
--> $DIR/format_args_unfixable.rs:46:5
122122
|
123123
LL | eprintln!("error: {}", format!("something failed at {}", Location::caller()));
124124
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -127,7 +127,7 @@ LL | eprintln!("error: {}", format!("something failed at {}", Location::call
127127
= help: or consider changing `format!` to `format_args!`
128128

129129
error: `format!` in `format_args!` args
130-
--> $DIR/format_args_unfixable.rs:46:13
130+
--> $DIR/format_args_unfixable.rs:47:13
131131
|
132132
LL | let _ = format_args!("error: {}", format!("something failed at {}", Location::caller()));
133133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -136,7 +136,7 @@ LL | let _ = format_args!("error: {}", format!("something failed at {}", Loc
136136
= help: or consider changing `format!` to `format_args!`
137137

138138
error: `format!` in `assert!` args
139-
--> $DIR/format_args_unfixable.rs:47:5
139+
--> $DIR/format_args_unfixable.rs:48:5
140140
|
141141
LL | assert!(true, "error: {}", format!("something failed at {}", Location::caller()));
142142
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -145,7 +145,7 @@ LL | assert!(true, "error: {}", format!("something failed at {}", Location::
145145
= help: or consider changing `format!` to `format_args!`
146146

147147
error: `format!` in `assert_eq!` args
148-
--> $DIR/format_args_unfixable.rs:48:5
148+
--> $DIR/format_args_unfixable.rs:49:5
149149
|
150150
LL | assert_eq!(0, 0, "error: {}", format!("something failed at {}", Location::caller()));
151151
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -154,7 +154,7 @@ LL | assert_eq!(0, 0, "error: {}", format!("something failed at {}", Locatio
154154
= help: or consider changing `format!` to `format_args!`
155155

156156
error: `format!` in `assert_ne!` args
157-
--> $DIR/format_args_unfixable.rs:49:5
157+
--> $DIR/format_args_unfixable.rs:50:5
158158
|
159159
LL | assert_ne!(0, 0, "error: {}", format!("something failed at {}", Location::caller()));
160160
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -163,7 +163,7 @@ LL | assert_ne!(0, 0, "error: {}", format!("something failed at {}", Locatio
163163
= help: or consider changing `format!` to `format_args!`
164164

165165
error: `format!` in `panic!` args
166-
--> $DIR/format_args_unfixable.rs:50:5
166+
--> $DIR/format_args_unfixable.rs:51:5
167167
|
168168
LL | panic!("error: {}", format!("something failed at {}", Location::caller()));
169169
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

tests/ui/uninlined_format_args.fixed

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// aux-build:proc_macros.rs
22
// run-rustfix
33
#![warn(clippy::uninlined_format_args)]
4-
#![allow(named_arguments_used_positionally, unused_imports, unused_macros, unused_variables)]
4+
#![allow(named_arguments_used_positionally, unused)]
55
#![allow(clippy::eq_op, clippy::format_in_format_args, clippy::print_literal)]
66

77
extern crate proc_macros;
@@ -178,3 +178,87 @@ fn _meets_msrv() {
178178
fn _do_not_fire() {
179179
println!("{:?}", None::<()>);
180180
}
181+
182+
macro_rules! _internal {
183+
($($args:tt)*) => {
184+
println!("{}", format_args!($($args)*))
185+
};
186+
}
187+
188+
macro_rules! my_println2 {
189+
($target:expr, $($args:tt)+) => {{
190+
if $target {
191+
_internal!($($args)+)
192+
}
193+
}};
194+
}
195+
196+
macro_rules! my_println2_args {
197+
($target:expr, $($args:tt)+) => {{
198+
if $target {
199+
_internal!("foo: {}", format_args!($($args)+))
200+
}
201+
}};
202+
}
203+
204+
macro_rules! my_concat {
205+
($fmt:literal $(, $e:expr)*) => {
206+
println!(concat!("ERROR: ", $fmt), $($e,)*)
207+
}
208+
}
209+
210+
macro_rules! my_good_macro {
211+
($fmt:literal $(, $e:expr)* $(,)?) => {
212+
println!($fmt $(, $e)*)
213+
}
214+
}
215+
216+
macro_rules! my_bad_macro {
217+
($fmt:literal, $($e:expr),*) => {
218+
println!($fmt, $($e,)*)
219+
}
220+
}
221+
222+
macro_rules! my_bad_macro2 {
223+
($fmt:literal) => {
224+
let s = $fmt.clone();
225+
println!("{}", s);
226+
};
227+
($fmt:literal, $($e:expr)+) => {
228+
println!($fmt, $($e,)*)
229+
};
230+
}
231+
232+
// This abomination was suggested by @Alexendoo, may the Rust gods have mercy on their soul...
233+
// https://github.com/rust-lang/rust-clippy/pull/9948#issuecomment-1327965962
234+
macro_rules! used_twice {
235+
(
236+
large = $large:literal,
237+
small = $small:literal,
238+
$val:expr,
239+
) => {
240+
if $val < 5 {
241+
println!($small, $val);
242+
} else {
243+
println!($large, $val);
244+
}
245+
};
246+
}
247+
248+
fn tester2() {
249+
let local_i32 = 1;
250+
my_println2_args!(true, "{}", local_i32);
251+
my_println2!(true, "{}", local_i32);
252+
my_concat!("{}", local_i32);
253+
my_good_macro!("{}", local_i32);
254+
my_good_macro!("{}", local_i32,);
255+
256+
// FIXME: Broken false positives, currently unhandled
257+
my_bad_macro!("{}", local_i32);
258+
my_bad_macro2!("{}", local_i32);
259+
used_twice! {
260+
large = "large value: {}",
261+
small = "small value: {}",
262+
local_i32,
263+
};
264+
}

0 commit comments

Comments
 (0)