-
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.
Auto merge of #6037 - matthiaskrgr:single_char_insert, r=flip1995
single_char_insert_str: lint using insert_str() on single-char literals and suggest insert() Fixes #6026 changelog: add single_char_insert_str lint which lints using string.insert_str() with single char literals and suggests string.insert() with a char
- Loading branch information
Showing
14 changed files
with
278 additions
and
125 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
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,45 @@ | ||
// run-rustfix | ||
#![warn(clippy::single_char_add_str)] | ||
|
||
macro_rules! get_string { | ||
() => { | ||
String::from("Hello world!") | ||
}; | ||
} | ||
|
||
fn main() { | ||
// `push_str` tests | ||
|
||
let mut string = String::new(); | ||
string.push('R'); | ||
string.push('\''); | ||
|
||
string.push('u'); | ||
string.push_str("st"); | ||
string.push_str(""); | ||
string.push('\x52'); | ||
string.push('\u{0052}'); | ||
string.push('a'); | ||
|
||
get_string!().push('ö'); | ||
|
||
// `insert_str` tests | ||
|
||
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'); | ||
string.insert(Y, '"'); | ||
string.insert(Y, '\''); | ||
|
||
get_string!().insert(1, '?'); | ||
} |
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,45 @@ | ||
// run-rustfix | ||
#![warn(clippy::single_char_add_str)] | ||
|
||
macro_rules! get_string { | ||
() => { | ||
String::from("Hello world!") | ||
}; | ||
} | ||
|
||
fn main() { | ||
// `push_str` tests | ||
|
||
let mut string = String::new(); | ||
string.push_str("R"); | ||
string.push_str("'"); | ||
|
||
string.push('u'); | ||
string.push_str("st"); | ||
string.push_str(""); | ||
string.push_str("\x52"); | ||
string.push_str("\u{0052}"); | ||
string.push_str(r##"a"##); | ||
|
||
get_string!().push_str("ö"); | ||
|
||
// `insert_str` tests | ||
|
||
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"##); | ||
string.insert_str(Y, r##"""##); | ||
string.insert_str(Y, r##"'"##); | ||
|
||
get_string!().insert_str(1, "?"); | ||
} |
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,94 @@ | ||
error: calling `push_str()` using a single-character string literal | ||
--> $DIR/single_char_add_str.rs:14:5 | ||
| | ||
LL | string.push_str("R"); | ||
| ^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `string.push('R')` | ||
| | ||
= note: `-D clippy::single-char-add-str` implied by `-D warnings` | ||
|
||
error: calling `push_str()` using a single-character string literal | ||
--> $DIR/single_char_add_str.rs:15:5 | ||
| | ||
LL | string.push_str("'"); | ||
| ^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `string.push('/'')` | ||
|
||
error: calling `push_str()` using a single-character string literal | ||
--> $DIR/single_char_add_str.rs:20:5 | ||
| | ||
LL | string.push_str("/x52"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `string.push('/x52')` | ||
|
||
error: calling `push_str()` using a single-character string literal | ||
--> $DIR/single_char_add_str.rs:21:5 | ||
| | ||
LL | string.push_str("/u{0052}"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `string.push('/u{0052}')` | ||
|
||
error: calling `push_str()` using a single-character string literal | ||
--> $DIR/single_char_add_str.rs:22:5 | ||
| | ||
LL | string.push_str(r##"a"##); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `string.push('a')` | ||
|
||
error: calling `push_str()` using a single-character string literal | ||
--> $DIR/single_char_add_str.rs:24:5 | ||
| | ||
LL | get_string!().push_str("ö"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `get_string!().push('ö')` | ||
|
||
error: calling `insert_str()` using a single-character string literal | ||
--> $DIR/single_char_add_str.rs:29:5 | ||
| | ||
LL | string.insert_str(0, "R"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `insert` with a character literal: `string.insert(0, 'R')` | ||
|
||
error: calling `insert_str()` using a single-character string literal | ||
--> $DIR/single_char_add_str.rs:30: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_add_str.rs:35: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_add_str.rs:36: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_add_str.rs:38: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_add_str.rs:40:5 | ||
| | ||
LL | string.insert_str(Y, r##"a"##); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `insert` with a character literal: `string.insert(Y, 'a')` | ||
|
||
error: calling `insert_str()` using a single-character string literal | ||
--> $DIR/single_char_add_str.rs:41:5 | ||
| | ||
LL | string.insert_str(Y, r##"""##); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `insert` with a character literal: `string.insert(Y, '"')` | ||
|
||
error: calling `insert_str()` using a single-character string literal | ||
--> $DIR/single_char_add_str.rs:42:5 | ||
| | ||
LL | string.insert_str(Y, r##"'"##); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `insert` with a character literal: `string.insert(Y, '/'')` | ||
|
||
error: calling `insert_str()` using a single-character string literal | ||
--> $DIR/single_char_add_str.rs:44:5 | ||
| | ||
LL | get_string!().insert_str(1, "?"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `insert` with a character literal: `get_string!().insert(1, '?')` | ||
|
||
error: aborting due to 15 previous errors | ||
|
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
Oops, something went wrong.