Skip to content

Commit c822b5f

Browse files
committed
Address review comments
1 parent a78c7fd commit c822b5f

File tree

4 files changed

+34
-34
lines changed

4 files changed

+34
-34
lines changed

clippy_lints/src/format_args.rs

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,12 @@ fn check_to_string_in_format_args<'tcx>(cx: &LateContext<'tcx>, name: Symbol, ar
153153
if let Some(display_trait_id) = cx.tcx.get_diagnostic_item(sym::Display);
154154
if let Some(receiver_snippet) = snippet_opt(cx, receiver.span);
155155
then {
156-
let (n_overloaded_derefs, target) = count_overloaded_derefs(
157-
0,
158-
0,
156+
let (n_needed_derefs, target) = count_needed_derefs(
159157
receiver_ty,
160-
&mut cx.typeck_results().expr_adjustments(receiver).iter(),
158+
cx.typeck_results().expr_adjustments(receiver).iter(),
161159
);
162160
if implements_trait(cx, target, display_trait_id, &[]) {
163-
if n_overloaded_derefs == 0 {
161+
if n_needed_derefs == 0 {
164162
span_lint_and_sugg(
165163
cx,
166164
TO_STRING_IN_FORMAT_ARGS,
@@ -177,7 +175,7 @@ fn check_to_string_in_format_args<'tcx>(cx: &LateContext<'tcx>, name: Symbol, ar
177175
value.span,
178176
&format!("`to_string` applied to a type that implements `Display` in `{}!` args", name),
179177
"use this",
180-
format!("{:*>width$}{}", "", receiver_snippet, width = n_overloaded_derefs),
178+
format!("{:*>width$}{}", "", receiver_snippet, width = n_needed_derefs),
181179
Applicability::MachineApplicable,
182180
);
183181
}
@@ -201,31 +199,25 @@ fn trim_semicolon(cx: &LateContext<'_>, span: Span) -> Span {
201199
})
202200
}
203201

204-
fn count_overloaded_derefs<'tcx, I>(
205-
n_total: usize,
206-
n_overloaded: usize,
207-
ty: Ty<'tcx>,
208-
iter: &mut I,
209-
) -> (usize, Ty<'tcx>)
202+
fn count_needed_derefs<'tcx, I>(mut ty: Ty<'tcx>, mut iter: I) -> (usize, Ty<'tcx>)
210203
where
211204
I: Iterator<Item = &'tcx Adjustment<'tcx>>,
212205
{
213-
if let Some(Adjustment {
214-
kind: Adjust::Deref(overloaded_deref),
215-
target,
216-
}) = iter.next()
217-
{
218-
count_overloaded_derefs(
219-
n_total + 1,
220-
if overloaded_deref.is_some() {
221-
n_total + 1
222-
} else {
223-
n_overloaded
224-
},
206+
let mut n_total = 0;
207+
let mut n_needed = 0;
208+
loop {
209+
if let Some(Adjustment {
210+
kind: Adjust::Deref(overloaded_deref),
225211
target,
226-
iter,
227-
)
228-
} else {
229-
(n_overloaded, ty)
212+
}) = iter.next()
213+
{
214+
n_total += 1;
215+
if overloaded_deref.is_some() {
216+
n_needed = n_total;
217+
}
218+
ty = target;
219+
} else {
220+
return (n_needed, ty);
221+
}
230222
}
231223
}

tests/ui/format_args.fixed

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ macro_rules! my_other_macro {
6969
}
7070

7171
fn main() {
72-
let x = 'x';
72+
let x = &X(1);
7373
let x_ref = &x;
7474

7575
let _ = format!("error: something failed at {}", Location::caller());
@@ -95,7 +95,8 @@ fn main() {
9595
println!("{}", *X(1));
9696
println!("{}", ***Y(&X(1)));
9797
println!("{}", Z(1));
98-
println!("{}", x_ref);
98+
println!("{}", **x);
99+
println!("{}", ***x_ref);
99100

100101
println!("error: something failed at {}", Somewhere.to_string());
101102
println!("{} and again {0}", x.to_string());

tests/ui/format_args.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ macro_rules! my_other_macro {
6969
}
7070

7171
fn main() {
72-
let x = 'x';
72+
let x = &X(1);
7373
let x_ref = &x;
7474

7575
let _ = format!("error: something failed at {}", Location::caller().to_string());
@@ -95,6 +95,7 @@ fn main() {
9595
println!("{}", X(1).to_string());
9696
println!("{}", Y(&X(1)).to_string());
9797
println!("{}", Z(1).to_string());
98+
println!("{}", x.to_string());
9899
println!("{}", x_ref.to_string());
99100

100101
println!("error: something failed at {}", Somewhere.to_string());

tests/ui/format_args.stderr

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,16 @@ 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:98:25
94+
--> $DIR/format_args.rs:98:20
95+
|
96+
LL | println!("{}", x.to_string());
97+
| ^^^^^^^^^^^^^ help: use this: `**x`
98+
99+
error: `to_string` applied to a type that implements `Display` in `println!` args
100+
--> $DIR/format_args.rs:99:20
95101
|
96102
LL | println!("{}", x_ref.to_string());
97-
| ^^^^^^^^^^^^ help: remove this
103+
| ^^^^^^^^^^^^^^^^^ help: use this: `***x_ref`
98104

99-
error: aborting due to 16 previous errors
105+
error: aborting due to 17 previous errors
100106

0 commit comments

Comments
 (0)