]> git.lizzy.rs Git - rust.git/blob - tests/ui/inherent_to_string.rs
Implement lint for inherent to_string() method.
[rust.git] / tests / ui / inherent_to_string.rs
1 #![warn(clippy::inherent_to_string)]
2 //#![deny(clippy::inherent_to_string_shadow)]
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
16 impl A {
17     // Should be detected; emit warning
18     fn to_string(&self) -> String {
19         "A.to_string()".to_string()
20     }
21
22     // Should not be detected as it does not match the function signature
23     fn to_str(&self) -> String {
24         "A.to_str()".to_string()
25     }
26 }
27
28 // Should not be detected as it is a free function
29 fn to_string() -> String {
30     "free to_string()".to_string()
31 }
32
33 impl B {
34     // Should not be detected, wrong return type
35     fn to_string(&self) -> i32 {
36         42
37     }
38 }
39
40 impl C {
41     // Should be detected and emit error as C also implements Display
42     fn to_string(&self) -> String {
43         "C.to_string()".to_string()
44     }
45 }
46
47 impl fmt::Display for C {
48     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
49         write!(f, "impl Display for C")
50     }
51 }
52
53 impl FalsePositive for D {
54     // Should not be detected, as it is a trait function
55     fn to_string(&self) -> String {
56         "impl FalsePositive for D".to_string()
57     }
58 }
59
60 impl E {
61     // Should not be detected, as it is not bound to an instance
62     fn to_string() -> String {
63         "E::to_string()".to_string()
64     }
65 }
66
67 fn main() {
68     let a = A;
69     a.to_string();
70     a.to_str();
71
72     to_string();
73
74     let b = B;
75     b.to_string();
76
77     let c = C;
78     C.to_string();
79
80     let d = D;
81     d.to_string();
82
83     E::to_string();
84 }