From 8bc1ded805ba25db8c926786946b71ea7a0c6ef8 Mon Sep 17 00:00:00 2001 From: Cristian Kubis Date: Sat, 31 Aug 2019 18:50:22 +0200 Subject: [PATCH] Fix incorrect swap suggestion Clippy suggests using swap on fields belonging to the same owner causing two mutable borrows of the owner Fixes #981 Signed-off-by: Cristian Kubis --- clippy_lints/src/swap.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/clippy_lints/src/swap.rs b/clippy_lints/src/swap.rs index 80c9a33c06a..79ff183c9ca 100644 --- a/clippy_lints/src/swap.rs +++ b/clippy_lints/src/swap.rs @@ -118,6 +118,14 @@ fn check_for_slice<'a>( None } + if let ExprKind::Field(ref lhs1, _) = lhs1.node { + if let ExprKind::Field(ref lhs2, _) = lhs2.node { + if lhs1.hir_id.owner_def_id() == lhs2.hir_id.owner_def_id() { + return; + } + } + } + let (replace, what, sugg) = if let Some((slice, idx1, idx2)) = check_for_slice(cx, lhs1, lhs2) { if let Some(slice) = Sugg::hir_opt(cx, slice) { (false, -- 2.44.0