2021-10-21 06:11:36 -05:00
|
|
|
#![warn(clippy::to_string_in_format_args)]
|
2022-10-06 02:44:38 -05:00
|
|
|
#![allow(unused)]
|
|
|
|
#![allow(
|
|
|
|
clippy::assertions_on_constants,
|
2022-10-23 08:18:45 -05:00
|
|
|
clippy::double_parens,
|
2022-10-06 02:44:38 -05:00
|
|
|
clippy::eq_op,
|
|
|
|
clippy::print_literal,
|
|
|
|
clippy::uninlined_format_args
|
|
|
|
)]
|
2021-10-21 06:11:36 -05:00
|
|
|
|
2024-09-24 04:58:04 -05:00
|
|
|
use std::io::{Write, stdout};
|
2021-10-21 06:11:36 -05:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::panic::Location;
|
|
|
|
|
|
|
|
struct Somewhere;
|
|
|
|
|
2024-02-08 13:24:42 -06:00
|
|
|
#[allow(clippy::to_string_trait_impl)]
|
2021-10-21 06:11:36 -05:00
|
|
|
impl ToString for Somewhere {
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
String::from("somewhere")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct X(u32);
|
|
|
|
|
|
|
|
impl Deref for X {
|
|
|
|
type Target = u32;
|
|
|
|
|
|
|
|
fn deref(&self) -> &u32 {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Y<'a>(&'a X);
|
|
|
|
|
|
|
|
impl<'a> Deref for Y<'a> {
|
|
|
|
type Target = &'a X;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Z(u32);
|
|
|
|
|
|
|
|
impl Deref for Z {
|
|
|
|
type Target = u32;
|
|
|
|
|
|
|
|
fn deref(&self) -> &u32 {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for Z {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "Z")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! my_macro {
|
|
|
|
() => {
|
|
|
|
// here be dragons, do not enter (or lint)
|
|
|
|
println!("error: something failed at {}", Location::caller().to_string());
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! my_other_macro {
|
|
|
|
() => {
|
|
|
|
Location::caller().to_string()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let x = &X(1);
|
|
|
|
let x_ref = &x;
|
|
|
|
|
|
|
|
let _ = format!("error: something failed at {}", Location::caller().to_string());
|
|
|
|
let _ = write!(
|
|
|
|
stdout(),
|
|
|
|
"error: something failed at {}",
|
|
|
|
Location::caller().to_string()
|
|
|
|
);
|
|
|
|
let _ = writeln!(
|
|
|
|
stdout(),
|
|
|
|
"error: something failed at {}",
|
|
|
|
Location::caller().to_string()
|
|
|
|
);
|
|
|
|
print!("error: something failed at {}", Location::caller().to_string());
|
|
|
|
println!("error: something failed at {}", Location::caller().to_string());
|
|
|
|
eprint!("error: something failed at {}", Location::caller().to_string());
|
|
|
|
eprintln!("error: something failed at {}", Location::caller().to_string());
|
|
|
|
let _ = format_args!("error: something failed at {}", Location::caller().to_string());
|
|
|
|
assert!(true, "error: something failed at {}", Location::caller().to_string());
|
|
|
|
assert_eq!(0, 0, "error: something failed at {}", Location::caller().to_string());
|
|
|
|
assert_ne!(0, 0, "error: something failed at {}", Location::caller().to_string());
|
|
|
|
panic!("error: something failed at {}", Location::caller().to_string());
|
|
|
|
println!("{}", X(1).to_string());
|
|
|
|
println!("{}", Y(&X(1)).to_string());
|
|
|
|
println!("{}", Z(1).to_string());
|
|
|
|
println!("{}", x.to_string());
|
|
|
|
println!("{}", x_ref.to_string());
|
2021-11-04 07:52:36 -05:00
|
|
|
// https://github.com/rust-lang/rust-clippy/issues/7903
|
|
|
|
println!("{foo}{bar}", foo = "foo".to_string(), bar = "bar");
|
|
|
|
println!("{foo}{bar}", foo = "foo", bar = "bar".to_string());
|
|
|
|
println!("{foo}{bar}", bar = "bar".to_string(), foo = "foo");
|
|
|
|
println!("{foo}{bar}", bar = "bar", foo = "foo".to_string());
|
2024-06-13 05:30:48 -05:00
|
|
|
println!("{}", my_other_macro!().to_string());
|
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: something failed at {}", Somewhere.to_string());
|
2021-11-04 07:52:36 -05:00
|
|
|
// The next two tests are negative because caching the string might be faster than calling `<X as
|
|
|
|
// Display>::fmt` twice.
|
2021-10-21 06:11:36 -05:00
|
|
|
println!("{} and again {0}", x.to_string());
|
2021-11-04 07:52:36 -05:00
|
|
|
println!("{foo}{foo}", foo = "foo".to_string());
|
2021-10-21 06:11:36 -05:00
|
|
|
my_macro!();
|
|
|
|
println!("error: something failed at {}", my_other_macro!());
|
2021-11-04 07:52:36 -05:00
|
|
|
// https://github.com/rust-lang/rust-clippy/issues/7903
|
|
|
|
println!("{foo}{foo:?}", foo = "foo".to_string());
|
2022-10-23 08:18:45 -05:00
|
|
|
print!("{}", (Location::caller().to_string()));
|
|
|
|
print!("{}", ((Location::caller()).to_string()));
|
2021-10-21 06:11:36 -05:00
|
|
|
}
|
2022-08-31 08:24:45 -05:00
|
|
|
|
|
|
|
fn issue8643(vendor_id: usize, product_id: usize, name: &str) {
|
|
|
|
println!(
|
|
|
|
"{:<9} {:<10} {}",
|
|
|
|
format!("0x{:x}", vendor_id),
|
|
|
|
format!("0x{:x}", product_id),
|
|
|
|
name
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/rust-lang/rust-clippy/issues/8855
|
|
|
|
mod issue_8855 {
|
|
|
|
#![allow(dead_code)]
|
|
|
|
|
|
|
|
struct A {}
|
|
|
|
|
|
|
|
impl std::fmt::Display for A {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
write!(f, "test")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let a = A {};
|
|
|
|
let b = A {};
|
|
|
|
|
|
|
|
let x = format!("{} {}", a, b.to_string());
|
|
|
|
dbg!(x);
|
|
|
|
|
|
|
|
let x = format!("{:>6} {:>6}", a, b.to_string());
|
|
|
|
dbg!(x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/rust-lang/rust-clippy/issues/9256
|
|
|
|
mod issue_9256 {
|
|
|
|
#![allow(dead_code)]
|
|
|
|
|
|
|
|
fn print_substring(original: &str) {
|
|
|
|
assert!(original.len() > 10);
|
|
|
|
println!("{}", original[..10].to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
print_substring("Hello, world!");
|
|
|
|
}
|
|
|
|
}
|