fork/rust
git clone https://git.julia.blue/fork/rust
3bcad65fbf6
master
1#![warn(clippy::useless_format)] 2#![allow( 3 unused_tuple_struct_fields, 4 clippy::print_literal, 5 clippy::redundant_clone, 6 clippy::to_string_in_format_args, 7 clippy::needless_borrow, 8 clippy::uninlined_format_args, 9 clippy::needless_raw_string_hashes, 10 clippy::useless_vec 11)] 12 13struct Foo(pub String); 14 15macro_rules! foo { 16 ($($t:tt)*) => (Foo(format!($($t)*))) 17} 18 19fn main() { 20 "foo".to_string(); 21 "{}".to_string(); 22 "{} abc {}".to_string(); 23 r##"foo {} 24" bar"##.to_string(); 25 26 let _ = String::new(); 27 28 "foo".to_string(); 29 format!("{:?}", "foo"); // Don't warn about `Debug`. 30 format!("{:8}", "foo"); 31 format!("{:width$}", "foo", width = 8); 32 format!("foo {}", "bar"); 33 format!("{} bar", "foo"); 34 35 let arg = String::new(); 36 arg.to_string(); 37 format!("{:?}", arg); // Don't warn about debug. 38 format!("{:8}", arg); 39 format!("{:width$}", arg, width = 8); 40 format!("foo {}", arg); 41 format!("{} bar", arg); 42 43 // We don’t want to warn for non-string args; see issue #697. 44 format!("{}", 42); 45 format!("{:?}", 42); 46 format!("{:+}", 42); 47 format!("foo {}", 42); 48 format!("{} bar", 42); 49 50 // We only want to warn about `format!` itself. 51 println!("foo"); 52 println!("{}", "foo"); 53 println!("foo {}", "foo"); 54 println!("{}", 42); 55 println!("foo {}", 42); 56 57 // A `format!` inside a macro should not trigger a warning. 58 foo!("should not warn"); 59 60 // Precision on string means slicing without panicking on size. 61 format!("{:.1}", "foo"); // Could be `"foo"[..1]` 62 format!("{:.10}", "foo"); // Could not be `"foo"[..10]` 63 format!("{:.prec$}", "foo", prec = 1); 64 format!("{:.prec$}", "foo", prec = 10); 65 66 42.to_string(); 67 let x = std::path::PathBuf::from("/bar/foo/qux"); 68 x.display().to_string(); 69 70 // False positive 71 let a = "foo".to_string(); 72 let _ = Some(a + "bar"); 73 74 // Wrap it with braces 75 let v: Vec<String> = vec!["foo".to_string(), "bar".to_string()]; 76 let _s: String = (&*v.join("\n")).to_string(); 77 78 format!("prepend {:+}", "s"); 79 80 // Issue #8290 81 let x = "foo"; 82 let _ = x.to_string(); 83 let _ = format!("{x:?}"); // Don't lint on debug 84 let _ = x.to_string(); 85 86 // Issue #9234 87 let abc = "abc"; 88 let _ = abc.to_string(); 89 let xx = "xx"; 90 let _ = xx.to_string(); 91}