Skip to content

Commit d958269

Browse files
committed
Rename single_char_push_str to single_char_add_str
1 parent c1eb8ce commit d958269

14 files changed

+166
-192
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1939,8 +1939,8 @@ Released 2018-09-13
19391939
[`should_assert_eq`]: https://rust-lang.github.io/rust-clippy/master/index.html#should_assert_eq
19401940
[`should_implement_trait`]: https://rust-lang.github.io/rust-clippy/master/index.html#should_implement_trait
19411941
[`similar_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#similar_names
1942+
[`single_char_add_str`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_char_add_str
19421943
[`single_char_pattern`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_char_pattern
1943-
[`single_char_push_str`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_char_push_str
19441944
[`single_component_path_imports`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_component_path_imports
19451945
[`single_element_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_element_loop
19461946
[`single_match`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_match

clippy_lints/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -713,8 +713,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
713713
&methods::RESULT_MAP_OR_INTO_OPTION,
714714
&methods::SEARCH_IS_SOME,
715715
&methods::SHOULD_IMPLEMENT_TRAIT,
716+
&methods::SINGLE_CHAR_ADD_STR,
716717
&methods::SINGLE_CHAR_PATTERN,
717-
&methods::SINGLE_CHAR_PUSH_STR,
718718
&methods::SKIP_WHILE_NEXT,
719719
&methods::STRING_EXTEND_CHARS,
720720
&methods::SUSPICIOUS_MAP,
@@ -1438,8 +1438,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
14381438
LintId::of(&methods::RESULT_MAP_OR_INTO_OPTION),
14391439
LintId::of(&methods::SEARCH_IS_SOME),
14401440
LintId::of(&methods::SHOULD_IMPLEMENT_TRAIT),
1441+
LintId::of(&methods::SINGLE_CHAR_ADD_STR),
14411442
LintId::of(&methods::SINGLE_CHAR_PATTERN),
1442-
LintId::of(&methods::SINGLE_CHAR_PUSH_STR),
14431443
LintId::of(&methods::SKIP_WHILE_NEXT),
14441444
LintId::of(&methods::STRING_EXTEND_CHARS),
14451445
LintId::of(&methods::SUSPICIOUS_MAP),
@@ -1631,7 +1631,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
16311631
LintId::of(&methods::OPTION_MAP_OR_NONE),
16321632
LintId::of(&methods::RESULT_MAP_OR_INTO_OPTION),
16331633
LintId::of(&methods::SHOULD_IMPLEMENT_TRAIT),
1634-
LintId::of(&methods::SINGLE_CHAR_PUSH_STR),
1634+
LintId::of(&methods::SINGLE_CHAR_ADD_STR),
16351635
LintId::of(&methods::STRING_EXTEND_CHARS),
16361636
LintId::of(&methods::UNNECESSARY_FOLD),
16371637
LintId::of(&methods::UNNECESSARY_LAZY_EVALUATIONS),

clippy_lints/src/methods/mod.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,8 +1290,8 @@ declare_clippy_lint! {
12901290
}
12911291

12921292
declare_clippy_lint! {
1293-
/// **What it does:** Warns when using `push_str` with a single-character string literal
1294-
/// where `push` with a `char` would work fine.
1293+
/// **What it does:** Warns when using `push_str`/`insert_str` with a single-character string literal
1294+
/// where `push`/`insert` with a `char` would work fine.
12951295
///
12961296
/// **Why is this bad?** It's less clear that we are pushing a single character.
12971297
///
@@ -1300,16 +1300,18 @@ declare_clippy_lint! {
13001300
/// **Example:**
13011301
/// ```rust
13021302
/// let mut string = String::new();
1303+
/// string.insert_str(0, "R");
13031304
/// string.push_str("R");
13041305
/// ```
13051306
/// Could be written as
13061307
/// ```rust
13071308
/// let mut string = String::new();
1309+
/// string.insert(0, 'R');
13081310
/// string.push('R');
13091311
/// ```
1310-
pub SINGLE_CHAR_PUSH_STR,
1312+
pub SINGLE_CHAR_ADD_STR,
13111313
style,
1312-
"`push_str()` used with a single-character string literal as parameter"
1314+
"`push_str()` or `insert_str()` used with a single-character string literal as parameter"
13131315
}
13141316

13151317
declare_clippy_lint! {
@@ -1390,7 +1392,7 @@ declare_lint_pass!(Methods => [
13901392
INEFFICIENT_TO_STRING,
13911393
NEW_RET_NO_SELF,
13921394
SINGLE_CHAR_PATTERN,
1393-
SINGLE_CHAR_PUSH_STR,
1395+
SINGLE_CHAR_ADD_STR,
13941396
SEARCH_IS_SOME,
13951397
FILTER_NEXT,
13961398
SKIP_WHILE_NEXT,
@@ -3248,7 +3250,7 @@ fn lint_single_char_push_string(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args
32483250
let sugg = format!("{}.push({})", base_string_snippet, extension_string);
32493251
span_lint_and_sugg(
32503252
cx,
3251-
SINGLE_CHAR_PUSH_STR,
3253+
SINGLE_CHAR_ADD_STR,
32523254
expr.span,
32533255
"calling `push_str()` using a single-character string literal",
32543256
"consider using `push` with a character literal",
@@ -3268,7 +3270,7 @@ fn lint_single_char_insert_string(cx: &LateContext<'_>, expr: &hir::Expr<'_>, ar
32683270
let sugg = format!("{}.insert({}, {})", base_string_snippet, pos_arg, extension_string);
32693271
span_lint_and_sugg(
32703272
cx,
3271-
SINGLE_CHAR_PUSH_STR,
3273+
SINGLE_CHAR_ADD_STR,
32723274
expr.span,
32733275
"calling `insert_str()` using a single-character string literal",
32743276
"consider using `insert` with a character literal",

src/lintlist/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2147,16 +2147,16 @@ vec![
21472147
module: "non_expressive_names",
21482148
},
21492149
Lint {
2150-
name: "single_char_pattern",
2151-
group: "perf",
2152-
desc: "using a single-character str where a char could be used, e.g., `_.split(\"x\")`",
2150+
name: "single_char_add_str",
2151+
group: "style",
2152+
desc: "`push_str()` or `insert_str()` used with a single-character string literal as parameter",
21532153
deprecation: None,
21542154
module: "methods",
21552155
},
21562156
Lint {
2157-
name: "single_char_push_str",
2158-
group: "style",
2159-
desc: "`push_str()` used with a single-character string literal as parameter",
2157+
name: "single_char_pattern",
2158+
group: "perf",
2159+
desc: "using a single-character str where a char could be used, e.g., `_.split(\"x\")`",
21602160
deprecation: None,
21612161
module: "methods",
21622162
},

tests/ui/single_char_insert_str.fixed renamed to tests/ui/single_char_add_str.fixed

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// run-rustfix
2-
#![warn(clippy::single_char_push_str)]
2+
#![warn(clippy::single_char_add_str)]
33

44
macro_rules! get_string {
55
() => {
@@ -8,6 +8,23 @@ macro_rules! get_string {
88
}
99

1010
fn main() {
11+
// `push_str` tests
12+
13+
let mut string = String::new();
14+
string.push('R');
15+
string.push('\'');
16+
17+
string.push('u');
18+
string.push_str("st");
19+
string.push_str("");
20+
string.push('\x52');
21+
string.push('\u{0052}');
22+
string.push('a');
23+
24+
get_string!().push('ö');
25+
26+
// `insert_str` tests
27+
1128
let mut string = String::new();
1229
string.insert(0, 'R');
1330
string.insert(1, '\'');

tests/ui/single_char_insert_str.rs renamed to tests/ui/single_char_add_str.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// run-rustfix
2-
#![warn(clippy::single_char_push_str)]
2+
#![warn(clippy::single_char_add_str)]
33

44
macro_rules! get_string {
55
() => {
@@ -8,6 +8,23 @@ macro_rules! get_string {
88
}
99

1010
fn main() {
11+
// `push_str` tests
12+
13+
let mut string = String::new();
14+
string.push_str("R");
15+
string.push_str("'");
16+
17+
string.push('u');
18+
string.push_str("st");
19+
string.push_str("");
20+
string.push_str("\x52");
21+
string.push_str("\u{0052}");
22+
string.push_str(r##"a"##);
23+
24+
get_string!().push_str("ö");
25+
26+
// `insert_str` tests
27+
1128
let mut string = String::new();
1229
string.insert_str(0, "R");
1330
string.insert_str(1, "'");

tests/ui/single_char_add_str.stderr

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
error: calling `push_str()` using a single-character string literal
2+
--> $DIR/single_char_add_str.rs:14:5
3+
|
4+
LL | string.push_str("R");
5+
| ^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `string.push('R')`
6+
|
7+
= note: `-D clippy::single-char-add-str` implied by `-D warnings`
8+
9+
error: calling `push_str()` using a single-character string literal
10+
--> $DIR/single_char_add_str.rs:15:5
11+
|
12+
LL | string.push_str("'");
13+
| ^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `string.push('/'')`
14+
15+
error: calling `push_str()` using a single-character string literal
16+
--> $DIR/single_char_add_str.rs:20:5
17+
|
18+
LL | string.push_str("/x52");
19+
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `string.push('/x52')`
20+
21+
error: calling `push_str()` using a single-character string literal
22+
--> $DIR/single_char_add_str.rs:21:5
23+
|
24+
LL | string.push_str("/u{0052}");
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `string.push('/u{0052}')`
26+
27+
error: calling `push_str()` using a single-character string literal
28+
--> $DIR/single_char_add_str.rs:22:5
29+
|
30+
LL | string.push_str(r##"a"##);
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `string.push('a')`
32+
33+
error: calling `push_str()` using a single-character string literal
34+
--> $DIR/single_char_add_str.rs:24:5
35+
|
36+
LL | get_string!().push_str("ö");
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `get_string!().push('ö')`
38+
39+
error: calling `insert_str()` using a single-character string literal
40+
--> $DIR/single_char_add_str.rs:29:5
41+
|
42+
LL | string.insert_str(0, "R");
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `insert` with a character literal: `string.insert(0, 'R')`
44+
45+
error: calling `insert_str()` using a single-character string literal
46+
--> $DIR/single_char_add_str.rs:30:5
47+
|
48+
LL | string.insert_str(1, "'");
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `insert` with a character literal: `string.insert(1, '/'')`
50+
51+
error: calling `insert_str()` using a single-character string literal
52+
--> $DIR/single_char_add_str.rs:35:5
53+
|
54+
LL | string.insert_str(0, "/x52");
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `insert` with a character literal: `string.insert(0, '/x52')`
56+
57+
error: calling `insert_str()` using a single-character string literal
58+
--> $DIR/single_char_add_str.rs:36:5
59+
|
60+
LL | string.insert_str(0, "/u{0052}");
61+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `insert` with a character literal: `string.insert(0, '/u{0052}')`
62+
63+
error: calling `insert_str()` using a single-character string literal
64+
--> $DIR/single_char_add_str.rs:38:5
65+
|
66+
LL | string.insert_str(x, r##"a"##);
67+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `insert` with a character literal: `string.insert(x, 'a')`
68+
69+
error: calling `insert_str()` using a single-character string literal
70+
--> $DIR/single_char_add_str.rs:40:5
71+
|
72+
LL | string.insert_str(Y, r##"a"##);
73+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `insert` with a character literal: `string.insert(Y, 'a')`
74+
75+
error: calling `insert_str()` using a single-character string literal
76+
--> $DIR/single_char_add_str.rs:42:5
77+
|
78+
LL | get_string!().insert_str(1, "?");
79+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `insert` with a character literal: `get_string!().insert(1, '?')`
80+
81+
error: aborting due to 13 previous errors
82+

tests/ui/single_char_insert_str.stderr

Lines changed: 0 additions & 46 deletions
This file was deleted.

tests/ui/single_char_pattern.fixed

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ fn main() {
1212

1313
let y = "x";
1414
x.split(y);
15-
// Not yet testing for multi-byte characters
16-
// Changing `r.len() == 1` to `r.chars().count() == 1` in `lint_clippy::single_char_pattern`
17-
// should have done this but produced an ICE
18-
//
19-
// We may not want to suggest changing these anyway
20-
// See: https://github.com/rust-lang/rust-clippy/issues/650#issuecomment-184328984
2115
x.split('ß');
2216
x.split('ℝ');
2317
x.split('💣');

tests/ui/single_char_pattern.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ fn main() {
1212

1313
let y = "x";
1414
x.split(y);
15-
// Not yet testing for multi-byte characters
16-
// Changing `r.len() == 1` to `r.chars().count() == 1` in `lint_clippy::single_char_pattern`
17-
// should have done this but produced an ICE
18-
//
19-
// We may not want to suggest changing these anyway
20-
// See: https://github.com/rust-lang/rust-clippy/issues/650#issuecomment-184328984
2115
x.split("ß");
2216
x.split("ℝ");
2317
x.split("💣");

0 commit comments

Comments
 (0)