Skip to content

Commit 7f0d8b3

Browse files
committed
integer_arithmetic: detect integer arithmetic on references.
changelog: integer_arithmetic fix false negatives with references on integers Fixes #5328
1 parent 23549a8 commit 7f0d8b3

File tree

3 files changed

+90
-23
lines changed

3 files changed

+90
-23
lines changed

clippy_lints/src/arithmetic.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Arithmetic {
8282
_ => (),
8383
}
8484
let (l_ty, r_ty) = (cx.tables.expr_ty(l), cx.tables.expr_ty(r));
85-
if l_ty.is_integral() && r_ty.is_integral() {
85+
// warn on integers as well as references to integers
86+
if l_ty.is_integral() || l_ty.is_any_ptr() && r_ty.is_integral() || r_ty.is_any_ptr() {
8687
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
8788
self.expr_span = Some(expr.span);
8889
} else if l_ty.is_floating_point() && r_ty.is_floating_point() {

tests/ui/arithmetic.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
clippy::shadow_reuse,
55
clippy::shadow_unrelated,
66
clippy::no_effect,
7-
clippy::unnecessary_operation
7+
clippy::unnecessary_operation,
8+
clippy::op_ref,
9+
clippy::trivially_copy_pass_by_ref
810
)]
911

1012
#[rustfmt::skip]
@@ -90,4 +92,26 @@ fn main() {
9092
1 + 1
9193
};
9294
}
95+
96+
// warn on references as well! (#5328)
97+
3 + &1;
98+
&3 + 1;
99+
&3 + &1;
100+
}
101+
102+
pub fn foo(x: &i32) -> i32 {
103+
let a = 5;
104+
a + x
105+
}
106+
107+
pub fn bar(x: &i32, y: &i32) -> i32 {
108+
x + y
109+
}
110+
111+
pub fn baz(x: i32, y: &i32) -> i32 {
112+
x + y
113+
}
114+
115+
pub fn qux(x: i32, y: i32) -> i32 {
116+
(&x + &y)
93117
}

tests/ui/arithmetic.stderr

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,169 @@
11
error: integer arithmetic detected
2-
--> $DIR/arithmetic.rs:13:5
2+
--> $DIR/arithmetic.rs:15:5
33
|
44
LL | 1 + i;
55
| ^^^^^
66
|
77
= note: `-D clippy::integer-arithmetic` implied by `-D warnings`
88

99
error: integer arithmetic detected
10-
--> $DIR/arithmetic.rs:14:5
10+
--> $DIR/arithmetic.rs:16:5
1111
|
1212
LL | i * 2;
1313
| ^^^^^
1414

1515
error: integer arithmetic detected
16-
--> $DIR/arithmetic.rs:15:5
16+
--> $DIR/arithmetic.rs:17:5
1717
|
1818
LL | / 1 %
1919
LL | | i / 2; // no error, this is part of the expression in the preceding line
2020
| |_________^
2121

2222
error: integer arithmetic detected
23-
--> $DIR/arithmetic.rs:17:5
23+
--> $DIR/arithmetic.rs:19:5
2424
|
2525
LL | i - 2 + 2 - i;
2626
| ^^^^^^^^^^^^^
2727

2828
error: integer arithmetic detected
29-
--> $DIR/arithmetic.rs:18:5
29+
--> $DIR/arithmetic.rs:20:5
3030
|
3131
LL | -i;
3232
| ^^
3333

3434
error: integer arithmetic detected
35-
--> $DIR/arithmetic.rs:30:5
35+
--> $DIR/arithmetic.rs:32:5
3636
|
3737
LL | i += 1;
3838
| ^^^^^^
3939

4040
error: integer arithmetic detected
41-
--> $DIR/arithmetic.rs:31:5
41+
--> $DIR/arithmetic.rs:33:5
4242
|
4343
LL | i -= 1;
4444
| ^^^^^^
4545

4646
error: integer arithmetic detected
47-
--> $DIR/arithmetic.rs:32:5
47+
--> $DIR/arithmetic.rs:34:5
4848
|
4949
LL | i *= 2;
5050
| ^^^^^^
5151

5252
error: integer arithmetic detected
53-
--> $DIR/arithmetic.rs:33:5
53+
--> $DIR/arithmetic.rs:35:5
5454
|
5555
LL | i /= 2;
5656
| ^^^^^^
5757

