From fe9408070ec51de32a609fbd1245b062a12509f4 Mon Sep 17 00:00:00 2001 From: Senthilnathan Date: Wed, 22 Jul 2026 19:04:22 +0530 Subject: [PATCH 1/2] tests: add UI test for `.swap()` suggestion --- tests/ui/suggestions/suggest-swap.rs | 13 +++++++++++++ tests/ui/suggestions/suggest-swap.stderr | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 tests/ui/suggestions/suggest-swap.rs create mode 100644 tests/ui/suggestions/suggest-swap.stderr diff --git a/tests/ui/suggestions/suggest-swap.rs b/tests/ui/suggestions/suggest-swap.rs new file mode 100644 index 0000000000000..13d86a4b706f4 --- /dev/null +++ b/tests/ui/suggestions/suggest-swap.rs @@ -0,0 +1,13 @@ +use std::mem; + +fn main() { + let mut arr = [1, 2, 3]; + + let i = 0usize; + let j = 1usize; + + mem::swap( + &mut arr[i], + &mut arr[j], //~ ERROR cannot borrow `arr[_]` as mutable more than once at a time + ); +} diff --git a/tests/ui/suggestions/suggest-swap.stderr b/tests/ui/suggestions/suggest-swap.stderr new file mode 100644 index 0000000000000..4c321ca8939f4 --- /dev/null +++ b/tests/ui/suggestions/suggest-swap.stderr @@ -0,0 +1,22 @@ +error[E0499]: cannot borrow `arr[_]` as mutable more than once at a time + --> $DIR/suggest-swap.rs:11:9 + | +LL | mem::swap( + | --------- first borrow later used by call +LL | &mut arr[i], + | ----------- first mutable borrow occurs here +LL | &mut arr[j], + | ^^^^^^^^^^^ second mutable borrow occurs here + | +help: use `.swap()` to swap elements at the specified indices instead + | +LL - mem::swap( +LL - &mut arr[i], +LL - &mut arr[j], +LL - ); +LL + arr.swap(j, i); + | + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0499`. From d5482401250fef44346e03ab35df9e1fac005016 Mon Sep 17 00:00:00 2001 From: Senthilnathan Date: Thu, 23 Jul 2026 11:03:10 +0530 Subject: [PATCH 2/2] fix: add comment on top of test file to follow style spec --- tests/ui/suggestions/suggest-swap.rs | 1 + tests/ui/suggestions/suggest-swap.stderr | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/ui/suggestions/suggest-swap.rs b/tests/ui/suggestions/suggest-swap.rs index 13d86a4b706f4..ef6604a42b2c0 100644 --- a/tests/ui/suggestions/suggest-swap.rs +++ b/tests/ui/suggestions/suggest-swap.rs @@ -1,3 +1,4 @@ +//! Regression test for use std::mem; fn main() { diff --git a/tests/ui/suggestions/suggest-swap.stderr b/tests/ui/suggestions/suggest-swap.stderr index 4c321ca8939f4..85fbbfb7e743c 100644 --- a/tests/ui/suggestions/suggest-swap.stderr +++ b/tests/ui/suggestions/suggest-swap.stderr @@ -1,5 +1,5 @@ error[E0499]: cannot borrow `arr[_]` as mutable more than once at a time - --> $DIR/suggest-swap.rs:11:9 + --> $DIR/suggest-swap.rs:12:9 | LL | mem::swap( | --------- first borrow later used by call