Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions clippy_lints/src/useless_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,13 +451,28 @@ fn has_eligible_receiver(cx: &LateContext<'_>, recv: &Expr<'_>, expr: &Expr<'_>)

fn adjustments(cx: &LateContext<'_>, expr: &Expr<'_>) -> String {
let mut prefix = String::new();
for adj in cx.typeck_results().expr_adjustments(expr) {
match adj.kind {
Adjust::Deref(_) => prefix = format!("*{prefix}"),
Adjust::Borrow(AutoBorrow::Ref(AutoBorrowMutability::Mut { .. })) => prefix = format!("&mut {prefix}"),
Adjust::Borrow(AutoBorrow::Ref(AutoBorrowMutability::Not)) => prefix = format!("&{prefix}"),
_ => {},
let adjustments = cx.typeck_results().expr_adjustments(expr);

if !adjustments.is_empty() {
let mut target = adjustments[0].target;

for adj in adjustments {
target = adj.target;
}
Comment on lines +456 to +461
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks overly complicated, can't you use something like this?

Suggested change
if !adjustments.is_empty() {
let mut target = adjustments[0].target;
for adj in adjustments {
target = adj.target;
}
if let adjustments @ [.., Adjustment { target, .. }] = cx.typeck_results().expr_adjustments(expr) {


for adj in adjustments {
match adj.kind {
Adjust::Deref(_) => prefix = format!("*{prefix}"),
Adjust::Borrow(AutoBorrow::Ref(AutoBorrowMutability::Mut { .. })) => prefix = format!("&mut {prefix}"),
Adjust::Borrow(AutoBorrow::Ref(AutoBorrowMutability::Not)) => prefix = format!("&{prefix}"),
_ => {},
}

if adj.target == target {
break;
}
}
}

prefix
}
Loading