-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
675 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include-custom-format-macros = true |
63 changes: 63 additions & 0 deletions
63
tests/ui-toml/include_custom_format_macros/format_args_unfixable.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#![warn(clippy::format_in_format_args, clippy::to_string_in_format_args)] | ||
#![allow(clippy::assertions_on_constants, clippy::eq_op, clippy::uninlined_format_args)] | ||
|
||
use std::io::{stdout, Error, ErrorKind, Write}; | ||
use std::ops::Deref; | ||
use std::panic::Location; | ||
|
||
macro_rules! _internal { | ||
($($args:tt)*) => { | ||
println!("{}", format_args!($($args)*)) | ||
}; | ||
} | ||
|
||
macro_rules! my_println2 { | ||
($target:expr, $($args:tt)+) => {{ | ||
if $target { | ||
_internal!($($args)+) | ||
} | ||
}}; | ||
} | ||
|
||
macro_rules! my_println2_args { | ||
($target:expr, $($args:tt)+) => {{ | ||
if $target { | ||
_internal!("foo: {}", format_args!($($args)+)) | ||
} | ||
}}; | ||
} | ||
|
||
fn main() { | ||
let error = Error::new(ErrorKind::Other, "bad thing"); | ||
|
||
my_println2!(true, "error: {}", format!("something failed at {}", Location::caller())); | ||
my_println2!( | ||
true, | ||
"{}: {}", | ||
error, | ||
format!("something failed at {}", Location::caller()) | ||
); | ||
|
||
my_println2!( | ||
true, | ||
"error: {}", | ||
format!("something failed at {}", Location::caller().to_string()) | ||
); | ||
my_println2!( | ||
true, | ||
"{}: {}", | ||
error, | ||
format!("something failed at {}", Location::caller().to_string()) | ||
); | ||
|
||
my_println2!(true, "error: {}", Location::caller().to_string()); | ||
my_println2!(true, "{}: {}", error, Location::caller().to_string()); | ||
|
||
my_println2_args!(true, "error: {}", format!("something failed at {}", Location::caller())); | ||
my_println2_args!( | ||
true, | ||
"{}: {}", | ||
error, | ||
format!("something failed at {}", Location::caller()) | ||
); | ||
} |
102 changes: 102 additions & 0 deletions
102
tests/ui-toml/include_custom_format_macros/format_args_unfixable.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
error: `format!` in `my_println2!` args | ||
--> $DIR/format_args_unfixable.rs:33:5 | ||
| | ||
LL | my_println2!(true, "error: {}", format!("something failed at {}", Location::caller())); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: combine the `format!(..)` arguments with the outer `my_println2!(..)` call | ||
= help: or consider changing `format!` to `format_args!` | ||
= note: `-D clippy::format-in-format-args` implied by `-D warnings` | ||
|
||
error: `format!` in `my_println2!` args | ||
--> $DIR/format_args_unfixable.rs:34:5 | ||
| | ||
LL | / my_println2!( | ||
LL | | true, | ||
LL | | "{}: {}", | ||
LL | | error, | ||
LL | | format!("something failed at {}", Location::caller()) | ||
LL | | ); | ||
| |_____^ | ||
| | ||
= help: combine the `format!(..)` arguments with the outer `my_println2!(..)` call | ||
= help: or consider changing `format!` to `format_args!` | ||
|
||
error: `format!` in `my_println2!` args | ||
--> $DIR/format_args_unfixable.rs:41:5 | ||
| | ||
LL | / my_println2!( | ||
LL | | true, | ||
LL | | "error: {}", | ||
LL | | format!("something failed at {}", Location::caller().to_string()) | ||
LL | | ); | ||
| |_____^ | ||
| | ||
= help: combine the `format!(..)` arguments with the outer `my_println2!(..)` call | ||
= help: or consider changing `format!` to `format_args!` | ||
|
||
error: `to_string` applied to a type that implements `Display` in `format!` args | ||
--> $DIR/format_args_unfixable.rs:44:61 | ||
| | ||
LL | format!("something failed at {}", Location::caller().to_string()) | ||
| ^^^^^^^^^^^^ help: remove this | ||
| | ||
= note: `-D clippy::to-string-in-format-args` implied by `-D warnings` | ||
|
||
error: `format!` in `my_println2!` args | ||
--> $DIR/format_args_unfixable.rs:46:5 | ||
| | ||
LL | / my_println2!( | ||
LL | | true, | ||
LL | | "{}: {}", | ||
LL | | error, | ||
LL | | format!("something failed at {}", Location::caller().to_string()) | ||
LL | | ); | ||
| |_____^ | ||
| | ||
= help: combine the `format!(..)` arguments with the outer `my_println2!(..)` call | ||
= help: or consider changing `format!` to `format_args!` | ||
|
||
error: `to_string` applied to a type that implements `Display` in `format!` args | ||
--> $DIR/format_args_unfixable.rs:50:61 | ||
| | ||
LL | format!("something failed at {}", Location::caller().to_string()) | ||
| ^^^^^^^^^^^^ help: remove this | ||
|
||
error: `to_string` applied to a type that implements `Display` in `my_println2!` args | ||
--> $DIR/format_args_unfixable.rs:53:55 | ||
| | ||
LL | my_println2!(true, "error: {}", Location::caller().to_string()); | ||
| ^^^^^^^^^^^^ help: remove this | ||
|
||
error: `to_string` applied to a type that implements `Display` in `my_println2!` args | ||
--> $DIR/format_args_unfixable.rs:54:59 | ||
| | ||
LL | my_println2!(true, "{}: {}", error, Location::caller().to_string()); | ||
| ^^^^^^^^^^^^ help: remove this | ||
|
||
error: `format!` in `my_println2_args!` args | ||
--> $DIR/format_args_unfixable.rs:56:5 | ||
| | ||
LL | my_println2_args!(true, "error: {}", format!("something failed at {}", Location::caller())); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: combine the `format!(..)` arguments with the outer `my_println2_args!(..)` call | ||
= help: or consider changing `format!` to `format_args!` | ||
|
||
error: `format!` in `my_println2_args!` args | ||
--> $DIR/format_args_unfixable.rs:57:5 | ||
| | ||
LL | / my_println2_args!( | ||
LL | | true, | ||
LL | | "{}: {}", | ||
LL | | error, | ||
LL | | format!("something failed at {}", Location::caller()) | ||
LL | | ); | ||
| |_____^ | ||
| | ||
= help: combine the `format!(..)` arguments with the outer `my_println2_args!(..)` call | ||
= help: or consider changing `format!` to `format_args!` | ||
|
||
error: aborting due to 10 previous errors | ||
|
89 changes: 89 additions & 0 deletions
89
tests/ui-toml/include_custom_format_macros/uninlined_format_args.fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// run-rustfix | ||
#![warn(clippy::uninlined_format_args)] | ||
#![allow(named_arguments_used_positionally, unused_imports, unused_macros, unused_variables)] | ||
#![allow(clippy::eq_op, clippy::format_in_format_args, clippy::print_literal)] | ||
|
||
macro_rules! _internal { | ||
($($args:tt)*) => { | ||
println!("{}", format_args!($($args)*)) | ||
}; | ||
} | ||
|
||
macro_rules! my_println2 { | ||
($target:expr, $($args:tt)+) => {{ | ||
if $target { | ||
_internal!($($args)+) | ||
} | ||
}}; | ||
} | ||
|
||
macro_rules! my_println2_args { | ||
($target:expr, $($args:tt)+) => {{ | ||
if $target { | ||
_internal!("foo: {}", format_args!($($args)+)) | ||
} | ||
}}; | ||
} | ||
|
||
macro_rules! my_concat { | ||
($fmt:literal $(, $e:expr)*) => { | ||
println!(concat!("ERROR: ", $fmt), $($e,)*) | ||
} | ||
} | ||
|
||
macro_rules! my_good_macro { | ||
($fmt:literal $(, $e:expr)* $(,)?) => { | ||
println!($fmt $(, $e)*) | ||
} | ||
} | ||
|
||
macro_rules! my_bad_macro { | ||
($fmt:literal, $($e:expr),*) => { | ||
println!($fmt, $($e,)*) | ||
} | ||
} | ||
|
||
macro_rules! my_bad_macro2 { | ||
($fmt:literal) => { | ||
let s = $fmt.clone(); | ||
println!("{}", s); | ||
}; | ||
($fmt:literal, $($e:expr)+) => { | ||
println!($fmt, $($e,)*) | ||
}; | ||
} | ||
|
||
// This abomination was suggested by @Alexendoo, may the Rust gods have mercy on their soul... | ||
// https://github.com/rust-lang/rust-clippy/pull/9948#issuecomment-1327965962 | ||
macro_rules! used_twice { | ||
( | ||
large = $large:literal, | ||
small = $small:literal, | ||
$val:expr, | ||
) => { | ||
if $val < 5 { | ||
println!($small, $val); | ||
} else { | ||
println!($large, $val); | ||
} | ||
}; | ||
} | ||
|
||
fn main() { | ||
let local_i32 = 1; | ||
println!("val='{local_i32}'"); | ||
my_println2_args!(true, "{}", local_i32); | ||
my_println2!(true, "{}", local_i32); | ||
my_concat!("{}", local_i32); | ||
my_good_macro!("{local_i32}"); | ||
my_good_macro!("{local_i32}",); | ||
|
||
// FIXME: Broken false positives, currently unhandled | ||
// my_bad_macro!("{}", local_i32); | ||
// my_bad_macro2!("{}", local_i32); | ||
// used_twice! { | ||
// large = "large value: {}", | ||
// small = "small value: {}", | ||
// local_i32, | ||
// }; | ||
} |
Oops, something went wrong.