Skip to content

Commit 24db031

Browse files
committed
Make useless_format recognize format!("")
Closes #7796
1 parent 9205e3d commit 24db031

File tree

4 files changed

+51
-19
lines changed

4 files changed

+51
-19
lines changed

clippy_lints/src/format.rs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,19 @@ impl<'tcx> LateLintPass<'tcx> for UselessFormat {
5050

5151
let mut applicability = Applicability::MachineApplicable;
5252
if format_args.value_args.is_empty() {
53-
if_chain! {
54-
if let [e] = &*format_args.format_string_parts;
55-
if let ExprKind::Lit(lit) = &e.kind;
56-
if let Some(s_src) = snippet_opt(cx, lit.span);
57-
then {
58-
// Simulate macro expansion, converting {{ and }} to { and }.
59-
let s_expand = s_src.replace("{{", "{").replace("}}", "}");
60-
let sugg = format!("{}.to_string()", s_expand);
61-
span_useless_format(cx, call_site, sugg, applicability);
53+
if format_args.format_string_parts.is_empty() {
54+
span_useless_format_empty(cx, call_site, "String::new()".to_owned(), applicability);
55+
} else {
56+
if_chain! {
57+
if let [e] = &*format_args.format_string_parts;
58+
if let ExprKind::Lit(lit) = &e.kind;
59+
if let Some(s_src) = snippet_opt(cx, lit.span);
60+
then {
61+
// Simulate macro expansion, converting {{ and }} to { and }.
62+
let s_expand = s_src.replace("{{", "{").replace("}}", "}");
63+
let sugg = format!("{}.to_string()", s_expand);
64+
span_useless_format(cx, call_site, sugg, applicability);
65+
}
6266
}
6367
}
6468
} else if let [value] = *format_args.value_args {
@@ -90,6 +94,24 @@ impl<'tcx> LateLintPass<'tcx> for UselessFormat {
9094
}
9195
}
9296

97+
98+
fn span_useless_format_empty(cx: &LateContext<'_>, span: Span, mut sugg: String, mut applicability: Applicability) {
99+
// The callsite span contains the statement semicolon for some reason.
100+
if snippet_with_applicability(cx, span, "..", &mut applicability).ends_with(';') {
101+
sugg.push(';');
102+
}
103+
104+
span_lint_and_sugg(
105+
cx,
106+
USELESS_FORMAT,
107+
span,
108+
"useless use of `format!`",
109+
"consider using `String::new()`",
110+
sugg,
111+
applicability,
112+
);
113+
}
114+
93115
fn span_useless_format(cx: &LateContext<'_>, span: Span, mut sugg: String, mut applicability: Applicability) {
94116
// The callsite span contains the statement semicolon for some reason.
95117
if snippet_with_applicability(cx, span, "..", &mut applicability).ends_with(';') {

tests/ui/format.fixed

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ fn main() {
1616
r##"foo {}
1717
" bar"##.to_string();
1818

19+
String::new();
20+
1921
"foo".to_string();
2022
format!("{:?}", "foo"); // Don't warn about `Debug`.
2123
format!("{:8}", "foo");

tests/ui/format.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ fn main() {
1818
" bar"##
1919
);
2020

21+
format!("");
22+
2123
format!("{}", "foo");
2224
format!("{:?}", "foo"); // Don't warn about `Debug`.
2325
format!("{:8}", "foo");

tests/ui/format.stderr

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,62 +36,68 @@ LL + " bar"##.to_string();
3636
error: useless use of `format!`
3737
--> $DIR/format.rs:21:5
3838
|
39+
LL | format!("");
40+
| ^^^^^^^^^^^^ help: consider using `String::new()`: `String::new();`
41+
42+
error: useless use of `format!`
43+
--> $DIR/format.rs:23:5
44+
|
3945
LL | format!("{}", "foo");
4046
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `"foo".to_string();`
4147

4248
error: useless use of `format!`
43-
--> $DIR/format.rs:25:5
49+
--> $DIR/format.rs:27:5
4450
|
4551
LL | format!("{:+}", "foo"); // Warn when the format makes no difference.
4652
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `"foo".to_string();`
4753

4854
error: useless use of `format!`
49-
--> $DIR/format.rs:26:5
55+
--> $DIR/format.rs:28:5
5056
|
5157
LL | format!("{:<}", "foo"); // Warn when the format makes no difference.
5258
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `"foo".to_string();`
5359

5460
error: useless use of `format!`
55-
--> $DIR/format.rs:31:5
61+
--> $DIR/format.rs:33:5
5662
|
5763
LL | format!("{}", arg);
5864
| ^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `arg.to_string();`
5965

6066
error: useless use of `format!`
61-
--> $DIR/format.rs:35:5
67+
--> $DIR/format.rs:37:5
6268
|
6369
LL | format!("{:+}", arg); // Warn when the format makes no difference.
6470
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `arg.to_string();`
6571

6672
error: useless use of `format!`
67-
--> $DIR/format.rs:36:5
73+
--> $DIR/format.rs:38:5
6874
|
6975
LL | format!("{:<}", arg); // Warn when the format makes no difference.
7076
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `arg.to_string();`
7177

7278
error: useless use of `format!`
73-
--> $DIR/format.rs:63:5
79+
--> $DIR/format.rs:65:5
7480
|
7581
LL | format!("{}", 42.to_string());
7682
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `42.to_string();`
7783

7884
error: useless use of `format!`
79-
--> $DIR/format.rs:65:5
85+
--> $DIR/format.rs:67:5
8086
|
8187
LL | format!("{}", x.display().to_string());
8288
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `x.display().to_string();`
8389

8490
error: useless use of `format!`
85-
--> $DIR/format.rs:69:18
91+
--> $DIR/format.rs:71:18
8692
|
8793
LL | let _ = Some(format!("{}", a + "bar"));
8894
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `a + "bar"`
8995

9096
error: useless use of `format!`
91-
--> $DIR/format.rs:73:22
97+
--> $DIR/format.rs:75:22
9298
|
9399
LL | let _s: String = format!("{}", &*v.join("/n"));
94100
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `(&*v.join("/n")).to_string()`
95101

96-
error: aborting due to 14 previous errors
102+
error: aborting due to 15 previous errors
97103

0 commit comments

Comments
 (0)