-
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
11 changed files
with
362 additions
and
5 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()) | ||
); | ||
} |
88 changes: 88 additions & 0 deletions
88
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,88 @@ | ||
#![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, | ||
// }; | ||
} |
88 changes: 88 additions & 0 deletions
88
tests/ui-toml/include_custom_format_macros/uninlined_format_args.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,88 @@ | ||
#![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.