|
1 | | -use clippy_utils::diagnostics::span_lint_and_then; |
| 1 | +use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then}; |
2 | 2 | use clippy_utils::source::snippet_with_applicability; |
3 | 3 | use clippy_utils::sugg::Sugg; |
4 | 4 | use clippy_utils::ty::is_type_diagnostic_item; |
5 | 5 | use clippy_utils::{differing_macro_contexts, eq_expr_value}; |
6 | 6 | use if_chain::if_chain; |
7 | 7 | use rustc_errors::Applicability; |
8 | | -use rustc_hir::{Block, Expr, ExprKind, PatKind, QPath, StmtKind}; |
| 8 | +use rustc_hir::{BinOpKind, Block, Expr, ExprKind, PatKind, QPath, Stmt, StmtKind}; |
9 | 9 | use rustc_lint::{LateContext, LateLintPass}; |
10 | 10 | use rustc_middle::ty; |
11 | 11 | use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 12 | +use rustc_span::source_map::Spanned; |
12 | 13 | use rustc_span::sym; |
13 | 14 |
|
14 | 15 | declare_clippy_lint! { |
@@ -64,12 +65,43 @@ declare_clippy_lint! { |
64 | 65 | "`foo = bar; bar = foo` sequence" |
65 | 66 | } |
66 | 67 |
|
67 | | -declare_lint_pass!(Swap => [MANUAL_SWAP, ALMOST_SWAPPED]); |
| 68 | +declare_clippy_lint! { |
| 69 | + /// **What it does:** Checks for uses of xor-based swaps. |
| 70 | + /// |
| 71 | + /// **Why is this bad?** The `std::mem::swap` function exposes the intent better |
| 72 | + /// without deinitializing or copying either variable. |
| 73 | + /// |
| 74 | + /// **Known problems:** None. |
| 75 | + /// |
| 76 | + /// **Example:** |
| 77 | + /// |
| 78 | + /// ```rust |
| 79 | + /// let mut a = 1; |
| 80 | + /// let mut b = 2; |
| 81 | + /// |
| 82 | + /// a ^= b; |
| 83 | + /// b ^= a; |
| 84 | + /// a ^= b; |
| 85 | + /// ``` |
| 86 | + /// |
| 87 | + /// Use std::mem::swap() instead: |
| 88 | + /// ```rust |
| 89 | + /// let mut a = 1; |
| 90 | + /// let mut b = 2; |
| 91 | + /// std::mem::swap(&mut a, &mut b); |
| 92 | + /// ``` |
| 93 | + pub XOR_SWAP, |
| 94 | + complexity, |
| 95 | + "xor-based swap of two variables" |
| 96 | +} |
| 97 | + |
| 98 | +declare_lint_pass!(Swap => [MANUAL_SWAP, ALMOST_SWAPPED, XOR_SWAP]); |
68 | 99 |
|
69 | 100 | impl<'tcx> LateLintPass<'tcx> for Swap { |
70 | 101 | fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'_>) { |
71 | 102 | check_manual_swap(cx, block); |
72 | 103 | check_suspicious_swap(cx, block); |
| 104 | + check_xor_swap(cx, block); |
73 | 105 | } |
74 | 106 | } |
75 | 107 |
|
@@ -262,3 +294,82 @@ fn check_suspicious_swap(cx: &LateContext<'_>, block: &Block<'_>) { |
262 | 294 | } |
263 | 295 | } |
264 | 296 | } |
| 297 | + |
| 298 | +/// Implementation of the `XOR_SWAP` lint. |
| 299 | +fn check_xor_swap(cx: &LateContext<'_>, block: &Block<'_>) { |
| 300 | + for window in block.stmts.windows(3) { |
| 301 | + if_chain! { |
| 302 | + if let Some((lhs0, rhs0)) = extract_sides_of_xor_assign(&window[0]); |
| 303 | + if let Some((lhs1, rhs1)) = extract_sides_of_xor_assign(&window[1]); |
| 304 | + if let Some((lhs2, rhs2)) = extract_sides_of_xor_assign(&window[2]); |
| 305 | + if eq_expr_value(cx, lhs0, rhs1) |
| 306 | + && eq_expr_value(cx, rhs1, lhs2) |
| 307 | + && eq_expr_value(cx, rhs0, lhs1) |
| 308 | + && eq_expr_value(cx, lhs1, rhs2); |
| 309 | + then { |
| 310 | + let span = window[0].span.to(window[2].span); |
| 311 | + let mut applicability = Applicability::MachineApplicable; |
| 312 | + match check_for_slice(cx, lhs0, rhs0) { |
| 313 | + Slice::Swappable(slice, idx0, idx1) => { |
| 314 | + if let Some(slice) = Sugg::hir_opt(cx, slice) { |
| 315 | + span_lint_and_sugg( |
| 316 | + cx, |
| 317 | + XOR_SWAP, |
| 318 | + span, |
| 319 | + &format!( |
| 320 | + "this xor-based swap of the elements of `{}` can be \ |
| 321 | + more clearly expressed using `.swap`", |
| 322 | + slice |
| 323 | + ), |
| 324 | + "try", |
| 325 | + format!( |
| 326 | + "{}.swap({}, {})", |
| 327 | + slice.maybe_par(), |
| 328 | + snippet_with_applicability(cx, idx0.span, "..", &mut applicability), |
| 329 | + snippet_with_applicability(cx, idx1.span, "..", &mut applicability) |
| 330 | + ), |
| 331 | + applicability |
| 332 | + ) |
| 333 | + } |
| 334 | + } |
| 335 | + Slice::None => { |
| 336 | + if let (Some(first), Some(second)) = (Sugg::hir_opt(cx, lhs0), Sugg::hir_opt(cx, rhs0)) { |
| 337 | + span_lint_and_sugg( |
| 338 | + cx, |
| 339 | + XOR_SWAP, |
| 340 | + span, |
| 341 | + &format!( |
| 342 | + "this xor-based swap of `{}` and `{}` can be \ |
| 343 | + more clearly expressed using `std::mem::swap`", |
| 344 | + first, second |
| 345 | + ), |
| 346 | + "try", |
| 347 | + format!("std::mem::swap({}, {})", first.mut_addr(), second.mut_addr()), |
| 348 | + applicability, |
| 349 | + ); |
| 350 | + } |
| 351 | + } |
| 352 | + Slice::NotSwappable => {} |
| 353 | + } |
| 354 | + } |
| 355 | + }; |
| 356 | + } |
| 357 | +} |
| 358 | + |
| 359 | +/// Returns the lhs and rhs of an xor assignment statement. |
| 360 | +fn extract_sides_of_xor_assign<'a, 'hir>(stmt: &'a Stmt<'hir>) -> Option<(&'a Expr<'hir>, &'a Expr<'hir>)> { |
| 361 | + if let StmtKind::Semi(expr) = stmt.kind { |
| 362 | + if let ExprKind::AssignOp( |
| 363 | + Spanned { |
| 364 | + node: BinOpKind::BitXor, |
| 365 | + .. |
| 366 | + }, |
| 367 | + lhs, |
| 368 | + rhs, |
| 369 | + ) = expr.kind |
| 370 | + { |
| 371 | + return Some((lhs, rhs)); |
| 372 | + } |
| 373 | + } |
| 374 | + None |
| 375 | +} |
0 commit comments