-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
manual_strip
: use existing identifier instead of placeholder (#14188)
When the manually stripped entity receives a name as the first use through a simple `let` statement, this name can be used in the generated `if let Some(…)` expression instead of a placeholder. Fix #14183 changelog: [`manual_strip`]: reuse existing identifier in suggestion when possible
- Loading branch information
Showing
6 changed files
with
188 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,15 @@ | ||
#![warn(clippy::manual_strip)] | ||
|
||
fn main() { | ||
let s = "abc"; | ||
|
||
if let Some(stripped) = s.strip_prefix("ab") { | ||
//~^ ERROR: stripping a prefix manually | ||
println!("{stripped}{}", stripped); | ||
} | ||
|
||
if let Some(stripped) = s.strip_suffix("bc") { | ||
//~^ ERROR: stripping a suffix manually | ||
println!("{stripped}{}", stripped); | ||
} | ||
} |
This file contains 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,17 @@ | ||
#![warn(clippy::manual_strip)] | ||
|
||
fn main() { | ||
let s = "abc"; | ||
|
||
if s.starts_with("ab") { | ||
let stripped = &s["ab".len()..]; | ||
//~^ ERROR: stripping a prefix manually | ||
println!("{stripped}{}", &s["ab".len()..]); | ||
} | ||
|
||
if s.ends_with("bc") { | ||
let stripped = &s[..s.len() - "bc".len()]; | ||
//~^ ERROR: stripping a suffix manually | ||
println!("{stripped}{}", &s[..s.len() - "bc".len()]); | ||
} | ||
} |
This file contains 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,40 @@ | ||
error: stripping a prefix manually | ||
--> tests/ui/manual_strip_fixable.rs:7:24 | ||
| | ||
LL | let stripped = &s["ab".len()..]; | ||
| ^^^^^^^^^^^^^^^^ | ||
| | ||
note: the prefix was tested here | ||
--> tests/ui/manual_strip_fixable.rs:6:5 | ||
| | ||
LL | if s.starts_with("ab") { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
= note: `-D clippy::manual-strip` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::manual_strip)]` | ||
help: try using the `strip_prefix` method | ||
| | ||
LL ~ if let Some(stripped) = s.strip_prefix("ab") { | ||
LL ~ | ||
LL ~ println!("{stripped}{}", stripped); | ||
| | ||
|
||
error: stripping a suffix manually | ||
--> tests/ui/manual_strip_fixable.rs:13:24 | ||
| | ||
LL | let stripped = &s[..s.len() - "bc".len()]; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
note: the suffix was tested here | ||
--> tests/ui/manual_strip_fixable.rs:12:5 | ||
| | ||
LL | if s.ends_with("bc") { | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
help: try using the `strip_suffix` method | ||
| | ||
LL ~ if let Some(stripped) = s.strip_suffix("bc") { | ||
LL ~ | ||
LL ~ println!("{stripped}{}", stripped); | ||
| | ||
|
||
error: aborting due to 2 previous errors | ||
|