Skip to content

Commit c6412ae

Browse files
matthiaskrgrebroto
authored andcommitted
handle macros returning Strings in single_char_push_str and single_char_insert_str
1 parent 2350ee7 commit c6412ae

File tree

7 files changed

+54
-14
lines changed

7 files changed

+54
-14
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3243,7 +3243,8 @@ fn lint_single_char_pattern(cx: &LateContext<'_>, _expr: &hir::Expr<'_>, arg: &h
32433243
fn lint_single_char_push_string(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
32443244
let mut applicability = Applicability::MachineApplicable;
32453245
if let Some(extension_string) = get_hint_if_single_char_arg(cx, &args[1], &mut applicability) {
3246-
let base_string_snippet = snippet_with_applicability(cx, args[0].span, "..", &mut applicability);
3246+
let base_string_snippet =
3247+
snippet_with_applicability(cx, args[0].span.source_callsite(), "..", &mut applicability);
32473248
let sugg = format!("{}.push({})", base_string_snippet, extension_string);
32483249
span_lint_and_sugg(
32493250
cx,
@@ -3261,7 +3262,8 @@ fn lint_single_char_push_string(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args
32613262
fn lint_single_char_insert_string(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
32623263
let mut applicability = Applicability::MachineApplicable;
32633264
if let Some(extension_string) = get_hint_if_single_char_arg(cx, &args[2], &mut applicability) {
3264-
let base_string_snippet = snippet_with_applicability(cx, args[0].span, "_", &mut applicability);
3265+
let base_string_snippet =
3266+
snippet_with_applicability(cx, args[0].span.source_callsite(), "_", &mut applicability);
32653267
let pos_arg = snippet(cx, args[1].span, "..");
32663268
let sugg = format!("{}.insert({}, {})", base_string_snippet, pos_arg, extension_string);
32673269
span_lint_and_sugg(

tests/ui/single_char_insert_str.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
// run-rustfix
22
#![warn(clippy::single_char_push_str)]
33

4+
macro_rules! get_string {
5+
() => {
6+
String::from("Hello world!")
7+
};
8+
}
9+
410
fn main() {
511
let mut string = String::new();
612
string.insert(0, 'R');
@@ -15,4 +21,6 @@ fn main() {
1521
string.insert(x, 'a');
1622
const Y: usize = 1;
1723
string.insert(Y, 'a');
24+
25+
get_string!().insert(1, '?');
1826
}

tests/ui/single_char_insert_str.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
// run-rustfix
22
#![warn(clippy::single_char_push_str)]
33

4+
macro_rules! get_string {
5+
() => {
6+
String::from("Hello world!")
7+
};
8+
}
9+
410
fn main() {
511
let mut string = String::new();
612
string.insert_str(0, "R");
@@ -15,4 +21,6 @@ fn main() {
1521
string.insert_str(x, r##"a"##);
1622
const Y: usize = 1;
1723
string.insert_str(Y, r##"a"##);
24+
25+
get_string!().insert_str(1, "?");
1826
}
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,46 @@
11
error: calling `insert_str()` using a single-character string literal
2-
--> $DIR/single_char_insert_str.rs:6:5
2+
--> $DIR/single_char_insert_str.rs:12:5
33
|
44
LL | string.insert_str(0, "R");
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `insert` with a character literal: `string.insert(0, 'R')`
66
|
77
= note: `-D clippy::single-char-push-str` implied by `-D warnings`
88

99
error: calling `insert_str()` using a single-character string literal
10-
--> $DIR/single_char_insert_str.rs:7:5
10+
--> $DIR/single_char_insert_str.rs:13:5
1111
|
1212
LL | string.insert_str(1, "'");
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `insert` with a character literal: `string.insert(1, '/'')`
1414

1515
error: calling `insert_str()` using a single-character string literal
16-
--> $DIR/single_char_insert_str.rs:12:5
16+
--> $DIR/single_char_insert_str.rs:18:5
1717
|
1818
LL | string.insert_str(0, "/x52");
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `insert` with a character literal: `string.insert(0, '/x52')`
2020

2121
error: calling `insert_str()` using a single-character string literal
22-
--> $DIR/single_char_insert_str.rs:13:5
22+
--> $DIR/single_char_insert_str.rs:19:5
2323
|
2424
LL | string.insert_str(0, "/u{0052}");
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `insert` with a character literal: `string.insert(0, '/u{0052}')`
2626

2727
error: calling `insert_str()` using a single-character string literal
28-
--> $DIR/single_char_insert_str.rs:15:5
28+
--> $DIR/single_char_insert_str.rs:21:5
2929
|
3030
LL | string.insert_str(x, r##"a"##);
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `insert` with a character literal: `string.insert(x, 'a')`
3232

3333
error: calling `insert_str()` using a single-character string literal
34-
--> $DIR/single_char_insert_str.rs:17:5
34+
--> $DIR/single_char_insert_str.rs:23:5
3535
|
3636
LL | string.insert_str(Y, r##"a"##);
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `insert` with a character literal: `string.insert(Y, 'a')`
3838

39-
error: aborting due to 6 previous errors
39+
error: calling `insert_str()` using a single-character string literal
40+
--> $DIR/single_char_insert_str.rs:25:5
41+
|
42+
LL | get_string!().insert_str(1, "?");
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `insert` with a character literal: `get_string!().insert(1, '?')`
44+
45+
error: aborting due to 7 previous errors
4046

tests/ui/single_char_push_str.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
// run-rustfix
22
#![warn(clippy::single_char_push_str)]
33

4+
macro_rules! get_string {
5+
() => {
6+
String::from("Hello world!")
7+
};
8+
}
9+
410
fn main() {
511
let mut string = String::new();
612
string.push('R');
@@ -12,4 +18,6 @@ fn main() {
1218
string.push('\x52');
1319
string.push('\u{0052}');
1420
string.push('a');
21+
22+
get_string!().push_str("ö");
1523
}

tests/ui/single_char_push_str.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
// run-rustfix
22
#![warn(clippy::single_char_push_str)]
33

4+
macro_rules! get_string {
5+
() => {
6+
String::from("Hello world!")
7+
};
8+
}
9+
410
fn main() {
511
let mut string = String::new();
612
string.push_str("R");
@@ -12,4 +18,6 @@ fn main() {
1218
string.push_str("\x52");
1319
string.push_str("\u{0052}");
1420
string.push_str(r##"a"##);
21+
22+
get_string!().push_str("ö");
1523
}

tests/ui/single_char_push_str.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
error: calling `push_str()` using a single-character string literal
2-
--> $DIR/single_char_push_str.rs:6:5
2+
--> $DIR/single_char_push_str.rs:12:5
33
|
44
LL | string.push_str("R");
55
| ^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `string.push('R')`
66
|
77
= note: `-D clippy::single-char-push-str` implied by `-D warnings`
88

99
error: calling `push_str()` using a single-character string literal
10-
--> $DIR/single_char_push_str.rs:7:5
10+
--> $DIR/single_char_push_str.rs:13:5
1111
|
1212
LL | string.push_str("'");
1313
| ^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `string.push('/'')`
1414

1515
error: calling `push_str()` using a single-character string literal
16-
--> $DIR/single_char_push_str.rs:12:5
16+
--> $DIR/single_char_push_str.rs:18:5
1717
|
1818
LL | string.push_str("/x52");
1919
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `string.push('/x52')`
2020

2121
error: calling `push_str()` using a single-character string literal
22-
--> $DIR/single_char_push_str.rs:13:5
22+
--> $DIR/single_char_push_str.rs:19:5
2323
|
2424
LL | string.push_str("/u{0052}");
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `string.push('/u{0052}')`
2626

2727
error: calling `push_str()` using a single-character string literal
28-
--> $DIR/single_char_push_str.rs:14:5
28+
--> $DIR/single_char_push_str.rs:20:5
2929
|
3030
LL | string.push_str(r##"a"##);
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `string.push('a')`

0 commit comments

Comments
 (0)