Skip to content

Commit

Permalink
Rename single_char_push_str to single_char_add_str
Browse files Browse the repository at this point in the history
  • Loading branch information
ebroto committed Oct 30, 2020
1 parent c1eb8ce commit d958269
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 192 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -1939,8 +1939,8 @@ Released 2018-09-13
[`should_assert_eq`]: https://rust-lang.github.io/rust-clippy/master/index.html#should_assert_eq
[`should_implement_trait`]: https://rust-lang.github.io/rust-clippy/master/index.html#should_implement_trait
[`similar_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#similar_names
[`single_char_add_str`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_char_add_str
[`single_char_pattern`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_char_pattern
[`single_char_push_str`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_char_push_str
[`single_component_path_imports`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_component_path_imports
[`single_element_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_element_loop
[`single_match`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_match
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,8 +713,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
&methods::RESULT_MAP_OR_INTO_OPTION,
&methods::SEARCH_IS_SOME,
&methods::SHOULD_IMPLEMENT_TRAIT,
&methods::SINGLE_CHAR_ADD_STR,
&methods::SINGLE_CHAR_PATTERN,
&methods::SINGLE_CHAR_PUSH_STR,
&methods::SKIP_WHILE_NEXT,
&methods::STRING_EXTEND_CHARS,
&methods::SUSPICIOUS_MAP,
Expand Down Expand Up @@ -1438,8 +1438,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&methods::RESULT_MAP_OR_INTO_OPTION),
LintId::of(&methods::SEARCH_IS_SOME),
LintId::of(&methods::SHOULD_IMPLEMENT_TRAIT),
LintId::of(&methods::SINGLE_CHAR_ADD_STR),
LintId::of(&methods::SINGLE_CHAR_PATTERN),
LintId::of(&methods::SINGLE_CHAR_PUSH_STR),
LintId::of(&methods::SKIP_WHILE_NEXT),
LintId::of(&methods::STRING_EXTEND_CHARS),
LintId::of(&methods::SUSPICIOUS_MAP),
Expand Down Expand Up @@ -1631,7 +1631,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&methods::OPTION_MAP_OR_NONE),
LintId::of(&methods::RESULT_MAP_OR_INTO_OPTION),
LintId::of(&methods::SHOULD_IMPLEMENT_TRAIT),
LintId::of(&methods::SINGLE_CHAR_PUSH_STR),
LintId::of(&methods::SINGLE_CHAR_ADD_STR),
LintId::of(&methods::STRING_EXTEND_CHARS),
LintId::of(&methods::UNNECESSARY_FOLD),
LintId::of(&methods::UNNECESSARY_LAZY_EVALUATIONS),
Expand Down
16 changes: 9 additions & 7 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1290,8 +1290,8 @@ declare_clippy_lint! {
}

declare_clippy_lint! {
/// **What it does:** Warns when using `push_str` with a single-character string literal
/// where `push` with a `char` would work fine.
/// **What it does:** Warns when using `push_str`/`insert_str` with a single-character string literal
/// where `push`/`insert` with a `char` would work fine.
///
/// **Why is this bad?** It's less clear that we are pushing a single character.
///
Expand All @@ -1300,16 +1300,18 @@ declare_clippy_lint! {
/// **Example:**
/// ```rust
/// let mut string = String::new();
/// string.insert_str(0, "R");
/// string.push_str("R");
/// ```
/// Could be written as
/// ```rust
/// let mut string = String::new();
/// string.insert(0, 'R');
/// string.push('R');
/// ```
pub SINGLE_CHAR_PUSH_STR,
pub SINGLE_CHAR_ADD_STR,
style,
"`push_str()` used with a single-character string literal as parameter"
"`push_str()` or `insert_str()` used with a single-character string literal as parameter"
}

declare_clippy_lint! {
Expand Down Expand Up @@ -1390,7 +1392,7 @@ declare_lint_pass!(Methods => [
INEFFICIENT_TO_STRING,
NEW_RET_NO_SELF,
SINGLE_CHAR_PATTERN,
SINGLE_CHAR_PUSH_STR,
SINGLE_CHAR_ADD_STR,
SEARCH_IS_SOME,
FILTER_NEXT,
SKIP_WHILE_NEXT,
Expand Down Expand Up @@ -3248,7 +3250,7 @@ fn lint_single_char_push_string(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args
let sugg = format!("{}.push({})", base_string_snippet, extension_string);
span_lint_and_sugg(
cx,
SINGLE_CHAR_PUSH_STR,
SINGLE_CHAR_ADD_STR,
expr.span,
"calling `push_str()` using a single-character string literal",
"consider using `push` with a character literal",
Expand All @@ -3268,7 +3270,7 @@ fn lint_single_char_insert_string(cx: &LateContext<'_>, expr: &hir::Expr<'_>, ar
let sugg = format!("{}.insert({}, {})", base_string_snippet, pos_arg, extension_string);
span_lint_and_sugg(
cx,
SINGLE_CHAR_PUSH_STR,
SINGLE_CHAR_ADD_STR,
expr.span,
"calling `insert_str()` using a single-character string literal",
"consider using `insert` with a character literal",
Expand Down
12 changes: 6 additions & 6 deletions src/lintlist/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2147,16 +2147,16 @@ vec![
module: "non_expressive_names",
},
Lint {
name: "single_char_pattern",
group: "perf",
desc: "using a single-character str where a char could be used, e.g., `_.split(\"x\")`",
name: "single_char_add_str",
group: "style",
desc: "`push_str()` or `insert_str()` used with a single-character string literal as parameter",
deprecation: None,
module: "methods",
},
Lint {
name: "single_char_push_str",
group: "style",
desc: "`push_str()` used with a single-character string literal as parameter",
name: "single_char_pattern",
group: "perf",
desc: "using a single-character str where a char could be used, e.g., `_.split(\"x\")`",
deprecation: None,
module: "methods",
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// run-rustfix
#![warn(clippy::single_char_push_str)]
#![warn(clippy::single_char_add_str)]

macro_rules! get_string {
() => {
Expand All @@ -8,6 +8,23 @@ macro_rules! get_string {
}

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, '\'');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// run-rustfix
#![warn(clippy::single_char_push_str)]
#![warn(clippy::single_char_add_str)]

macro_rules! get_string {
() => {
Expand All @@ -8,6 +8,23 @@ macro_rules! get_string {
}

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, "'");
Expand Down
82 changes: 82 additions & 0 deletions tests/ui/single_char_add_str.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
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:42:5
|
LL | get_string!().insert_str(1, "?");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `insert` with a character literal: `get_string!().insert(1, '?')`

error: aborting due to 13 previous errors

46 changes: 0 additions & 46 deletions tests/ui/single_char_insert_str.stderr

This file was deleted.

6 changes: 0 additions & 6 deletions tests/ui/single_char_pattern.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ fn main() {

let y = "x";
x.split(y);
// Not yet testing for multi-byte characters
// Changing `r.len() == 1` to `r.chars().count() == 1` in `lint_clippy::single_char_pattern`
// should have done this but produced an ICE
//
// We may not want to suggest changing these anyway
// See: https://github.com/rust-lang/rust-clippy/issues/650#issuecomment-184328984
x.split('ß');
x.split('ℝ');
x.split('💣');
Expand Down
6 changes: 0 additions & 6 deletions tests/ui/single_char_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ fn main() {

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

0 comments on commit d958269

Please sign in to comment.