2022-10-06 02:44:38 -05:00
|
|
|
#![warn(clippy::format_in_format_args, clippy::to_string_in_format_args)]
|
2023-04-11 08:31:08 -05:00
|
|
|
#![allow(unused)]
|
2022-10-06 02:44:38 -05:00
|
|
|
#![allow(clippy::assertions_on_constants, clippy::eq_op, clippy::uninlined_format_args)]
|
2021-10-21 06:11:36 -05:00
|
|
|
|
2024-09-24 04:58:04 -05:00
|
|
|
use std::io::{Error, ErrorKind, Write, stdout};
|
2021-10-21 06:11:36 -05:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::panic::Location;
|
|
|
|
|
|
|
|
macro_rules! my_macro {
|
|
|
|
() => {
|
|
|
|
// here be dragons, do not enter (or lint)
|
|
|
|
println!("error: {}", format!("something failed at {}", Location::caller()));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! my_other_macro {
|
|
|
|
() => {
|
|
|
|
format!("something failed at {}", Location::caller())
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let error = Error::new(ErrorKind::Other, "bad thing");
|
|
|
|
let x = 'x';
|
|
|
|
|
|
|
|
println!("error: {}", format!("something failed at {}", Location::caller()));
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: `format!` in `println!` args
|
2021-10-21 06:11:36 -05:00
|
|
|
println!("{}: {}", error, format!("something failed at {}", Location::caller()));
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: `format!` in `println!` args
|
2021-10-21 06:11:36 -05:00
|
|
|
println!("{:?}: {}", error, format!("something failed at {}", Location::caller()));
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: `format!` in `println!` args
|
2021-10-21 06:11:36 -05:00
|
|
|
println!("{{}}: {}", format!("something failed at {}", Location::caller()));
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: `format!` in `println!` args
|
2021-10-21 06:11:36 -05:00
|
|
|
println!(r#"error: "{}""#, format!("something failed at {}", Location::caller()));
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: `format!` in `println!` args
|
2021-10-21 06:11:36 -05:00
|
|
|
println!("error: {}", format!(r#"something failed at "{}""#, Location::caller()));
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: `format!` in `println!` args
|
2021-10-21 06:11:36 -05:00
|
|
|
println!("error: {}", format!("something failed at {} {0}", Location::caller()));
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: `format!` in `println!` args
|
2021-10-21 06:11:36 -05:00
|
|
|
let _ = format!("error: {}", format!("something failed at {}", Location::caller()));
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: `format!` in `format!` args
|
2021-10-21 06:11:36 -05:00
|
|
|
let _ = write!(
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: `format!` in `write!` args
|
2021-10-21 06:11:36 -05:00
|
|
|
stdout(),
|
|
|
|
"error: {}",
|
|
|
|
format!("something failed at {}", Location::caller())
|
|
|
|
);
|
|
|
|
let _ = writeln!(
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: `format!` in `writeln!` args
|
2021-10-21 06:11:36 -05:00
|
|
|
stdout(),
|
|
|
|
"error: {}",
|
|
|
|
format!("something failed at {}", Location::caller())
|
|
|
|
);
|
|
|
|
print!("error: {}", format!("something failed at {}", Location::caller()));
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: `format!` in `print!` args
|
2021-10-21 06:11:36 -05:00
|
|
|
eprint!("error: {}", format!("something failed at {}", Location::caller()));
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: `format!` in `eprint!` args
|
2021-10-21 06:11:36 -05:00
|
|
|
eprintln!("error: {}", format!("something failed at {}", Location::caller()));
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: `format!` in `eprintln!` args
|
2021-10-21 06:11:36 -05:00
|
|
|
let _ = format_args!("error: {}", format!("something failed at {}", Location::caller()));
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: `format!` in `format_args!` args
|
2021-10-21 06:11:36 -05:00
|
|
|
assert!(true, "error: {}", format!("something failed at {}", Location::caller()));
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: `format!` in `assert!` args
|
2021-10-21 06:11:36 -05:00
|
|
|
assert_eq!(0, 0, "error: {}", format!("something failed at {}", Location::caller()));
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: `format!` in `assert_eq!` args
|
2021-10-21 06:11:36 -05:00
|
|
|
assert_ne!(0, 0, "error: {}", format!("something failed at {}", Location::caller()));
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: `format!` in `assert_ne!` args
|
2021-10-21 06:11:36 -05:00
|
|
|
panic!("error: {}", format!("something failed at {}", Location::caller()));
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: `format!` in `panic!` args
|
2021-10-21 06:11:36 -05:00
|
|
|
|
2021-11-04 07:52:36 -05:00
|
|
|
// negative tests
|
2021-10-21 06:11:36 -05:00
|
|
|
println!("error: {}", format_args!("something failed at {}", Location::caller()));
|
|
|
|
println!("error: {:>70}", format!("something failed at {}", Location::caller()));
|
|
|
|
println!("error: {} {0}", format!("something failed at {}", Location::caller()));
|
|
|
|
println!("{} and again {0}", format!("hi {}", x));
|
|
|
|
my_macro!();
|
|
|
|
println!("error: {}", my_other_macro!());
|
|
|
|
}
|
2023-04-11 08:31:08 -05:00
|
|
|
|
|
|
|
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 test2() {
|
|
|
|
let error = Error::new(ErrorKind::Other, "bad thing");
|
|
|
|
|
|
|
|
// None of these should be linted without the config change
|
|
|
|
my_println2!(true, "error: {}", format!("something failed at {}", Location::caller()));
|
|
|
|
my_println2!(
|
|
|
|
true,
|
|
|
|
"{}: {}",
|
|
|
|
error,
|
|
|
|
format!("something failed at {}", Location::caller())
|
|
|
|
);
|
|
|
|
|
|
|
|
my_println2_args!(true, "error: {}", format!("something failed at {}", Location::caller()));
|
|
|
|
my_println2_args!(
|
|
|
|
true,
|
|
|
|
"{}: {}",
|
|
|
|
error,
|
|
|
|
format!("something failed at {}", Location::caller())
|
|
|
|
);
|
|
|
|
}
|