Skip to content

Commit 3254f02

Browse files
authored
arithmetic_side_effects: check adjusted expression types (rust-lang#14062)
Fix rust-lang#14054 changelog: [`arithmetic_side_effects`]: warn about more cases when `Deref` is involved
2 parents 106db26 + 69ff46e commit 3254f02

File tree

3 files changed

+166
-125
lines changed

3 files changed

+166
-125
lines changed

clippy_lints/src/operators/arithmetic_side_effects.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ impl ArithmeticSideEffects {
220220
actual_lhs = expr_or_init(cx, actual_lhs);
221221
actual_rhs = expr_or_init(cx, actual_rhs);
222222
let lhs_ty = cx.typeck_results().expr_ty(actual_lhs).peel_refs();
223-
let rhs_ty = cx.typeck_results().expr_ty(actual_rhs).peel_refs();
223+
let rhs_ty = cx.typeck_results().expr_ty_adjusted(actual_rhs).peel_refs();
224224
if self.has_allowed_binary(lhs_ty, rhs_ty) {
225225
return;
226226
}
@@ -283,7 +283,7 @@ impl ArithmeticSideEffects {
283283
if ConstEvalCtxt::new(cx).eval_simple(receiver).is_some() {
284284
return;
285285
}
286-
let instance_ty = cx.typeck_results().expr_ty(receiver);
286+
let instance_ty = cx.typeck_results().expr_ty_adjusted(receiver);
287287
if !Self::is_integral(instance_ty) {
288288
return;
289289
}
@@ -311,7 +311,7 @@ impl ArithmeticSideEffects {
311311
if ConstEvalCtxt::new(cx).eval(un_expr).is_some() {
312312
return;
313313
}
314-
let ty = cx.typeck_results().expr_ty(expr).peel_refs();
314+
let ty = cx.typeck_results().expr_ty_adjusted(expr).peel_refs();
315315
if self.has_allowed_unary(ty) {
316316
return;
317317
}

tests/ui/arithmetic_side_effects.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ pub fn hard_coded_allowed() {
170170

171171
let _ = Saturating(0u32) + Saturating(0u32);
172172
let _ = String::new() + "";
173+
let _ = String::new() + &String::new();
173174
let _ = Wrapping(0u32) + Wrapping(0u32);
174175

175176
let saturating: Saturating<u32> = Saturating(0u32);
@@ -408,11 +409,14 @@ pub fn unknown_ops_or_runtime_ops_that_can_overflow() {
408409
_n.wrapping_rem(_n);
409410
_n.wrapping_rem_euclid(_n);
410411

412+
_n.saturating_div(*Box::new(_n));
413+
411414
// Unary
412415
_n = -_n;
413416
_n = -&_n;
414417
_custom = -_custom;
415418
_custom = -&_custom;
419+
_ = -*Box::new(_n);
416420
}
417421

418422
// Copied and pasted from the `integer_arithmetic` lint for comparison.
@@ -534,4 +538,11 @@ pub fn issue_12318() {
534538
one.sub_assign(1);
535539
}
536540

541+
pub fn explicit_methods() {
542+
use core::ops::Add;
543+
let one: i32 = 1;
544+
one.add(&one);
545+
Box::new(one).add(one);
546+
}
547+
537548
fn main() {}

0 commit comments

Comments
 (0)