Skip to content

Commit

Permalink
add additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FoseFx committed Apr 13, 2022
1 parent 6c62166 commit 1216903
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 8 deletions.
35 changes: 35 additions & 0 deletions tests/ui/trim_split_whitespace.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,31 @@ impl DerefStrAndCustom {
fn split_whitespace(self) {}
}

struct DerefStrAndCustomSplit(&'static str);
impl std::ops::Deref for DerefStrAndCustomSplit {
type Target = str;
fn deref(&self) -> &Self::Target {
self.0
}
}
impl DerefStrAndCustomSplit {
#[allow(dead_code)]
fn split_whitespace(self) {}
}

struct DerefStrAndCustomTrim(&'static str);
impl std::ops::Deref for DerefStrAndCustomTrim {
type Target = str;
fn deref(&self) -> &Self::Target {
self.0
}
}
impl DerefStrAndCustomTrim {
fn trim(self) -> Self {
self
}
}

fn main() {
// &str
let _ = " A B C ".split_whitespace(); // should trigger lint
Expand All @@ -52,4 +77,14 @@ fn main() {
// Deref<Target=str> + custom impl
let s = DerefStrAndCustom(" A B C ");
let _ = s.trim().split_whitespace(); // should not trigger lint

// Deref<Target=str> + only custom split_ws() impl
let s = DerefStrAndCustomSplit(" A B C ");
let _ = s.split_whitespace(); // should trigger lint
// Expl: trim() is called on str (deref) and returns &str.
// Thus split_ws() is called on str as well and the custom impl on S is unused

// Deref<Target=str> + only custom trim() impl
let s = DerefStrAndCustomTrim(" A B C ");
let _ = s.trim().split_whitespace(); // should not trigger lint
}
35 changes: 35 additions & 0 deletions tests/ui/trim_split_whitespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,31 @@ impl DerefStrAndCustom {
fn split_whitespace(self) {}
}

struct DerefStrAndCustomSplit(&'static str);
impl std::ops::Deref for DerefStrAndCustomSplit {
type Target = str;
fn deref(&self) -> &Self::Target {
self.0
}
}
impl DerefStrAndCustomSplit {
#[allow(dead_code)]
fn split_whitespace(self) {}
}

struct DerefStrAndCustomTrim(&'static str);
impl std::ops::Deref for DerefStrAndCustomTrim {
type Target = str;
fn deref(&self) -> &Self::Target {
self.0
}
}
impl DerefStrAndCustomTrim {
fn trim(self) -> Self {
self
}
}

fn main() {
// &str
let _ = " A B C ".trim().split_whitespace(); // should trigger lint
Expand All @@ -52,4 +77,14 @@ fn main() {
// Deref<Target=str> + custom impl
let s = DerefStrAndCustom(" A B C ");
let _ = s.trim().split_whitespace(); // should not trigger lint

// Deref<Target=str> + only custom split_ws() impl
let s = DerefStrAndCustomSplit(" A B C ");
let _ = s.trim().split_whitespace(); // should trigger lint
// Expl: trim() is called on str (deref) and returns &str.
// Thus split_ws() is called on str as well and the custom impl on S is unused

// Deref<Target=str> + only custom trim() impl
let s = DerefStrAndCustomTrim(" A B C ");
let _ = s.trim().split_whitespace(); // should not trigger lint
}
22 changes: 14 additions & 8 deletions tests/ui/trim_split_whitespace.stderr
Original file line number Diff line number Diff line change
@@ -1,46 +1,52 @@
error: found call to `str::trim` before `str::split_whitespace`
--> $DIR/trim_split_whitespace.rs:36:23
--> $DIR/trim_split_whitespace.rs:61:23
|
LL | let _ = " A B C ".trim().split_whitespace(); // should trigger lint
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `trim()`: `split_whitespace()`
|
= note: `-D clippy::trim-split-whitespace` implied by `-D warnings`

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

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

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

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

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

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

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

error: aborting due to 8 previous errors

0 comments on commit 1216903

Please sign in to comment.