]> git.lizzy.rs Git - rust.git/blob - tests/ui/inherent_to_string.rs
Auto merge of #8359 - flip1995:rustup, r=flip1995
[rust.git] / tests / ui / inherent_to_string.rs
1 #![warn(clippy::inherent_to_string)]
2 #![deny(clippy::inherent_to_string_shadow_display)]
3
4 use std::fmt;
5
6 trait FalsePositive {
7     fn to_string(&self) -> String;
8 }
9
10 struct A;
11 struct B;
12 struct C;
13 struct D;
14 struct E;
15 struct F;
16 struct G;
17
18 impl A {
19     // Should be detected; emit warning
20     fn to_string(&self) -> String {
21         "A.to_string()".to_string()
22     }
23
24     // Should not be detected as it does not match the function signature
25     fn to_str(&self) -> String {
26         "A.to_str()".to_string()
27     }
28 }
29
30 // Should not be detected as it is a free function
31 fn to_string() -> String {
32     "free to_string()".to_string()
33 }
34
35 impl B {
36     // Should not be detected, wrong return type
37     fn to_string(&self) -> i32 {
38         42
39     }
40 }
41
42 impl C {
43     // Should be detected and emit error as C also implements Display
44     fn to_string(&self) -> String {
45         "C.to_string()".to_string()
46     }
47 }
48
49 impl fmt::Display for C {
50     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
51         write!(f, "impl Display for C")
52     }
53 }
54
55 impl FalsePositive for D {
56     // Should not be detected, as it is a trait function
57     fn to_string(&self) -> String {
58         "impl FalsePositive for D".to_string()
59     }
60 }
61
62 impl E {
63     // Should not be detected, as it is not bound to an instance
64     fn to_string() -> String {
65         "E::to_string()".to_string()
66     }
67 }
68
69 impl F {
70     // Should not be detected, as it does not match the function signature
71     fn to_string(&self, _i: i32) -> String {
72         "F.to_string()".to_string()
73     }
74 }
75
76 impl G {
77     // Should not be detected, as it does not match the function signature
78     fn to_string<const _N: usize>(&self) -> String {
79         "G.to_string()".to_string()
80     }
81 }
82
83 fn main() {
84     let a = A;
85     a.to_string();
86     a.to_str();
87
88     to_string();
89
90     let b = B;
91     b.to_string();
92
93     let c = C;
94     C.to_string();
95
96     let d = D;
97     d.to_string();
98
99     E::to_string();
100
101     let f = F;
102     f.to_string(1);
103
104     let g = G;
105     g.to_string::<1>();
106 }