fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

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