Skip to content

Commit 2ba1a6a

Browse files
committed
Auto merge of #7801 - aDotInTheVoid:empty-format, r=camsteffen
Make useless_format recognize format!("") Closes #7796 changelog: [`useless_format`] Fix for false negitive for `format!("")`
2 parents c97a06d + 081d0f8 commit 2ba1a6a

File tree

4 files changed

+46
-20
lines changed

4 files changed

+46
-20
lines changed

clippy_lints/src/format.rs

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

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

96+
fn span_useless_format_empty(cx: &LateContext<'_>, span: Span, sugg: String, applicability: Applicability) {
97+
span_lint_and_sugg(
98+
cx,
99+
USELESS_FORMAT,
100+
span,
101+
"useless use of `format!`",
102+
"consider using `String::new()`",
103+
sugg,
104+
applicability,
105+
);
106+
}
107+
92108
fn span_useless_format(cx: &LateContext<'_>, span: Span, sugg: String, applicability: Applicability) {
93109
span_lint_and_sugg(
94110
cx,

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+
let _ = 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+
let _ = format!("");
22+
2123
format!("{}", "foo");
2224
format!("{:?}", "foo"); // Don't warn about `Debug`.
2325
format!("{:8}", "foo");

tests/ui/format.stderr

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,64 +34,70 @@ LL ~ " bar"##.to_string();
3434
|
3535

3636
error: useless use of `format!`
37-
--> $DIR/format.rs:21:5
37+
--> $DIR/format.rs:21:13
38+
|
39+
LL | let _ = format!("");
40+
| ^^^^^^^^^^^ help: consider using `String::new()`: `String::new()`
41+
42+
error: useless use of `format!`
43+
--> $DIR/format.rs:23:5
3844
|
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)