Skip to content

[arithmetic_side_effects] Fix #11266 #12710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 37 additions & 17 deletions clippy_lints/src/operators/arithmetic_side_effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ impl ArithmeticSideEffects {
false
}

/// For example, `fn foo<const N: usize>() -> i32 { N + 1 }`.
fn is_const_param(expr: &hir::Expr<'_>) -> bool {
if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
&& let hir::def::Res::Def(hir::def::DefKind::ConstParam, _) = path.res
{
return true;
}
false
}

// For example, 8i32 or &i64::MAX.
fn is_integral(ty: Ty<'_>) -> bool {
ty.peel_refs().is_integral()
Expand Down Expand Up @@ -233,24 +243,34 @@ impl ArithmeticSideEffects {
Self::literal_integer(cx, actual_lhs),
Self::literal_integer(cx, actual_rhs),
) {
(None, None) => false,
(None, Some(n)) => match (&op, n) {
// Division and module are always valid if applied to non-zero integers
(hir::BinOpKind::Div | hir::BinOpKind::Rem, local_n) if local_n != 0 => true,
// Adding or subtracting zeros is always a no-op
(hir::BinOpKind::Add | hir::BinOpKind::Sub, 0)
// Multiplication by 1 or 0 will never overflow
| (hir::BinOpKind::Mul, 0 | 1)
=> true,
_ => false,
(None, None) => Self::is_const_param(lhs) && Self::is_const_param(rhs),
(None, Some(n)) => {
if Self::is_const_param(lhs) {
return;
}
match (&op, n) {
// Division and module are always valid if applied to non-zero integers
(hir::BinOpKind::Div | hir::BinOpKind::Rem, local_n) if local_n != 0 => true,
// Adding or subtracting zeros is always a no-op
(hir::BinOpKind::Add | hir::BinOpKind::Sub, 0)
// Multiplication by 1 or 0 will never overflow
| (hir::BinOpKind::Mul, 0 | 1)
=> true,
_ => false,
}
},
(Some(n), None) => match (&op, n) {
// Adding or subtracting zeros is always a no-op
(hir::BinOpKind::Add | hir::BinOpKind::Sub, 0)
// Multiplication by 1 or 0 will never overflow
| (hir::BinOpKind::Mul, 0 | 1)
=> true,
_ => false,
(Some(n), None) => {
if Self::is_const_param(rhs) {
return;
}
match (&op, n) {
// Adding or subtracting zeros is always a no-op
(hir::BinOpKind::Add | hir::BinOpKind::Sub, 0)
// Multiplication by 1 or 0 will never overflow
| (hir::BinOpKind::Mul, 0 | 1)
=> true,
_ => false,
}
},
(Some(_), Some(_)) => {
matches!((lhs_ref_counter, rhs_ref_counter), (0, 0))
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/arithmetic_side_effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,19 @@ pub fn issue_11393() {
example_rem(x, maybe_zero);
}

pub fn issue_11266_literal<const N: usize>() {
let _ = N + 1;
let _ = 1 + N;
let _ = N + N;
}

pub fn issue_11266_runtime<const N: usize>() {
fn runtime_number() -> usize {
1
}
let _ = N + runtime_number();
}

pub fn issue_12318() {
use core::ops::{AddAssign, DivAssign, MulAssign, RemAssign, SubAssign};
let mut one: i32 = 1;
Expand Down
12 changes: 9 additions & 3 deletions tests/ui/arithmetic_side_effects.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -716,16 +716,22 @@ LL | x % maybe_zero
| ^^^^^^^^^^^^^^

error: arithmetic operation that can potentially result in unexpected side-effects
--> tests/ui/arithmetic_side_effects.rs:527:5
--> tests/ui/arithmetic_side_effects.rs:534:13
|
LL | let _ = N + runtime_number();
| ^^^^^^^^^^^^^^^^^^^^

error: arithmetic operation that can potentially result in unexpected side-effects
--> tests/ui/arithmetic_side_effects.rs:540:5
|
LL | one.add_assign(1);
| ^^^^^^^^^^^^^^^^^

error: arithmetic operation that can potentially result in unexpected side-effects
--> tests/ui/arithmetic_side_effects.rs:531:5
--> tests/ui/arithmetic_side_effects.rs:544:5
|
LL | one.sub_assign(1);
| ^^^^^^^^^^^^^^^^^

error: aborting due to 121 previous errors
error: aborting due to 122 previous errors