Skip to content

Commit c7c7ab2

Browse files
committed
integer_arithmetic: detect integer arithmetic on references.
Also fixes the same for float_arithmetic. changelog: integer_arithmetic,float_arithmetic: fix false negatives with references on integers Fixes rust-lang#5328
1 parent 23549a8 commit c7c7ab2

File tree

3 files changed

+162
-24
lines changed

3 files changed

+162
-24
lines changed

clippy_lints/src/arithmetic.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Arithmetic {
8181
| hir::BinOpKind::Gt => return,
8282
_ => (),
8383
}
84+
8485
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() {
86+
if l_ty.peel_refs().is_integral() && r_ty.peel_refs().is_integral() {
8687
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
8788
self.expr_span = Some(expr.span);
88-
} else if l_ty.is_floating_point() && r_ty.is_floating_point() {
89+
} else if l_ty.peel_refs().is_floating_point() && r_ty.peel_refs().is_floating_point() {
8990
span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
9091
self.expr_span = Some(expr.span);
9192
}

tests/ui/arithmetic.rs

+54-1
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,55 @@ fn main() {
9092
1 + 1
9193
};
9294
}
95+
96+
97+
}
98+
99+
// warn on references as well! (#5328)
100+
pub fn int_arith_ref() {
101+
3 + &1;
102+
&3 + 1;
103+
&3 + &1;
104+
}
105+
106+
pub fn foo(x: &i32) -> i32 {
107+
let a = 5;
108+
a + x
109+
}
110+
111+
pub fn bar(x: &i32, y: &i32) -> i32 {
112+
x + y
113+
}
114+
115+
pub fn baz(x: i32, y: &i32) -> i32 {
116+
x + y
117+
}
118+
119+
pub fn qux(x: i32, y: i32) -> i32 {
120+
(&x + &y)
121+
}
122+
123+
// also warn about floating point arith with references involved
124+
125+
pub fn float_arith_ref() {
126+
3.1_f32 + &1.2_f32;
127+
&3.4_f32 + 1.5_f32;
128+
&3.5_f32 + &1.3_f32;
129+
}
130+
131+
pub fn float_foo(f: &f32) -> f32 {
132+
let a = 5.1;
133+
a + f
134+
}
135+
136+
pub fn float_bar(f1: &f32, f2: &f32) -> f32 {
137+
f1 + f2
138+
}
139+
140+
pub fn float_baz(f1: f32, f2: &f32) -> f32 {
141+
f1 + f2
142+
}
143+
144+
pub fn float_qux(f1: f32, f2: f32) -> f32 {
145+
(&f1 + &f2)
93146
}

tests/ui/arithmetic.stderr

+105-21
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,211 @@
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:101:5
128+
|
129+
LL | 3 + &1;
130+
| ^^^^^^
131+
132+
error: integer arithmetic detected
133+
--> $DIR/arithmetic.rs:102:5
134+
|
135+
LL | &3 + 1;
136+
| ^^^^^^
137+
138+
error: integer arithmetic detected
139+
--> $DIR/arithmetic.rs:103:5
140+
|
141+
LL | &3 + &1;
142+
| ^^^^^^^
143+
144+
error: integer arithmetic detected
145+
--> $DIR/arithmetic.rs:108:5
146+
|
147+
LL | a + x
148+
| ^^^^^
149+
150+
error: integer arithmetic detected
151+
--> $DIR/arithmetic.rs:112:5
152+
|
153+
LL | x + y
154+
| ^^^^^
155+
156+
error: integer arithmetic detected
157+
--> $DIR/arithmetic.rs:116:5
158+
|
159+
LL | x + y
160+
| ^^^^^
161+
162+
error: integer arithmetic detected
163+
--> $DIR/arithmetic.rs:120:5
164+
|
165+
LL | (&x + &y)
166+
| ^^^^^^^^^
167+
168+
error: floating-point arithmetic detected
169+
--> $DIR/arithmetic.rs:126:5
170+
|
171+
LL | 3.1_f32 + &1.2_f32;
172+
| ^^^^^^^^^^^^^^^^^^
173+
174+
error: floating-point arithmetic detected
175+
--> $DIR/arithmetic.rs:127:5
176+
|
177+
LL | &3.4_f32 + 1.5_f32;
178+
| ^^^^^^^^^^^^^^^^^^
179+
180+
error: floating-point arithmetic detected
181+
--> $DIR/arithmetic.rs:128:5
182+
|
183+
LL | &3.5_f32 + &1.3_f32;
184+
| ^^^^^^^^^^^^^^^^^^^
185+
186+
error: floating-point arithmetic detected
187+
--> $DIR/arithmetic.rs:133:5
188+
|
189+
LL | a + f
190+
| ^^^^^
191+
192+
error: floating-point arithmetic detected
193+
--> $DIR/arithmetic.rs:137:5
194+
|
195+
LL | f1 + f2
196+
| ^^^^^^^
197+
198+
error: floating-point arithmetic detected
199+
--> $DIR/arithmetic.rs:141:5
200+
|
201+
LL | f1 + f2
202+
| ^^^^^^^
203+
204+
error: floating-point arithmetic detected
205+
--> $DIR/arithmetic.rs:145:5
206+
|
207+
LL | (&f1 + &f2)
208+
| ^^^^^^^^^^^
209+
210+
error: aborting due to 34 previous errors
127211

0 commit comments

Comments
 (0)