Skip to content

Fix suggestion-causes-error of manual_swap #14978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 12, 2025
Merged
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
17 changes: 13 additions & 4 deletions clippy_lints/src/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clippy_utils::source::{snippet_indent, snippet_with_context};
use clippy_utils::sugg::Sugg;
use clippy_utils::ty::is_type_diagnostic_item;

use clippy_utils::{can_mut_borrow_both, eq_expr_value, is_in_const_context, std_or_core};
use clippy_utils::{can_mut_borrow_both, eq_expr_value, is_in_const_context, path_to_local, std_or_core};
use itertools::Itertools;

use rustc_data_structures::fx::FxIndexSet;
Expand Down Expand Up @@ -350,12 +350,21 @@ impl<'tcx> IndexBinding<'_, 'tcx> {
format!("{lhs_snippet}{rhs_snippet}")
},
ExprKind::Path(QPath::Resolved(_, path)) => {
let init = self.cx.expr_or_init(expr);

let Some(first_segment) = path.segments.first() else {
return String::new();
};
if !self.suggest_span.contains(init.span) || !self.is_used_other_than_swapping(first_segment.ident) {

let init = self.cx.expr_or_init(expr);

// We skip suggesting a variable binding in any of these cases:
// - Variable initialization is outside the suggestion span
// - Variable declaration is outside the suggestion span
// - Variable is not used as an index or elsewhere later
if !self.suggest_span.contains(init.span)
|| path_to_local(expr)
.is_some_and(|hir_id| !self.suggest_span.contains(self.cx.tcx.hir_span(hir_id)))
|| !self.is_used_other_than_swapping(first_segment.ident)
{
return String::new();
}

Expand Down
11 changes: 11 additions & 0 deletions tests/ui/manual_swap_auto_fix.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,14 @@ fn swap8() {
let i2 = 1;
v.swap(i1 + i2, i2);
}

fn issue_14931() {
let mut v = [1, 2, 3, 4];

let mut i1 = 0;
for i2 in 0..4 {
v.swap(i1, i2);

i1 += 2;
}
}
14 changes: 14 additions & 0 deletions tests/ui/manual_swap_auto_fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,17 @@ fn swap8() {
v[i1 + i2] = v[i2];
v[i2] = tmp;
}

fn issue_14931() {
let mut v = [1, 2, 3, 4];

let mut i1 = 0;
for i2 in 0..4 {
let tmp = v[i1];
//~^ manual_swap
v[i1] = v[i2];
v[i2] = tmp;

i1 += 2;
}
}
11 changes: 10 additions & 1 deletion tests/ui/manual_swap_auto_fix.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,14 @@ LL | | v[i1 + i2] = v[i2];
LL | | v[i2] = tmp;
| |________________^ help: try: `v.swap(i1 + i2, i2);`

error: aborting due to 8 previous errors
error: this looks like you are swapping elements of `v` manually
--> tests/ui/manual_swap_auto_fix.rs:87:9
|
LL | / let tmp = v[i1];
LL | |
LL | | v[i1] = v[i2];
LL | | v[i2] = tmp;
| |____________________^ help: try: `v.swap(i1, i2);`

error: aborting due to 9 previous errors