Skip to content

Commit

Permalink
[aritmetic_side_effects] Fix rust-lang#11266
Browse files Browse the repository at this point in the history
  • Loading branch information
c410-f3r committed Apr 24, 2024
1 parent b2a7371 commit 51505d4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
13 changes: 13 additions & 0 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 @@ -197,6 +207,9 @@ impl ArithmeticSideEffects {
lhs: &'tcx hir::Expr<'_>,
rhs: &'tcx hir::Expr<'_>,
) {
if Self::is_const_param(lhs) || Self::is_const_param(rhs) {
return;
}
if constant_simple(cx, cx.typeck_results(), expr).is_some() {
return;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/arithmetic_side_effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,10 @@ pub fn issue_11393() {
example_rem(x, maybe_zero);
}

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

pub fn issue_12318() {
use core::ops::{AddAssign, DivAssign, MulAssign, RemAssign, SubAssign};
let mut one: i32 = 1;
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/arithmetic_side_effects.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -716,13 +716,13 @@ 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:531: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:535:5
|
LL | one.sub_assign(1);
| ^^^^^^^^^^^^^^^^^
Expand Down

0 comments on commit 51505d4

Please sign in to comment.