Allow manual swap in const fn

This commit is contained in:
koka 2022-11-18 21:51:43 +09:00
parent dfe37f13cf
commit 928a158716
No known key found for this signature in database
GPG Key ID: A5917A40697774CD
3 changed files with 23 additions and 1 deletions

View File

@ -2,7 +2,7 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::sugg::Sugg;
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::{can_mut_borrow_both, eq_expr_value, std_or_core};
use clippy_utils::{can_mut_borrow_both, eq_expr_value, in_constant, std_or_core};
use if_chain::if_chain;
use rustc_errors::Applicability;
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
/// Implementation of the `MANUAL_SWAP` lint.
fn check_manual_swap(cx: &LateContext<'_>, block: &Block<'_>) {
if in_constant(cx, block.hir_id) {
return;
}
for w in block.stmts.windows(3) {
if_chain! {
// let t = foo();

View File

@ -155,3 +155,12 @@ fn issue_8154() {
let s = S3(&mut s);
std::mem::swap(&mut s.0.x, &mut s.0.y);
}
const fn issue_9864(mut u: u32) -> u32 {
let mut v = 10;
let temp = u;
u = v;
v = temp;
u + v
}

View File

@ -179,3 +179,12 @@ fn issue_8154() {
s.0.x = s.0.y;
s.0.y = t;
}
const fn issue_9864(mut u: u32) -> u32 {
let mut v = 10;
let temp = u;
u = v;
v = temp;
u + v
}