Skip to content
Open
Show file tree
Hide file tree
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
14 changes: 13 additions & 1 deletion clippy_lints/src/useless_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,13 +456,25 @@ 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) {

let adjustments = cx.typeck_results().expr_adjustments(expr);

let [.., last] = adjustments else { return prefix };
let target = last.target;

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}"),
_ => {},
}

// Stop once we reach the final target type.
// This prevents over-adjusting (e.g. suggesting &**y instead of *y).
if adj.target == target {
break;
}
}
prefix
}
10 changes: 10 additions & 0 deletions tests/ui/useless_conversion.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ fn main() {
dont_lint_into_iter_on_copy_iter();
dont_lint_into_iter_on_static_copy_iter();

#[allow(clippy::into_iter_on_ref)]
{
// triggers the IntoIterator trait
fn consume(_: impl IntoIterator) {}

// Should suggest `*items` instead of `&**items`
let items = &&[1, 2, 3];
consume(*items); //~ ERROR: explicit call to `.into_iter()`
}

let _: String = "foo".into();
let _: String = From::from("foo");
let _ = String::from("foo");
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/useless_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ fn main() {
dont_lint_into_iter_on_copy_iter();
dont_lint_into_iter_on_static_copy_iter();

#[allow(clippy::into_iter_on_ref)]
{
// triggers the IntoIterator trait
fn consume(_: impl IntoIterator) {}

// Should suggest `*items` instead of `&**items`
let items = &&[1, 2, 3];
consume(items.into_iter()); //~ ERROR: explicit call to `.into_iter()`
}

let _: String = "foo".into();
let _: String = From::from("foo");
let _ = String::from("foo");
Expand Down
Loading