-
Notifications
You must be signed in to change notification settings - Fork 1.7k
add trim_split_whitespace
#8575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// run-rustfix | ||
#![warn(clippy::trim_split_whitespace)] | ||
#![allow(clippy::let_unit_value)] | ||
|
||
struct Custom; | ||
impl Custom { | ||
fn trim(self) -> Self { | ||
self | ||
} | ||
fn split_whitespace(self) {} | ||
} | ||
|
||
struct DerefStr(&'static str); | ||
impl std::ops::Deref for DerefStr { | ||
type Target = str; | ||
fn deref(&self) -> &Self::Target { | ||
self.0 | ||
} | ||
} | ||
|
||
struct DerefStrAndCustom(&'static str); | ||
impl std::ops::Deref for DerefStrAndCustom { | ||
type Target = str; | ||
fn deref(&self) -> &Self::Target { | ||
self.0 | ||
} | ||
} | ||
impl DerefStrAndCustom { | ||
fn trim(self) -> Self { | ||
self | ||
} | ||
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 | ||
let _ = " A B C ".split_whitespace(); // should trigger lint | ||
let _ = " A B C ".split_whitespace(); // should trigger lint | ||
|
||
// String | ||
let _ = (" A B C ").to_string().split_whitespace(); // should trigger lint | ||
let _ = (" A B C ").to_string().split_whitespace(); // should trigger lint | ||
let _ = (" A B C ").to_string().split_whitespace(); // should trigger lint | ||
|
||
// Custom | ||
let _ = Custom.trim().split_whitespace(); // should not trigger lint | ||
|
||
// Deref<Target=str> | ||
let s = DerefStr(" A B C "); | ||
let _ = s.split_whitespace(); // should trigger lint | ||
|
||
// 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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// run-rustfix | ||
#![warn(clippy::trim_split_whitespace)] | ||
#![allow(clippy::let_unit_value)] | ||
|
||
struct Custom; | ||
impl Custom { | ||
fn trim(self) -> Self { | ||
self | ||
} | ||
fn split_whitespace(self) {} | ||
} | ||
|
||
struct DerefStr(&'static str); | ||
impl std::ops::Deref for DerefStr { | ||
type Target = str; | ||
fn deref(&self) -> &Self::Target { | ||
self.0 | ||
} | ||
} | ||
|
||
struct DerefStrAndCustom(&'static str); | ||
impl std::ops::Deref for DerefStrAndCustom { | ||
type Target = str; | ||
fn deref(&self) -> &Self::Target { | ||
self.0 | ||
} | ||
} | ||
impl DerefStrAndCustom { | ||
fn trim(self) -> Self { | ||
self | ||
} | ||
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 | ||
let _ = " A B C ".trim_start().split_whitespace(); // should trigger lint | ||
let _ = " A B C ".trim_end().split_whitespace(); // should trigger lint | ||
|
||
// String | ||
let _ = (" A B C ").to_string().trim().split_whitespace(); // should trigger lint | ||
let _ = (" A B C ").to_string().trim_start().split_whitespace(); // should trigger lint | ||
let _ = (" A B C ").to_string().trim_end().split_whitespace(); // should trigger lint | ||
|
||
// Custom | ||
let _ = Custom.trim().split_whitespace(); // should not trigger lint | ||
|
||
// Deref<Target=str> | ||
let s = DerefStr(" A B C "); | ||
let _ = s.trim().split_whitespace(); // should trigger lint | ||
|
||
// Deref<Target=str> + custom impl | ||
let s = DerefStrAndCustom(" A B C "); | ||
let _ = s.trim().split_whitespace(); // should not trigger lint | ||
flip1995 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
error: found call to `str::trim` before `str::split_whitespace` | ||
--> $DIR/trim_split_whitespace.rs:62:23 | ||
| | ||
LL | let _ = " A B C ".trim().split_whitespace(); // should trigger lint | ||
| ^^^^^^^ help: remove `trim()` | ||
| | ||
= 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:63:23 | ||
| | ||
LL | let _ = " A B C ".trim_start().split_whitespace(); // should trigger lint | ||
| ^^^^^^^^^^^^^ help: remove `trim_start()` | ||
|
||
error: found call to `str::trim_end` before `str::split_whitespace` | ||
--> $DIR/trim_split_whitespace.rs:64:23 | ||
| | ||
LL | let _ = " A B C ".trim_end().split_whitespace(); // should trigger lint | ||
| ^^^^^^^^^^^ help: remove `trim_end()` | ||
|
||
error: found call to `str::trim` before `str::split_whitespace` | ||
--> $DIR/trim_split_whitespace.rs:67:37 | ||
| | ||
LL | let _ = (" A B C ").to_string().trim().split_whitespace(); // should trigger lint | ||
| ^^^^^^^ help: remove `trim()` | ||
|
||
error: found call to `str::trim_start` before `str::split_whitespace` | ||
--> $DIR/trim_split_whitespace.rs:68:37 | ||
| | ||
LL | let _ = (" A B C ").to_string().trim_start().split_whitespace(); // should trigger lint | ||
| ^^^^^^^^^^^^^ help: remove `trim_start()` | ||
|
||
error: found call to `str::trim_end` before `str::split_whitespace` | ||
--> $DIR/trim_split_whitespace.rs:69:37 | ||
| | ||
LL | let _ = (" A B C ").to_string().trim_end().split_whitespace(); // should trigger lint | ||
| ^^^^^^^^^^^ help: remove `trim_end()` | ||
|
||
error: found call to `str::trim` before `str::split_whitespace` | ||
--> $DIR/trim_split_whitespace.rs:76:15 | ||
| | ||
LL | let _ = s.trim().split_whitespace(); // should trigger lint | ||
| ^^^^^^^ help: remove `trim()` | ||
|
||
error: found call to `str::trim` before `str::split_whitespace` | ||
--> $DIR/trim_split_whitespace.rs:84:15 | ||
| | ||
LL | let _ = s.trim().split_whitespace(); // should trigger lint | ||
| ^^^^^^^ help: remove `trim()` | ||
|
||
error: aborting due to 8 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.