5858
error: integer arithmetic detected
59-
--> $DIR/arithmetic.rs:34:5
59+
--> $DIR/arithmetic.rs:36:5
6060
|
6161
LL | i %= 2;
6262
| ^^^^^^
6363

6464
error: floating-point arithmetic detected
65-
--> $DIR/arithmetic.rs:45:5
65+
--> $DIR/arithmetic.rs:47:5
6666
|
6767
LL | f * 2.0;
6868
| ^^^^^^^
6969
|
7070
= note: `-D clippy::float-arithmetic` implied by `-D warnings`
7171

7272
error: floating-point arithmetic detected
73-
--> $DIR/arithmetic.rs:47:5
73+
--> $DIR/arithmetic.rs:49:5
7474
|
7575
LL | 1.0 + f;
7676
| ^^^^^^^
7777

7878
error: floating-point arithmetic detected
79-
--> $DIR/arithmetic.rs:48:5
79+
--> $DIR/arithmetic.rs:50:5
8080
|
8181
LL | f * 2.0;
8282
| ^^^^^^^
8383

8484
error: floating-point arithmetic detected
85-
--> $DIR/arithmetic.rs:49:5
85+
--> $DIR/arithmetic.rs:51:5
8686
|
8787
LL | f / 2.0;
8888
| ^^^^^^^
8989

9090
error: floating-point arithmetic detected
91-
--> $DIR/arithmetic.rs:50:5
91+
--> $DIR/arithmetic.rs:52:5
9292
|
9393
LL | f - 2.0 * 4.2;
9494
| ^^^^^^^^^^^^^
9595

9696
error: floating-point arithmetic detected
97-
--> $DIR/arithmetic.rs:51:5
97+
--> $DIR/arithmetic.rs:53:5
9898
|
9999
LL | -f;
100100
| ^^
101101

102102
error: floating-point arithmetic detected
103-
--> $DIR/arithmetic.rs:53:5
103+
--> $DIR/arithmetic.rs:55:5
104104
|
105105
LL | f += 1.0;
106106
| ^^^^^^^^
107107

108108
error: floating-point arithmetic detected
109-
--> $DIR/arithmetic.rs:54:5
109+
--> $DIR/arithmetic.rs:56:5
110110
|
111111
LL | f -= 1.0;
112112
| ^^^^^^^^
113113

114114
error: floating-point arithmetic detected
115-
--> $DIR/arithmetic.rs:55:5
115+
--> $DIR/arithmetic.rs:57:5
116116
|
117117
LL | f *= 2.0;
118118
| ^^^^^^^^
119119

120120
error: floating-point arithmetic detected
121-
--> $DIR/arithmetic.rs:56:5
121+
--> $DIR/arithmetic.rs:58:5
122122
|
123123
LL | f /= 2.0;
124124
| ^^^^^^^^
125125

126-
error: aborting due to 20 previous errors
126+
error: integer arithmetic detected
127+
--> $DIR/arithmetic.rs:97:5
128+
|
129+
LL | 3 + &1;
130+
| ^^^^^^
131+
132+
error: integer arithmetic detected
133+
--> $DIR/arithmetic.rs:98:5
134+
|
135+
LL | &3 + 1;
136+
| ^^^^^^
137+
138+
error: integer arithmetic detected
139+
--> $DIR/arithmetic.rs:99:5
140+
|
141+
LL | &3 + &1;
142+
| ^^^^^^^
143+
144+
error: integer arithmetic detected
145+
--> $DIR/arithmetic.rs:104:5
146+
|
147+
LL | a + x
148+
| ^^^^^
149+
150+
error: integer arithmetic detected
151+
--> $DIR/arithmetic.rs:108:5
152+
|
153+
LL | x + y
154+
| ^^^^^
155+
156+
error: integer arithmetic detected
157+
--> $DIR/arithmetic.rs:112:5
158+
|
159+
LL | x + y
160+
| ^^^^^
161+
162+
error: integer arithmetic detected
163+
--> $DIR/arithmetic.rs:116:5
164+
|
165+
LL | (&x + &y)
166+
| ^^^^^^^^^
167+
168+
error: aborting due to 27 previous errors
127169

0 commit comments

Comments
 (0)