Skip to content

Commit

Permalink
handle macros returning Strings in single_char_push_str and single_ch…
Browse files Browse the repository at this point in the history
…ar_insert_str
  • Loading branch information
matthiaskrgr authored and ebroto committed Oct 30, 2020
1 parent 2350ee7 commit c6412ae
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 14 deletions.
6 changes: 4 additions & 2 deletions clippy_lints/src/methods/mod.rs
Expand Up @@ -3243,7 +3243,8 @@ fn lint_single_char_pattern(cx: &LateContext<'_>, _expr: &hir::Expr<'_>, arg: &h
fn lint_single_char_push_string(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
let mut applicability = Applicability::MachineApplicable;
if let Some(extension_string) = get_hint_if_single_char_arg(cx, &args[1], &mut applicability) {
let base_string_snippet = snippet_with_applicability(cx, args[0].span, "..", &mut applicability);
let base_string_snippet =
snippet_with_applicability(cx, args[0].span.source_callsite(), "..", &mut applicability);
let sugg = format!("{}.push({})", base_string_snippet, extension_string);
span_lint_and_sugg(
cx,
Expand All @@ -3261,7 +3262,8 @@ fn lint_single_char_push_string(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args
fn lint_single_char_insert_string(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
let mut applicability = Applicability::MachineApplicable;
if let Some(extension_string) = get_hint_if_single_char_arg(cx, &args[2], &mut applicability) {
let base_string_snippet = snippet_with_applicability(cx, args[0].span, "_", &mut applicability);
let base_string_snippet =
snippet_with_applicability(cx, args[0].span.source_callsite(), "_", &mut applicability);
let pos_arg = snippet(cx, args[1].span, "..");
let sugg = format!("{}.insert({}, {})", base_string_snippet, pos_arg, extension_string);
span_lint_and_sugg(
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/single_char_insert_str.fixed
@@ -1,6 +1,12 @@
// run-rustfix
#![warn(clippy::single_char_push_str)]

macro_rules! get_string {
() => {
String::from("Hello world!")
};
}

fn main() {
let mut string = String::new();
string.insert(0, 'R');
Expand All @@ -15,4 +21,6 @@ fn main() {
string.insert(x, 'a');
const Y: usize = 1;
string.insert(Y, 'a');

get_string!().insert(1, '?');
}
8 changes: 8 additions & 0 deletions tests/ui/single_char_insert_str.rs
@@ -1,6 +1,12 @@
// run-rustfix
#![warn(clippy::single_char_push_str)]

macro_rules! get_string {
() => {
String::from("Hello world!")
};
}

fn main() {
let mut string = String::new();
string.insert_str(0, "R");
Expand All @@ -15,4 +21,6 @@ fn main() {
string.insert_str(x, r##"a"##);
const Y: usize = 1;
string.insert_str(Y, r##"a"##);

get_string!().insert_str(1, "?");
}
20 changes: 13 additions & 7 deletions tests/ui/single_char_insert_str.stderr
@@ -1,40 +1,46 @@
error: calling `insert_str()` using a single-character string literal
--> $DIR/single_char_insert_str.rs:6:5
--> $DIR/single_char_insert_str.rs:12:5
|
LL | string.insert_str(0, "R");
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `insert` with a character literal: `string.insert(0, 'R')`
|
= note: `-D clippy::single-char-push-str` implied by `-D warnings`

error: calling `insert_str()` using a single-character string literal
--> $DIR/single_char_insert_str.rs:7:5
--> $DIR/single_char_insert_str.rs:13: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
--> $DIR/single_char_insert_str.rs:18: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
--> $DIR/single_char_insert_str.rs:19: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
--> $DIR/single_char_insert_str.rs:21: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
--> $DIR/single_char_insert_str.rs:23: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
error: calling `insert_str()` using a single-character string literal
--> $DIR/single_char_insert_str.rs:25:5
|
LL | get_string!().insert_str(1, "?");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `insert` with a character literal: `get_string!().insert(1, '?')`

error: aborting due to 7 previous errors

8 changes: 8 additions & 0 deletions tests/ui/single_char_push_str.fixed
@@ -1,6 +1,12 @@
// run-rustfix
#![warn(clippy::single_char_push_str)]

macro_rules! get_string {
() => {
String::from("Hello world!")
};
}

fn main() {
let mut string = String::new();
string.push('R');
Expand All @@ -12,4 +18,6 @@ fn main() {
string.push('\x52');
string.push('\u{0052}');
string.push('a');

get_string!().push_str("ö");
}
8 changes: 8 additions & 0 deletions tests/ui/single_char_push_str.rs
@@ -1,6 +1,12 @@
// run-rustfix
#![warn(clippy::single_char_push_str)]

macro_rules! get_string {
() => {
String::from("Hello world!")
};
}

fn main() {
let mut string = String::new();
string.push_str("R");
Expand All @@ -12,4 +18,6 @@ fn main() {
string.push_str("\x52");
string.push_str("\u{0052}");
string.push_str(r##"a"##);

get_string!().push_str("ö");
}
10 changes: 5 additions & 5 deletions tests/ui/single_char_push_str.stderr
@@ -1,31 +1,31 @@
error: calling `push_str()` using a single-character string literal
--> $DIR/single_char_push_str.rs:6:5
--> $DIR/single_char_push_str.rs:12:5
|
LL | string.push_str("R");
| ^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `string.push('R')`
|
= note: `-D clippy::single-char-push-str` implied by `-D warnings`

error: calling `push_str()` using a single-character string literal
--> $DIR/single_char_push_str.rs:7:5
--> $DIR/single_char_push_str.rs:13: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_push_str.rs:12:5
--> $DIR/single_char_push_str.rs:18: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_push_str.rs:13:5
--> $DIR/single_char_push_str.rs:19: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_push_str.rs:14:5
--> $DIR/single_char_push_str.rs:20:5
|
LL | string.push_str(r##"a"##);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `string.push('a')`
Expand Down

0 comments on commit c6412ae

Please sign in to comment.