fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
4.1 KiB166 linesraw
1#![warn(clippy::to_string_in_format_args)]
2#![allow(unused)]
3#![allow(
4    clippy::assertions_on_constants,
5    clippy::double_parens,
6    clippy::eq_op,
7    clippy::print_literal,
8    clippy::uninlined_format_args
9)]
10
11use std::io::{stdout, Write};
12use std::ops::Deref;
13use std::panic::Location;
14
15struct Somewhere;
16
17impl ToString for Somewhere {
18    fn to_string(&self) -> String {
19        String::from("somewhere")
20    }
21}
22
23struct X(u32);
24
25impl Deref for X {
26    type Target = u32;
27
28    fn deref(&self) -> &u32 {
29        &self.0
30    }
31}
32
33struct Y<'a>(&'a X);
34
35impl<'a> Deref for Y<'a> {
36    type Target = &'a X;
37
38    fn deref(&self) -> &Self::Target {
39        &self.0
40    }
41}
42
43struct Z(u32);
44
45impl Deref for Z {
46    type Target = u32;
47
48    fn deref(&self) -> &u32 {
49        &self.0
50    }
51}
52
53impl std::fmt::Display for Z {
54    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55        write!(f, "Z")
56    }
57}
58
59macro_rules! my_macro {
60    () => {
61        // here be dragons, do not enter (or lint)
62        println!("error: something failed at {}", Location::caller().to_string());
63    };
64}
65
66macro_rules! my_other_macro {
67    () => {
68        Location::caller().to_string()
69    };
70}
71
72fn main() {
73    let x = &X(1);
74    let x_ref = &x;
75
76    let _ = format!("error: something failed at {}", Location::caller());
77    let _ = write!(
78        stdout(),
79        "error: something failed at {}",
80        Location::caller()
81    );
82    let _ = writeln!(
83        stdout(),
84        "error: something failed at {}",
85        Location::caller()
86    );
87    print!("error: something failed at {}", Location::caller());
88    println!("error: something failed at {}", Location::caller());
89    eprint!("error: something failed at {}", Location::caller());
90    eprintln!("error: something failed at {}", Location::caller());
91    let _ = format_args!("error: something failed at {}", Location::caller());
92    assert!(true, "error: something failed at {}", Location::caller());
93    assert_eq!(0, 0, "error: something failed at {}", Location::caller());
94    assert_ne!(0, 0, "error: something failed at {}", Location::caller());
95    panic!("error: something failed at {}", Location::caller());
96    println!("{}", *X(1));
97    println!("{}", ***Y(&X(1)));
98    println!("{}", Z(1));
99    println!("{}", **x);
100    println!("{}", ***x_ref);
101    // https://github.com/rust-lang/rust-clippy/issues/7903
102    println!("{foo}{bar}", foo = "foo", bar = "bar");
103    println!("{foo}{bar}", foo = "foo", bar = "bar");
104    println!("{foo}{bar}", bar = "bar", foo = "foo");
105    println!("{foo}{bar}", bar = "bar", foo = "foo");
106
107    // negative tests
108    println!("error: something failed at {}", Somewhere.to_string());
109    // The next two tests are negative because caching the string might be faster than calling `<X as
110    // Display>::fmt` twice.
111    println!("{} and again {0}", x.to_string());
112    println!("{foo}{foo}", foo = "foo".to_string());
113    my_macro!();
114    println!("error: something failed at {}", my_other_macro!());
115    // https://github.com/rust-lang/rust-clippy/issues/7903
116    println!("{foo}{foo:?}", foo = "foo".to_string());
117    print!("{}", (Location::caller()));
118    print!("{}", ((Location::caller())));
119}
120
121fn issue8643(vendor_id: usize, product_id: usize, name: &str) {
122    println!(
123        "{:<9}  {:<10}  {}",
124        format!("0x{:x}", vendor_id),
125        format!("0x{:x}", product_id),
126        name
127    );
128}
129
130// https://github.com/rust-lang/rust-clippy/issues/8855
131mod issue_8855 {
132    #![allow(dead_code)]
133
134    struct A {}
135
136    impl std::fmt::Display for A {
137        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
138            write!(f, "test")
139        }
140    }
141
142    fn main() {
143        let a = A {};
144        let b = A {};
145
146        let x = format!("{} {}", a, b);
147        dbg!(x);
148
149        let x = format!("{:>6} {:>6}", a, b.to_string());
150        dbg!(x);
151    }
152}
153
154// https://github.com/rust-lang/rust-clippy/issues/9256
155mod issue_9256 {
156    #![allow(dead_code)]
157
158    fn print_substring(original: &str) {
159        assert!(original.len() > 10);
160        println!("{}", &original[..10]);
161    }
162
163    fn main() {
164        print_substring("Hello, world!");
165    }
166}