Skip to content

Commit 42bdfa2

Browse files
committed
Auto merge of #9590 - nyurik:fix-parens, r=Alexendoo
Fix to_string_in_format_args in parens Fix suggestions like ``` print!("error: something failed at {}", (Location::caller().to_string())); ``` where the parenthesis enclose some portion of the value. Fixes #9540 changelog: [`to_string_in_format_args`]: fix incorrect fix when value is enclosed in parenthesis
2 parents 45dd9f3 + 7717904 commit 42bdfa2

File tree

4 files changed

+44
-26
lines changed

4 files changed

+44
-26
lines changed

clippy_lints/src/format_args.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ fn check_format_in_format_args(cx: &LateContext<'_>, call_site: Span, name: Symb
249249
fn check_to_string_in_format_args(cx: &LateContext<'_>, name: Symbol, value: &Expr<'_>) {
250250
if_chain! {
251251
if !value.span.from_expansion();
252-
if let ExprKind::MethodCall(_, receiver, [], _) = value.kind;
252+
if let ExprKind::MethodCall(_, receiver, [], to_string_span) = value.kind;
253253
if let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(value.hir_id);
254254
if is_diag_trait_item(cx, method_def_id, sym::ToString);
255255
let receiver_ty = cx.typeck_results().expr_ty(receiver);
@@ -265,7 +265,7 @@ fn check_to_string_in_format_args(cx: &LateContext<'_>, name: Symbol, value: &Ex
265265
span_lint_and_sugg(
266266
cx,
267267
TO_STRING_IN_FORMAT_ARGS,
268-
value.span.with_lo(receiver.span.hi()),
268+
to_string_span.with_lo(receiver.span.hi()),
269269
&format!(
270270
"`to_string` applied to a type that implements `Display` in `{name}!` args"
271271
),

tests/ui/format_args.fixed

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![allow(unused)]
44
#![allow(
55
clippy::assertions_on_constants,
6+
clippy::double_parens,
67
clippy::eq_op,
78
clippy::print_literal,
89
clippy::uninlined_format_args
@@ -114,6 +115,8 @@ fn main() {
114115
println!("error: something failed at {}", my_other_macro!());
115116
// https://github.com/rust-lang/rust-clippy/issues/7903
116117
println!("{foo}{foo:?}", foo = "foo".to_string());
118+
print!("{}", (Location::caller()));
119+
print!("{}", ((Location::caller())));
117120
}
118121

119122
fn issue8643(vendor_id: usize, product_id: usize, name: &str) {

tests/ui/format_args.rs

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![allow(unused)]
44
#![allow(
55
clippy::assertions_on_constants,
6+
clippy::double_parens,
67
clippy::eq_op,
78
clippy::print_literal,
89
clippy::uninlined_format_args
@@ -114,6 +115,8 @@ fn main() {
114115
println!("error: something failed at {}", my_other_macro!());
115116
// https://github.com/rust-lang/rust-clippy/issues/7903
116117
println!("{foo}{foo:?}", foo = "foo".to_string());
118+
print!("{}", (Location::caller().to_string()));
119+
print!("{}", ((Location::caller()).to_string()));
117120
}
118121

119122
fn issue8643(vendor_id: usize, product_id: usize, name: &str) {

tests/ui/format_args.stderr

+36-24
Original file line numberDiff line numberDiff line change
@@ -1,142 +1,154 @@
11
error: `to_string` applied to a type that implements `Display` in `format!` args
2-
--> $DIR/format_args.rs:76:72
2+
--> $DIR/format_args.rs:77:72
33
|
44
LL | let _ = format!("error: something failed at {}", Location::caller().to_string());
55
| ^^^^^^^^^^^^ help: remove this
66
|
77
= note: `-D clippy::to-string-in-format-args` implied by `-D warnings`
88

99
error: `to_string` applied to a type that implements `Display` in `write!` args
10-
--> $DIR/format_args.rs:80:27
10+
--> $DIR/format_args.rs:81:27
1111
|
1212
LL | Location::caller().to_string()
1313
| ^^^^^^^^^^^^ help: remove this
1414

1515
error: `to_string` applied to a type that implements `Display` in `writeln!` args
16-
--> $DIR/format_args.rs:85:27
16+
--> $DIR/format_args.rs:86:27
1717
|
1818
LL | Location::caller().to_string()
1919
| ^^^^^^^^^^^^ help: remove this
2020

2121
error: `to_string` applied to a type that implements `Display` in `print!` args
22-
--> $DIR/format_args.rs:87:63
22+
--> $DIR/format_args.rs:88:63
2323
|
2424
LL | print!("error: something failed at {}", Location::caller().to_string());
2525
| ^^^^^^^^^^^^ help: remove this
2626

2727
error: `to_string` applied to a type that implements `Display` in `println!` args
28-
--> $DIR/format_args.rs:88:65
28+
--> $DIR/format_args.rs:89:65
2929
|
3030
LL | println!("error: something failed at {}", Location::caller().to_string());
3131
| ^^^^^^^^^^^^ help: remove this
3232

3333
error: `to_string` applied to a type that implements `Display` in `eprint!` args
34-
--> $DIR/format_args.rs:89:64
34+
--> $DIR/format_args.rs:90:64
3535
|
3636
LL | eprint!("error: something failed at {}", Location::caller().to_string());
3737
| ^^^^^^^^^^^^ help: remove this
3838

3939
error: `to_string` applied to a type that implements `Display` in `eprintln!` args
40-
--> $DIR/format_args.rs:90:66
40+
--> $DIR/format_args.rs:91:66
4141
|
4242
LL | eprintln!("error: something failed at {}", Location::caller().to_string());
4343
| ^^^^^^^^^^^^ help: remove this
4444

4545
error: `to_string` applied to a type that implements `Display` in `format_args!` args
46-
--> $DIR/format_args.rs:91:77
46+
--> $DIR/format_args.rs:92:77
4747
|
4848
LL | let _ = format_args!("error: something failed at {}", Location::caller().to_string());
4949
| ^^^^^^^^^^^^ help: remove this
5050

5151
error: `to_string` applied to a type that implements `Display` in `assert!` args
52-
--> $DIR/format_args.rs:92:70
52+
--> $DIR/format_args.rs:93:70
5353
|
5454
LL | assert!(true, "error: something failed at {}", Location::caller().to_string());
5555
| ^^^^^^^^^^^^ help: remove this
5656

5757
error: `to_string` applied to a type that implements `Display` in `assert_eq!` args
58-
--> $DIR/format_args.rs:93:73
58+
--> $DIR/format_args.rs:94:73
5959
|
6060
LL | assert_eq!(0, 0, "error: something failed at {}", Location::caller().to_string());
6161
| ^^^^^^^^^^^^ help: remove this
6262

6363
error: `to_string` applied to a type that implements `Display` in `assert_ne!` args
64-
--> $DIR/format_args.rs:94:73
64+
--> $DIR/format_args.rs:95:73
6565
|
6666
LL | assert_ne!(0, 0, "error: something failed at {}", Location::caller().to_string());
6767
| ^^^^^^^^^^^^ help: remove this
6868

6969
error: `to_string` applied to a type that implements `Display` in `panic!` args
70-
--> $DIR/format_args.rs:95:63
70+
--> $DIR/format_args.rs:96:63
7171
|
7272
LL | panic!("error: something failed at {}", Location::caller().to_string());
7373
| ^^^^^^^^^^^^ help: remove this
7474

7575
error: `to_string` applied to a type that implements `Display` in `println!` args
76-
--> $DIR/format_args.rs:96:20
76+
--> $DIR/format_args.rs:97:20
7777
|
7878
LL | println!("{}", X(1).to_string());
7979
| ^^^^^^^^^^^^^^^^ help: use this: `*X(1)`
8080

8181
error: `to_string` applied to a type that implements `Display` in `println!` args
82-
--> $DIR/format_args.rs:97:20
82+
--> $DIR/format_args.rs:98:20
8383
|
8484
LL | println!("{}", Y(&X(1)).to_string());
8585
| ^^^^^^^^^^^^^^^^^^^^ help: use this: `***Y(&X(1))`
8686

8787
error: `to_string` applied to a type that implements `Display` in `println!` args
88-
--> $DIR/format_args.rs:98:24
88+
--> $DIR/format_args.rs:99:24
8989
|
9090
LL | println!("{}", Z(1).to_string());
9191
| ^^^^^^^^^^^^ help: remove this
9292

9393
error: `to_string` applied to a type that implements `Display` in `println!` args
94-
--> $DIR/format_args.rs:99:20
94+
--> $DIR/format_args.rs:100:20
9595
|
9696
LL | println!("{}", x.to_string());
9797
| ^^^^^^^^^^^^^ help: use this: `**x`
9898

9999
error: `to_string` applied to a type that implements `Display` in `println!` args
100-
--> $DIR/format_args.rs:100:20
100+
--> $DIR/format_args.rs:101:20
101101
|
102102
LL | println!("{}", x_ref.to_string());
103103
| ^^^^^^^^^^^^^^^^^ help: use this: `***x_ref`
104104

105105
error: `to_string` applied to a type that implements `Display` in `println!` args
106-
--> $DIR/format_args.rs:102:39
106+
--> $DIR/format_args.rs:103:39
107107
|
108108
LL | println!("{foo}{bar}", foo = "foo".to_string(), bar = "bar");
109109
| ^^^^^^^^^^^^ help: remove this
110110

111111
error: `to_string` applied to a type that implements `Display` in `println!` args
112-
--> $DIR/format_args.rs:103:52
112+
--> $DIR/format_args.rs:104:52
113113
|
114114
LL | println!("{foo}{bar}", foo = "foo", bar = "bar".to_string());
115115
| ^^^^^^^^^^^^ help: remove this
116116

117117
error: `to_string` applied to a type that implements `Display` in `println!` args
118-
--> $DIR/format_args.rs:104:39
118+
--> $DIR/format_args.rs:105:39
119119
|
120120
LL | println!("{foo}{bar}", bar = "bar".to_string(), foo = "foo");
121121
| ^^^^^^^^^^^^ help: remove this
122122

123123
error: `to_string` applied to a type that implements `Display` in `println!` args
124-
--> $DIR/format_args.rs:105:52
124+
--> $DIR/format_args.rs:106:52
125125
|
126126
LL | println!("{foo}{bar}", bar = "bar", foo = "foo".to_string());
127127
| ^^^^^^^^^^^^ help: remove this
128128

129+
error: `to_string` applied to a type that implements `Display` in `print!` args
130+
--> $DIR/format_args.rs:118:37
131+
|
132+
LL | print!("{}", (Location::caller().to_string()));
133+
| ^^^^^^^^^^^^ help: remove this
134+
135+
error: `to_string` applied to a type that implements `Display` in `print!` args
136+
--> $DIR/format_args.rs:119:39
137+
|
138+
LL | print!("{}", ((Location::caller()).to_string()));
139+
| ^^^^^^^^^^^^ help: remove this
140+
129141
error: `to_string` applied to a type that implements `Display` in `format!` args
130-
--> $DIR/format_args.rs:144:38
142+
--> $DIR/format_args.rs:147:38
131143
|
132144
LL | let x = format!("{} {}", a, b.to_string());
133145
| ^^^^^^^^^^^^ help: remove this
134146

135147
error: `to_string` applied to a type that implements `Display` in `println!` args
136-
--> $DIR/format_args.rs:158:24
148+
--> $DIR/format_args.rs:161:24
137149
|
138150
LL | println!("{}", original[..10].to_string());
139151
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use this: `&original[..10]`
140152

141-
error: aborting due to 23 previous errors
153+
error: aborting due to 25 previous errors
142154

0 commit comments

Comments
 (0)