Skip to content

Commit c465bf7

Browse files
committed
Auto merge of #10499 - blyxyas:fix-almost_swapped, r=giraffate
Fix `almost_swapped` false positive (`let mut a = b; a = a`) Fixes `2` in #10421 changelog: [`almost_swapped`]: Fix false positive when a variable is changed to itself. (`a = a`)
2 parents 5941616 + 6631480 commit c465bf7

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

clippy_lints/src/swap.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ fn check_suspicious_swap(cx: &LateContext<'_>, block: &Block<'_>) {
190190
&& first.span.eq_ctxt(second.span)
191191
&& is_same(cx, lhs0, rhs1)
192192
&& is_same(cx, lhs1, rhs0)
193+
&& !is_same(cx, lhs1, rhs1) // Ignore a = b; a = a (#10421)
193194
&& let Some(lhs_sugg) = match &lhs0 {
194195
ExprOrIdent::Expr(expr) => Sugg::hir_opt(cx, expr),
195196
ExprOrIdent::Ident(ident) => Some(Sugg::NonParen(ident.as_str().into())),

tests/ui/swap.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,11 @@ const fn issue_9864(mut u: u32) -> u32 {
186186
v = temp;
187187
u + v
188188
}
189+
190+
#[allow(clippy::let_and_return)]
191+
const fn issue_10421(x: u32) -> u32 {
192+
let a = x;
193+
let a = a;
194+
let a = a;
195+
a
196+
}

tests/ui/swap.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,11 @@ const fn issue_9864(mut u: u32) -> u32 {
215215
v = temp;
216216
u + v
217217
}
218+
219+
#[allow(clippy::let_and_return)]
220+
const fn issue_10421(x: u32) -> u32 {
221+
let a = x;
222+
let a = a;
223+
let a = a;
224+
a
225+
}

0 commit comments

Comments
 (0)