Skip to content

Commit e144c7d

Browse files
committed
Auto merge of #9871 - koka831:fix/9864, r=xFrednet
Allow manual swap in const fn Fix #9864 changelog: Fix [`manual_swap`]: No longer lints in constant code
2 parents d019fd9 + 3c86cad commit e144c7d

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

clippy_lints/src/swap.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
22
use clippy_utils::source::snippet_with_applicability;
33
use clippy_utils::sugg::Sugg;
44
use clippy_utils::ty::is_type_diagnostic_item;
5-
use clippy_utils::{can_mut_borrow_both, eq_expr_value, std_or_core};
5+
use clippy_utils::{can_mut_borrow_both, eq_expr_value, in_constant, std_or_core};
66
use if_chain::if_chain;
77
use rustc_errors::Applicability;
88
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, PatKind, QPath, Stmt, StmtKind};
@@ -16,6 +16,8 @@ declare_clippy_lint! {
1616
/// ### What it does
1717
/// Checks for manual swapping.
1818
///
19+
/// Note that the lint will not be emitted in const blocks, as the suggestion would not be applicable.
20+
///
1921
/// ### Why is this bad?
2022
/// The `std::mem::swap` function exposes the intent better
2123
/// without deinitializing or copying either variable.
@@ -138,6 +140,10 @@ fn generate_swap_warning(cx: &LateContext<'_>, e1: &Expr<'_>, e2: &Expr<'_>, spa
138140

139141
/// Implementation of the `MANUAL_SWAP` lint.
140142
fn check_manual_swap(cx: &LateContext<'_>, block: &Block<'_>) {
143+
if in_constant(cx, block.hir_id) {
144+
return;
145+
}
146+
141147
for w in block.stmts.windows(3) {
142148
if_chain! {
143149
// let t = foo();

tests/ui/swap.fixed

+9
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,12 @@ fn issue_8154() {
155155
let s = S3(&mut s);
156156
std::mem::swap(&mut s.0.x, &mut s.0.y);
157157
}
158+
159+
const fn issue_9864(mut u: u32) -> u32 {
160+
let mut v = 10;
161+
162+
let temp = u;
163+
u = v;
164+
v = temp;
165+
u + v
166+
}

tests/ui/swap.rs

+9
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,12 @@ fn issue_8154() {
179179
s.0.x = s.0.y;
180180
s.0.y = t;
181181
}
182+
183+
const fn issue_9864(mut u: u32) -> u32 {
184+
let mut v = 10;
185+
186+
let temp = u;
187+
u = v;
188+
v = temp;
189+
u + v
190+
}

0 commit comments

Comments
 (0)