-
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.
single_char_insert_str: lint using insert_str() on single-char litera…
…ls and suggest insert() changelog: add single_char_insert_str lint which lints using string.insert_str() with single char literals and suggests string.insert() with a char Fixes #6026
- Loading branch information
1 parent
231444d
commit d02e4c8
Showing
5 changed files
with
124 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// run-rustfix | ||
#![warn(clippy::single_char_push_str)] | ||
|
||
fn main() { | ||
let mut string = String::new(); | ||
string.insert(0, 'R'); | ||
string.insert(1, '\''); | ||
|
||
string.insert(0, 'u'); | ||
string.insert_str(2, "st"); | ||
string.insert_str(0, ""); | ||
string.insert(0, '\x52'); | ||
string.insert(0, '\u{0052}'); | ||
let x: usize = 2; | ||
string.insert(x, 'a'); | ||
const Y: usize = 1; | ||
string.insert(Y, 'a'); | ||
} |
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,18 @@ | ||
// run-rustfix | ||
#![warn(clippy::single_char_push_str)] | ||
|
||
fn main() { | ||
let mut string = String::new(); | ||
string.insert_str(0, "R"); | ||
string.insert_str(1, "'"); | ||
|
||
string.insert(0, 'u'); | ||
string.insert_str(2, "st"); | ||
string.insert_str(0, ""); | ||
string.insert_str(0, "\x52"); | ||
string.insert_str(0, "\u{0052}"); | ||
let x: usize = 2; | ||
string.insert_str(x, r##"a"##); | ||
const Y: usize = 1; | ||
string.insert_str(Y, r##"a"##); | ||
} |
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: calling `insert_str()` using a single-character string literal | ||
--> $DIR/single_char_insert_str.rs:6:5 | ||
| | ||
LL | string.insert_str(0, "R"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `insert` with a character literal: `string.insert(0, 'R')` | ||
| | ||
= note: `-D clippy::single-char-insert-str` implied by `-D warnings` | ||
|
||
error: calling `insert_str()` using a single-character string literal | ||
--> $DIR/single_char_insert_str.rs:7:5 | ||
| | ||
LL | string.insert_str(1, "'"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `insert` with a character literal: `string.insert(1, '/'')` | ||
|
||
error: calling `insert_str()` using a single-character string literal | ||
--> $DIR/single_char_insert_str.rs:12:5 | ||
| | ||
LL | string.insert_str(0, "/x52"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `insert` with a character literal: `string.insert(0, '/x52')` | ||
|
||
error: calling `insert_str()` using a single-character string literal | ||
--> $DIR/single_char_insert_str.rs:13:5 | ||
| | ||
LL | string.insert_str(0, "/u{0052}"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `insert` with a character literal: `string.insert(0, '/u{0052}')` | ||
|
||
error: calling `insert_str()` using a single-character string literal | ||
--> $DIR/single_char_insert_str.rs:15:5 | ||
| | ||
LL | string.insert_str(x, r##"a"##); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `insert` with a character literal: `string.insert(x, 'a')` | ||
|
||
error: calling `insert_str()` using a single-character string literal | ||
--> $DIR/single_char_insert_str.rs:17:5 | ||
| | ||
LL | string.insert_str(Y, r##"a"##); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `insert` with a character literal: `string.insert(Y, 'a')` | ||
|
||
error: aborting due to 6 previous errors | ||
|