Skip to content

Commit 52c6157

Browse files
committed
add additional tests
1 parent 4941f95 commit 52c6157

3 files changed

+84
-8
lines changed

tests/ui/trim_split_whitespace.fixed

+35
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,31 @@ impl DerefStrAndCustom {
3131
fn split_whitespace(self) {}
3232
}
3333

34+
struct DerefStrAndCustomSplit(&'static str);
35+
impl std::ops::Deref for DerefStrAndCustomSplit {
36+
type Target = str;
37+
fn deref(&self) -> &Self::Target {
38+
self.0
39+
}
40+
}
41+
impl DerefStrAndCustomSplit {
42+
#[allow(dead_code)]
43+
fn split_whitespace(self) {}
44+
}
45+
46+
struct DerefStrAndCustomTrim(&'static str);
47+
impl std::ops::Deref for DerefStrAndCustomTrim {
48+
type Target = str;
49+
fn deref(&self) -> &Self::Target {
50+
self.0
51+
}
52+
}
53+
impl DerefStrAndCustomTrim {
54+
fn trim(self) -> Self {
55+
self
56+
}
57+
}
58+
3459
fn main() {
3560
// &str
3661
let _ = " A B C ".split_whitespace(); // should trigger lint
@@ -52,4 +77,14 @@ fn main() {
5277
// Deref<Target=str> + custom impl
5378
let s = DerefStrAndCustom(" A B C ");
5479
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
5590
}

tests/ui/trim_split_whitespace.rs

+35
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,31 @@ impl DerefStrAndCustom {
3131
fn split_whitespace(self) {}
3232
}
3333

34+
struct DerefStrAndCustomSplit(&'static str);
35+
impl std::ops::Deref for DerefStrAndCustomSplit {
36+
type Target = str;
37+
fn deref(&self) -> &Self::Target {
38+
self.0
39+
}
40+
}
41+
impl DerefStrAndCustomSplit {
42+
#[allow(dead_code)]
43+
fn split_whitespace(self) {}
44+
}
45+
46+
struct DerefStrAndCustomTrim(&'static str);
47+
impl std::ops::Deref for DerefStrAndCustomTrim {
48+
type Target = str;
49+
fn deref(&self) -> &Self::Target {
50+
self.0
51+
}
52+
}
53+
impl DerefStrAndCustomTrim {
54+
fn trim(self) -> Self {
55+
self
56+
}
57+
}
58+
3459
fn main() {
3560
// &str
3661
let _ = " A B C ".trim().split_whitespace(); // should trigger lint
@@ -52,4 +77,14 @@ fn main() {
5277
// Deref<Target=str> + custom impl
5378
let s = DerefStrAndCustom(" A B C ");
5479
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.trim().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
5590
}

tests/ui/trim_split_whitespace.stderr

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,52 @@
11
error: found call to `str::trim` before `str::split_whitespace`
2-
--> $DIR/trim_split_whitespace.rs:36:23
2+
--> $DIR/trim_split_whitespace.rs:61:23
33
|
44
LL | let _ = " A B C ".trim().split_whitespace(); // should trigger lint
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `trim()`: `split_whitespace()`
66
|
77
= note: `-D clippy::trim-split-whitespace` implied by `-D warnings`
88

99
error: found call to `str::trim_start` before `str::split_whitespace`
10-
--> $DIR/trim_split_whitespace.rs:37:23
10+
--> $DIR/trim_split_whitespace.rs:62:23
1111
|
1212
LL | let _ = " A B C ".trim_start().split_whitespace(); // should trigger lint
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `trim_start()`: `split_whitespace()`
1414

1515
error: found call to `str::trim_end` before `str::split_whitespace`
16-
--> $DIR/trim_split_whitespace.rs:38:23
16+
--> $DIR/trim_split_whitespace.rs:63:23
1717
|
1818
LL | let _ = " A B C ".trim_end().split_whitespace(); // should trigger lint
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `trim_end()`: `split_whitespace()`
2020

2121
error: found call to `str::trim` before `str::split_whitespace`
22-
--> $DIR/trim_split_whitespace.rs:41:37
22+
--> $DIR/trim_split_whitespace.rs:66:37
2323
|
2424
LL | let _ = (" A B C ").to_string().trim().split_whitespace(); // should trigger lint
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `trim()`: `split_whitespace()`
2626

2727
error: found call to `str::trim_start` before `str::split_whitespace`
28-
--> $DIR/trim_split_whitespace.rs:42:37
28+
--> $DIR/trim_split_whitespace.rs:67:37
2929
|
3030
LL | let _ = (" A B C ").to_string().trim_start().split_whitespace(); // should trigger lint
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `trim_start()`: `split_whitespace()`
3232

3333
error: found call to `str::trim_end` before `str::split_whitespace`
34-
--> $DIR/trim_split_whitespace.rs:43:37
34+
--> $DIR/trim_split_whitespace.rs:68:37
3535
|
3636
LL | let _ = (" A B C ").to_string().trim_end().split_whitespace(); // should trigger lint
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `trim_end()`: `split_whitespace()`
3838

3939
error: found call to `str::trim` before `str::split_whitespace`
40-
--> $DIR/trim_split_whitespace.rs:50:15
40+
--> $DIR/trim_split_whitespace.rs:75:15
4141
|
4242
LL | let _ = s.trim().split_whitespace(); // should trigger lint
4343
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `trim()`: `split_whitespace()`
4444

45-
error: aborting due to 7 previous errors
45+
error: found call to `str::trim` before `str::split_whitespace`
46+
--> $DIR/trim_split_whitespace.rs:83:15
47+
|
48+
LL | let _ = s.trim().split_whitespace(); // should trigger lint
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `trim()`: `split_whitespace()`
50+
51+
error: aborting due to 8 previous errors
4652

0 commit comments

Comments
 (0)