Skip to content

Commit 4876882

Browse files
committed
Fix #9544
1 parent 68408c5 commit 4876882

File tree

3 files changed

+243
-49
lines changed

3 files changed

+243
-49
lines changed

clippy_lints/src/operators/arithmetic_side_effects.rs

+18-19
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ const HARD_CODED_ALLOWED: &[&str] = &[
1616
"f32",
1717
"f64",
1818
"std::num::Saturating",
19-
"std::string::String",
2019
"std::num::Wrapping",
20+
"std::string::String",
21+
"&str",
2122
];
2223

2324
#[derive(Debug)]
@@ -77,6 +78,11 @@ impl ArithmeticSideEffects {
7778
)
7879
}
7980

81+
// For example, 8i32 or &i64::MAX.
82+
fn is_integral<'expr, 'tcx>(cx: &LateContext<'tcx>, expr: &'expr hir::Expr<'tcx>) -> bool {
83+
cx.typeck_results().expr_ty(expr).peel_refs().is_integral()
84+
}
85+
8086
// Common entry-point to avoid code duplication.
8187
fn issue_lint(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) {
8288
let msg = "arithmetic operation that can potentially result in unexpected side-effects";
@@ -88,24 +94,13 @@ impl ArithmeticSideEffects {
8894
/// * Is `expr` is a literal integer reference like `&199`, returns the literal integer without
8995
/// references.
9096
/// * If `expr` is anything else, returns `None`.
91-
fn literal_integer<'expr, 'tcx>(
92-
cx: &LateContext<'tcx>,
93-
expr: &'expr hir::Expr<'tcx>,
94-
) -> Option<&'expr hir::Expr<'tcx>> {
95-
let expr_refs = cx.typeck_results().expr_ty(expr).peel_refs();
96-
97-
if !expr_refs.is_integral() {
98-
return None;
99-
}
100-
97+
fn literal_integer<'expr, 'tcx>(expr: &'expr hir::Expr<'tcx>) -> Option<&'expr hir::Expr<'tcx>> {
10198
if matches!(expr.kind, hir::ExprKind::Lit(_)) {
10299
return Some(expr);
103100
}
104-
105101
if let hir::ExprKind::AddrOf(.., inn) = expr.kind && let hir::ExprKind::Lit(_) = inn.kind {
106102
return Some(inn)
107103
}
108-
109104
None
110105
}
111106

@@ -134,14 +129,18 @@ impl ArithmeticSideEffects {
134129
) {
135130
return;
136131
};
137-
if self.is_allowed_ty(cx, lhs) || self.is_allowed_ty(cx, rhs) {
132+
if self.is_allowed_ty(cx, lhs) && self.is_allowed_ty(cx, rhs) {
138133
return;
139134
}
140-
let has_valid_op = match (Self::literal_integer(cx, lhs), Self::literal_integer(cx, rhs)) {
141-
(None, None) => false,
142-
(None, Some(local_expr)) => Self::has_valid_op(op, local_expr),
143-
(Some(local_expr), None) => Self::has_valid_op(op, local_expr),
144-
(Some(_), Some(_)) => true,
135+
let has_valid_op = if Self::is_integral(cx, lhs) && Self::is_integral(cx, rhs) {
136+
match (Self::literal_integer(lhs), Self::literal_integer(rhs)) {
137+
(None, None) => false,
138+
(None, Some(local_expr)) => Self::has_valid_op(op, local_expr),
139+
(Some(local_expr), None) => Self::has_valid_op(op, local_expr),
140+
(Some(_), Some(_)) => true,
141+
}
142+
} else {
143+
false
145144
};
146145
if !has_valid_op {
147146
self.issue_lint(cx, expr);

tests/ui/arithmetic_side_effects.rs

+52-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,31 @@
1212

1313
use core::num::{Saturating, Wrapping};
1414

15+
pub struct Custom;
16+
17+
macro_rules! impl_arith {
18+
( $( $_trait:ident, $ty:ty, $method:ident; )* ) => {
19+
$(
20+
impl core::ops::$_trait<$ty> for Custom {
21+
type Output = Self;
22+
fn $method(self, _: $ty) -> Self::Output { Self }
23+
}
24+
)*
25+
}
26+
}
27+
28+
impl_arith!(
29+
Add, i32, add;
30+
Div, i32, div;
31+
Mul, i32, mul;
32+
Sub, i32, sub;
33+
34+
Add, f64, add;
35+
Div, f64, div;
36+
Mul, f64, mul;
37+
Sub, f64, sub;
38+
);
39+
1540
pub fn association_with_structures_should_not_trigger_the_lint() {
1641
enum Foo {
1742
Bar = -2,
@@ -130,7 +155,7 @@ pub fn non_overflowing_ops_or_ops_already_handled_by_the_compiler_should_not_tri
130155
_n = -(-1);
131156
}
132157

133-
pub fn overflowing_runtime_ops() {
158+
pub fn runtime_ops() {
134159
let mut _n = i32::MAX;
135160

136161
// Assign
@@ -163,6 +188,32 @@ pub fn overflowing_runtime_ops() {
163188
_n = 2 * _n;
164189
_n = &2 * _n;
165190

191+
// Custom
192+
let _ = Custom + 0;
193+
let _ = Custom + 1;
194+
let _ = Custom + 2;
195+
let _ = Custom + 0.0;
196+
let _ = Custom + 1.0;
197+
let _ = Custom + 2.0;
198+
let _ = Custom - 0;
199+
let _ = Custom - 1;
200+
let _ = Custom - 2;
201+
let _ = Custom - 0.0;
202+
let _ = Custom - 1.0;
203+
let _ = Custom - 2.0;
204+
let _ = Custom / 0;
205+
let _ = Custom / 1;
206+
let _ = Custom / 2;
207+
let _ = Custom / 0.0;
208+
let _ = Custom / 1.0;
209+
let _ = Custom / 2.0;
210+
let _ = Custom * 0;
211+
let _ = Custom * 1;
212+
let _ = Custom * 2;
213+
let _ = Custom * 0.0;
214+
let _ = Custom * 1.0;
215+
let _ = Custom * 2.0;
216+
166217
// Unary
167218
_n = -_n;
168219
_n = -&_n;

0 commit comments

Comments
 (0)