From 6631480a7b7cb7b3e7d53c9c4cd7915f7ebc3110 Mon Sep 17 00:00:00 2001 From: blyxyas Date: Mon, 13 Mar 2023 19:13:56 +0100 Subject: [PATCH] Fix false positive with `a = a` --- clippy_lints/src/swap.rs | 1 + tests/ui/swap.fixed | 8 ++++++++ tests/ui/swap.rs | 8 ++++++++ 3 files changed, 17 insertions(+) diff --git a/clippy_lints/src/swap.rs b/clippy_lints/src/swap.rs index 1aeac724ab1..5087c19e5f3 100644 --- a/clippy_lints/src/swap.rs +++ b/clippy_lints/src/swap.rs @@ -190,6 +190,7 @@ fn check_suspicious_swap(cx: &LateContext<'_>, block: &Block<'_>) { && first.span.eq_ctxt(second.span) && is_same(cx, lhs0, rhs1) && is_same(cx, lhs1, rhs0) + && !is_same(cx, lhs1, rhs1) // Ignore a = b; a = a (#10421) && let Some(lhs_sugg) = match &lhs0 { ExprOrIdent::Expr(expr) => Sugg::hir_opt(cx, expr), ExprOrIdent::Ident(ident) => Some(Sugg::NonParen(ident.as_str().into())), diff --git a/tests/ui/swap.fixed b/tests/ui/swap.fixed index 04008c0d9b3..775b0dbde88 100644 --- a/tests/ui/swap.fixed +++ b/tests/ui/swap.fixed @@ -186,3 +186,11 @@ const fn issue_9864(mut u: u32) -> u32 { v = temp; u + v } + +#[allow(clippy::let_and_return)] +const fn issue_10421(x: u32) -> u32 { + let a = x; + let a = a; + let a = a; + a +} diff --git a/tests/ui/swap.rs b/tests/ui/swap.rs index ef8a81c8341..bc9a78b49c7 100644 --- a/tests/ui/swap.rs +++ b/tests/ui/swap.rs @@ -215,3 +215,11 @@ const fn issue_9864(mut u: u32) -> u32 { v = temp; u + v } + +#[allow(clippy::let_and_return)] +const fn issue_10421(x: u32) -> u32 { + let a = x; + let a = a; + let a = a; + a +}