Skip to content

Commit 5155119

Browse files
committed
Auto merge of #10401 - samueltardieu:issue-10148, r=Alexendoo
Do not panic when analyzing the malformed origin of a format string Fixes #10148. This will trigger only when generating format strings while accepting weird things in a procedural macro and setting the span to something which is not a string. changelog: none
2 parents b528cc9 + 64775f3 commit 5155119

File tree

4 files changed

+35
-16
lines changed

4 files changed

+35
-16
lines changed

Diff for: clippy_utils/src/macros.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -391,11 +391,18 @@ impl FormatString {
391391
};
392392

393393
let mut unescaped = String::with_capacity(inner.len());
394+
// Sometimes the original string comes from a macro which accepts a malformed string, such as in a
395+
// #[display(""somestring)] attribute (accepted by the `displaythis` crate). Reconstructing the
396+
// string from the span will not be possible, so we will just return None here.
397+
let mut unparsable = false;
394398
unescape_literal(inner, mode, &mut |_, ch| match ch {
395399
Ok(ch) => unescaped.push(ch),
396400
Err(e) if !e.is_fatal() => (),
397-
Err(e) => panic!("{e:?}"),
401+
Err(_) => unparsable = true,
398402
});
403+
if unparsable {
404+
return None;
405+
}
399406

400407
let mut parts = Vec::new();
401408
let _: Option<!> = for_each_expr(pieces, |expr| {

Diff for: tests/ui/format.fixed

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// run-rustfix
2+
// aux-build: proc_macro_with_span.rs
23
#![warn(clippy::useless_format)]
34
#![allow(
45
unused_tuple_struct_fields,
@@ -9,6 +10,8 @@
910
clippy::uninlined_format_args
1011
)]
1112

13+
extern crate proc_macro_with_span;
14+
1215
struct Foo(pub String);
1316

1417
macro_rules! foo {
@@ -87,4 +90,7 @@ fn main() {
8790
let _ = abc.to_string();
8891
let xx = "xx";
8992
let _ = xx.to_string();
93+
94+
// Issue #10148
95+
println!(proc_macro_with_span::with_span!(""something ""));
9096
}

Diff for: tests/ui/format.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// run-rustfix
2+
// aux-build: proc_macro_with_span.rs
23
#![warn(clippy::useless_format)]
34
#![allow(
45
unused_tuple_struct_fields,
@@ -9,6 +10,8 @@
910
clippy::uninlined_format_args
1011
)]
1112

13+
extern crate proc_macro_with_span;
14+
1215
struct Foo(pub String);
1316

1417
macro_rules! foo {
@@ -89,4 +92,7 @@ fn main() {
8992
let _ = format!("{abc}");
9093
let xx = "xx";
9194
let _ = format!("{xx}");
95+
96+
// Issue #10148
97+
println!(proc_macro_with_span::with_span!(""something ""));
9298
}

Diff for: tests/ui/format.stderr

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
error: useless use of `format!`
2-
--> $DIR/format.rs:19:5
2+
--> $DIR/format.rs:22:5
33
|
44
LL | format!("foo");
55
| ^^^^^^^^^^^^^^ help: consider using `.to_string()`: `"foo".to_string()`
66
|
77
= note: `-D clippy::useless-format` implied by `-D warnings`
88

99
error: useless use of `format!`
10-
--> $DIR/format.rs:20:5
10+
--> $DIR/format.rs:23:5
1111
|
1212
LL | format!("{{}}");
1313
| ^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `"{}".to_string()`
1414

1515
error: useless use of `format!`
16-
--> $DIR/format.rs:21:5
16+
--> $DIR/format.rs:24:5
1717
|
1818
LL | format!("{{}} abc {{}}");
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `"{} abc {}".to_string()`
2020

2121
error: useless use of `format!`
22-
--> $DIR/format.rs:22:5
22+
--> $DIR/format.rs:25:5
2323
|
2424
LL | / format!(
2525
LL | | r##"foo {{}}
@@ -34,67 +34,67 @@ LL ~ " bar"##.to_string();
3434
|
3535

3636
error: useless use of `format!`
37-
--> $DIR/format.rs:27:13
37+
--> $DIR/format.rs:30:13
3838
|
3939
LL | let _ = format!("");
4040
| ^^^^^^^^^^^ help: consider using `String::new()`: `String::new()`
4141

4242
error: useless use of `format!`
43-
--> $DIR/format.rs:29:5
43+
--> $DIR/format.rs:32:5
4444
|
4545
LL | format!("{}", "foo");
4646
| ^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `"foo".to_string()`
4747

4848
error: useless use of `format!`
49-
--> $DIR/format.rs:37:5
49+
--> $DIR/format.rs:40:5
5050
|
5151
LL | format!("{}", arg);
5252
| ^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `arg.to_string()`
5353

5454
error: useless use of `format!`
55-
--> $DIR/format.rs:67:5
55+
--> $DIR/format.rs:70:5
5656
|
5757
LL | format!("{}", 42.to_string());
5858
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `42.to_string()`
5959

6060
error: useless use of `format!`
61-
--> $DIR/format.rs:69:5
61+
--> $DIR/format.rs:72:5
6262
|
6363
LL | format!("{}", x.display().to_string());
6464
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `x.display().to_string()`
6565

6666
error: useless use of `format!`
67-
--> $DIR/format.rs:73:18
67+
--> $DIR/format.rs:76:18
6868
|
6969
LL | let _ = Some(format!("{}", a + "bar"));
7070
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `a + "bar"`
7171

7272
error: useless use of `format!`
73-
--> $DIR/format.rs:77:22
73+
--> $DIR/format.rs:80:22
7474
|
7575
LL | let _s: String = format!("{}", &*v.join("/n"));
7676
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `(&*v.join("/n")).to_string()`
7777

7878
error: useless use of `format!`
79-
--> $DIR/format.rs:83:13
79+
--> $DIR/format.rs:86:13
8080
|
8181
LL | let _ = format!("{x}");
8282
| ^^^^^^^^^^^^^^ help: consider using `.to_string()`: `x.to_string()`
8383

8484
error: useless use of `format!`
85-
--> $DIR/format.rs:85:13
85+
--> $DIR/format.rs:88:13
8686
|
8787
LL | let _ = format!("{y}", y = x);
8888
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `x.to_string()`
8989

9090
error: useless use of `format!`
91-
--> $DIR/format.rs:89:13
91+
--> $DIR/format.rs:92:13
9292
|
9393
LL | let _ = format!("{abc}");
9494
| ^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `abc.to_string()`
9595

9696
error: useless use of `format!`
97-
--> $DIR/format.rs:91:13
97+
--> $DIR/format.rs:94:13
9898
|
9999
LL | let _ = format!("{xx}");
100100
| ^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `xx.to_string()`

0 commit comments

Comments
 (0)