Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed Oct 15, 2021
1 parent a78c7fd commit c822b5f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 34 deletions.
48 changes: 20 additions & 28 deletions clippy_lints/src/format_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,12 @@ fn check_to_string_in_format_args<'tcx>(cx: &LateContext<'tcx>, name: Symbol, ar
if let Some(display_trait_id) = cx.tcx.get_diagnostic_item(sym::Display);
if let Some(receiver_snippet) = snippet_opt(cx, receiver.span);
then {
let (n_overloaded_derefs, target) = count_overloaded_derefs(
0,
0,
let (n_needed_derefs, target) = count_needed_derefs(
receiver_ty,
&mut cx.typeck_results().expr_adjustments(receiver).iter(),
cx.typeck_results().expr_adjustments(receiver).iter(),
);
if implements_trait(cx, target, display_trait_id, &[]) {
if n_overloaded_derefs == 0 {
if n_needed_derefs == 0 {
span_lint_and_sugg(
cx,
TO_STRING_IN_FORMAT_ARGS,
Expand All @@ -177,7 +175,7 @@ fn check_to_string_in_format_args<'tcx>(cx: &LateContext<'tcx>, name: Symbol, ar
value.span,
&format!("`to_string` applied to a type that implements `Display` in `{}!` args", name),
"use this",
format!("{:*>width$}{}", "", receiver_snippet, width = n_overloaded_derefs),
format!("{:*>width$}{}", "", receiver_snippet, width = n_needed_derefs),
Applicability::MachineApplicable,
);
}
Expand All @@ -201,31 +199,25 @@ fn trim_semicolon(cx: &LateContext<'_>, span: Span) -> Span {
})
}

fn count_overloaded_derefs<'tcx, I>(
n_total: usize,
n_overloaded: usize,
ty: Ty<'tcx>,
iter: &mut I,
) -> (usize, Ty<'tcx>)
fn count_needed_derefs<'tcx, I>(mut ty: Ty<'tcx>, mut iter: I) -> (usize, Ty<'tcx>)
where
I: Iterator<Item = &'tcx Adjustment<'tcx>>,
{
if let Some(Adjustment {
kind: Adjust::Deref(overloaded_deref),
target,
}) = iter.next()
{
count_overloaded_derefs(
n_total + 1,
if overloaded_deref.is_some() {
n_total + 1
} else {
n_overloaded
},
let mut n_total = 0;
let mut n_needed = 0;
loop {
if let Some(Adjustment {
kind: Adjust::Deref(overloaded_deref),
target,
iter,
)
} else {
(n_overloaded, ty)
}) = iter.next()
{
n_total += 1;
if overloaded_deref.is_some() {
n_needed = n_total;
}
ty = target;
} else {
return (n_needed, ty);
}
}
}
5 changes: 3 additions & 2 deletions tests/ui/format_args.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ macro_rules! my_other_macro {
}

fn main() {
let x = 'x';
let x = &X(1);
let x_ref = &x;

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

println!("error: something failed at {}", Somewhere.to_string());
println!("{} and again {0}", x.to_string());
Expand Down
3 changes: 2 additions & 1 deletion tests/ui/format_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ macro_rules! my_other_macro {
}

fn main() {
let x = 'x';
let x = &X(1);
let x_ref = &x;

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

println!("error: something failed at {}", Somewhere.to_string());
Expand Down
12 changes: 9 additions & 3 deletions tests/ui/format_args.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,16 @@ LL | println!("{}", Z(1).to_string());
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `println!` args
--> $DIR/format_args.rs:98:25
--> $DIR/format_args.rs:98:20
|
LL | println!("{}", x.to_string());
| ^^^^^^^^^^^^^ help: use this: `**x`

error: `to_string` applied to a type that implements `Display` in `println!` args
--> $DIR/format_args.rs:99:20
|
LL | println!("{}", x_ref.to_string());
| ^^^^^^^^^^^^ help: remove this
| ^^^^^^^^^^^^^^^^^ help: use this: `***x_ref`

error: aborting due to 16 previous errors
error: aborting due to 17 previous errors

0 comments on commit c822b5f

Please sign in to comment.