handle macros returning Strings in single_char_push_str and single_char_insert_str

This commit is contained in:
Matthias Krüger 2020-09-14 15:31:04 +02:00 committed by Eduardo Broto
parent 2350ee75b2
commit c6412aeebc
7 changed files with 54 additions and 14 deletions

View file

@ -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,
@ -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(

View file

@ -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');
@ -15,4 +21,6 @@ fn main() {
string.insert(x, 'a');
const Y: usize = 1;
string.insert(Y, 'a');
get_string!().insert(1, '?');
}

View file

@ -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");
@ -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, "?");
}

View file

@ -1,5 +1,5 @@
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')`
@ -7,34 +7,40 @@ LL | string.insert_str(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

View file

@ -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');
@ -12,4 +18,6 @@ fn main() {
string.push('\x52');
string.push('\u{0052}');
string.push('a');
get_string!().push_str("ö");
}

View file

@ -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");
@ -12,4 +18,6 @@ fn main() {
string.push_str("\x52");
string.push_str("\u{0052}");
string.push_str(r##"a"##);
get_string!().push_str("ö");
}

View file

@ -1,5 +1,5 @@
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')`
@ -7,25 +7,25 @@ LL | string.push_str("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')`