Skip to content

Commit 99363fe

Browse files
committed
Address comments
1 parent 4876882 commit 99363fe

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

clippy_lints/src/operators/arithmetic_side_effects.rs

+11-13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_ast as ast;
99
use rustc_data_structures::fx::FxHashSet;
1010
use rustc_hir as hir;
1111
use rustc_lint::{LateContext, LateLintPass};
12+
use rustc_middle::ty::Ty;
1213
use rustc_session::impl_lint_pass;
1314
use rustc_span::source_map::{Span, Spanned};
1415

@@ -67,20 +68,14 @@ impl ArithmeticSideEffects {
6768
}
6869

6970
/// Checks if the given `expr` has any of the inner `allowed` elements.
70-
fn is_allowed_ty(&self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool {
71-
self.allowed.contains(
72-
cx.typeck_results()
73-
.expr_ty(expr)
74-
.to_string()
75-
.split('<')
76-
.next()
77-
.unwrap_or_default(),
78-
)
71+
fn is_allowed_ty(&self, ty: Ty<'_>) -> bool {
72+
self.allowed
73+
.contains(ty.to_string().split('<').next().unwrap_or_default())
7974
}
8075

8176
// 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()
77+
fn is_integral(ty: Ty<'_>) -> bool {
78+
ty.peel_refs().is_integral()
8479
}
8580

8681
// Common entry-point to avoid code duplication.
@@ -129,10 +124,13 @@ impl ArithmeticSideEffects {
129124
) {
130125
return;
131126
};
132-
if self.is_allowed_ty(cx, lhs) && self.is_allowed_ty(cx, rhs) {
127+
let lhs_ty = cx.typeck_results().expr_ty(lhs);
128+
let rhs_ty = cx.typeck_results().expr_ty(rhs);
129+
let lhs_and_rhs_have_the_same_ty = lhs_ty == rhs_ty;
130+
if lhs_and_rhs_have_the_same_ty && self.is_allowed_ty(lhs_ty) && self.is_allowed_ty(rhs_ty) {
133131
return;
134132
}
135-
let has_valid_op = if Self::is_integral(cx, lhs) && Self::is_integral(cx, rhs) {
133+
let has_valid_op = if Self::is_integral(lhs_ty) && Self::is_integral(rhs_ty) {
136134
match (Self::literal_integer(lhs), Self::literal_integer(rhs)) {
137135
(None, None) => false,
138136
(None, Some(local_expr)) => Self::has_valid_op(op, local_expr),

tests/ui/arithmetic_side_effects.stderr

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
1+
error: arithmetic operation that can potentially result in unexpected side-effects
2+
--> $DIR/arithmetic_side_effects.rs:78:13
3+
|
4+
LL | let _ = String::new() + "";
5+
| ^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::arithmetic-side-effects` implied by `-D warnings`
8+
9+
error: arithmetic operation that can potentially result in unexpected side-effects
10+
--> $DIR/arithmetic_side_effects.rs:86:27
11+
|
12+
LL | let inferred_string = string + "";
13+
| ^^^^^^^^^^^
14+
15+
error: arithmetic operation that can potentially result in unexpected side-effects
16+
--> $DIR/arithmetic_side_effects.rs:90:13
17+
|
18+
LL | let _ = inferred_string + "";
19+
| ^^^^^^^^^^^^^^^^^^^^
20+
121
error: arithmetic operation that can potentially result in unexpected side-effects
222
--> $DIR/arithmetic_side_effects.rs:162:5
323
|
424
LL | _n += 1;
525
| ^^^^^^^
6-
|
7-
= note: `-D clippy::arithmetic-side-effects` implied by `-D warnings`
826

927
error: arithmetic operation that can potentially result in unexpected side-effects
1028
--> $DIR/arithmetic_side_effects.rs:163:5
@@ -312,5 +330,5 @@ error: arithmetic operation that can potentially result in unexpected side-effec
312330
LL | _n = -&_n;
313331
| ^^^^
314332

315-
error: aborting due to 52 previous errors
333+
error: aborting due to 55 previous errors
316334

0 commit comments

Comments
 (0)