]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/trim_split_whitespace.rs
New upstream version 1.62.1+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / trim_split_whitespace.rs
1 // run-rustfix
2 #![warn(clippy::trim_split_whitespace)]
3 #![allow(clippy::let_unit_value)]
4
5 struct Custom;
6 impl Custom {
7 fn trim(self) -> Self {
8 self
9 }
10 fn split_whitespace(self) {}
11 }
12
13 struct DerefStr(&'static str);
14 impl std::ops::Deref for DerefStr {
15 type Target = str;
16 fn deref(&self) -> &Self::Target {
17 self.0
18 }
19 }
20
21 struct DerefStrAndCustom(&'static str);
22 impl std::ops::Deref for DerefStrAndCustom {
23 type Target = str;
24 fn deref(&self) -> &Self::Target {
25 self.0
26 }
27 }
28 impl DerefStrAndCustom {
29 fn trim(self) -> Self {
30 self
31 }
32 fn split_whitespace(self) {}
33 }
34
35 struct DerefStrAndCustomSplit(&'static str);
36 impl std::ops::Deref for DerefStrAndCustomSplit {
37 type Target = str;
38 fn deref(&self) -> &Self::Target {
39 self.0
40 }
41 }
42 impl DerefStrAndCustomSplit {
43 #[allow(dead_code)]
44 fn split_whitespace(self) {}
45 }
46
47 struct DerefStrAndCustomTrim(&'static str);
48 impl std::ops::Deref for DerefStrAndCustomTrim {
49 type Target = str;
50 fn deref(&self) -> &Self::Target {
51 self.0
52 }
53 }
54 impl DerefStrAndCustomTrim {
55 fn trim(self) -> Self {
56 self
57 }
58 }
59
60 fn main() {
61 // &str
62 let _ = " A B C ".trim().split_whitespace(); // should trigger lint
63 let _ = " A B C ".trim_start().split_whitespace(); // should trigger lint
64 let _ = " A B C ".trim_end().split_whitespace(); // should trigger lint
65
66 // String
67 let _ = (" A B C ").to_string().trim().split_whitespace(); // should trigger lint
68 let _ = (" A B C ").to_string().trim_start().split_whitespace(); // should trigger lint
69 let _ = (" A B C ").to_string().trim_end().split_whitespace(); // should trigger lint
70
71 // Custom
72 let _ = Custom.trim().split_whitespace(); // should not trigger lint
73
74 // Deref<Target=str>
75 let s = DerefStr(" A B C ");
76 let _ = s.trim().split_whitespace(); // should trigger lint
77
78 // Deref<Target=str> + custom impl
79 let s = DerefStrAndCustom(" A B C ");
80 let _ = s.trim().split_whitespace(); // should not trigger lint
81
82 // Deref<Target=str> + only custom split_ws() impl
83 let s = DerefStrAndCustomSplit(" A B C ");
84 let _ = s.trim().split_whitespace(); // should trigger lint
85 // Expl: trim() is called on str (deref) and returns &str.
86 // Thus split_ws() is called on str as well and the custom impl on S is unused
87
88 // Deref<Target=str> + only custom trim() impl
89 let s = DerefStrAndCustomTrim(" A B C ");
90 let _ = s.trim().split_whitespace(); // should not trigger lint
91 }