]> git.lizzy.rs Git - rust.git/blob - tests/ui/inherent_to_string.rs
Auto merge of #4478 - tsurai:master, 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 #![allow(clippy::many_single_char_names)]
4
5 use std::fmt;
6
7 trait FalsePositive {
8     fn to_string(&self) -> String;
9 }
10
11 struct A;
12 struct B;
13 struct C;
14 struct D;
15 struct E;
16 struct F;
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 fn main() {
77     let a = A;
78     a.to_string();
79     a.to_str();
80
81     to_string();
82
83     let b = B;
84     b.to_string();
85
86     let c = C;
87     C.to_string();
88
89     let d = D;
90     d.to_string();
91
92     E::to_string();
93
94     let f = F;
95     f.to_string(1);
96 }