]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/tests/ui/inherent_to_string.rs
Merge tag 'debian/1.52.1+dfsg1-1_exp2' into proxmox/buster
[rustc.git] / src / tools / clippy / tests / ui / inherent_to_string.rs
CommitLineData
f20569fa
XL
1#![warn(clippy::inherent_to_string)]
2#![deny(clippy::inherent_to_string_shadow_display)]
3#![allow(clippy::many_single_char_names)]
4
5use std::fmt;
6
7trait FalsePositive {
8 fn to_string(&self) -> String;
9}
10
11struct A;
12struct B;
13struct C;
14struct D;
15struct E;
16struct F;
17struct G;
18
19impl 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
32fn to_string() -> String {
33 "free to_string()".to_string()
34}
35
36impl B {
37 // Should not be detected, wrong return type
38 fn to_string(&self) -> i32 {
39 42
40 }
41}
42
43impl 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
50impl fmt::Display for C {
51 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
52 write!(f, "impl Display for C")
53 }
54}
55
56impl 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
63impl 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
70impl 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
77impl 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
84fn 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}