Skip to content

Commit 928a158

Browse files
committed
Allow manual swap in const fn
1 parent dfe37f1 commit 928a158

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

clippy_lints/src/swap.rs

Lines changed: 5 additions & 1 deletion
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};
@@ -138,6 +138,10 @@ fn generate_swap_warning(cx: &LateContext<'_>, e1: &Expr<'_>, e2: &Expr<'_>, spa
138138

139139
/// Implementation of the `MANUAL_SWAP` lint.
140140
fn check_manual_swap(cx: &LateContext<'_>, block: &Block<'_>) {
141+
if in_constant(cx, block.hir_id) {
142+
return;
143+
}
144+
141145
for w in block.stmts.windows(3) {
142146
if_chain! {
143147
// let t = foo();

tests/ui/swap.fixed

Lines changed: 9 additions & 0 deletions
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

Lines changed: 9 additions & 0 deletions
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)