Skip to content

Commit 51505d4

Browse files
committed
[aritmetic_side_effects] Fix #11266
1 parent b2a7371 commit 51505d4

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

clippy_lints/src/operators/arithmetic_side_effects.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,16 @@ impl ArithmeticSideEffects {
132132
false
133133
}
134134

135+
/// For example, `fn foo<const N: usize>() -> i32 { N + 1 }`.
136+
fn is_const_param(expr: &hir::Expr<'_>) -> bool {
137+
if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
138+
&& let hir::def::Res::Def(hir::def::DefKind::ConstParam, _) = path.res
139+
{
140+
return true;
141+
}
142+
false
143+
}
144+
135145
// For example, 8i32 or &i64::MAX.
136146
fn is_integral(ty: Ty<'_>) -> bool {
137147
ty.peel_refs().is_integral()
@@ -197,6 +207,9 @@ impl ArithmeticSideEffects {
197207
lhs: &'tcx hir::Expr<'_>,
198208
rhs: &'tcx hir::Expr<'_>,
199209
) {
210+
if Self::is_const_param(lhs) || Self::is_const_param(rhs) {
211+
return;
212+
}
200213
if constant_simple(cx, cx.typeck_results(), expr).is_some() {
201214
return;
202215
}

tests/ui/arithmetic_side_effects.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,10 @@ pub fn issue_11393() {
521521
example_rem(x, maybe_zero);
522522
}
523523

524+
pub fn issue_11266<const N: usize>() {
525+
let _ = N + 1;
526+
}
527+
524528
pub fn issue_12318() {
525529
use core::ops::{AddAssign, DivAssign, MulAssign, RemAssign, SubAssign};
526530
let mut one: i32 = 1;

tests/ui/arithmetic_side_effects.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,13 +716,13 @@ LL | x % maybe_zero
716716
| ^^^^^^^^^^^^^^
717717

718718
error: arithmetic operation that can potentially result in unexpected side-effects
719-
--> tests/ui/arithmetic_side_effects.rs:527:5
719+
--> tests/ui/arithmetic_side_effects.rs:531:5
720720
|
721721
LL | one.add_assign(1);
722722
| ^^^^^^^^^^^^^^^^^
723723

724724
error: arithmetic operation that can potentially result in unexpected side-effects
725-
--> tests/ui/arithmetic_side_effects.rs:531:5
725+
--> tests/ui/arithmetic_side_effects.rs:535:5
726726
|
727727
LL | one.sub_assign(1);
728728
| ^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)