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