Skip to content

Commit fe3200c

Browse files
committed
Auto merge of rust-lang#9584 - royrustdev:implicit_saturating_sub, r=llogiq
add tests in `implicit_saturating_sub` lint This adds more tests to the `implicit_saturating_sub` lint to rule out certain false positives that have appeared in the past. Now with those false positives out of the equation, we can move the lint to `style`. --- changelog: promote [`implicit-saturating-sub`] to the `style` category
2 parents 58ef56e + ac6d2ba commit fe3200c

7 files changed

+126
-25
lines changed

clippy_lints/src/implicit_saturating_sub.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ declare_clippy_lint! {
3535
/// ```
3636
#[clippy::version = "1.44.0"]
3737
pub IMPLICIT_SATURATING_SUB,
38-
pedantic,
38+
style,
3939
"Perform saturating subtraction instead of implicitly checking lower bound of data type"
4040
}
4141

clippy_lints/src/lib.register_all.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
8989
LintId::of(functions::TOO_MANY_ARGUMENTS),
9090
LintId::of(if_let_mutex::IF_LET_MUTEX),
9191
LintId::of(implicit_saturating_add::IMPLICIT_SATURATING_ADD),
92+
LintId::of(implicit_saturating_sub::IMPLICIT_SATURATING_SUB),
9293
LintId::of(indexing_slicing::OUT_OF_BOUNDS_INDEXING),
9394
LintId::of(infinite_iter::INFINITE_ITER),
9495
LintId::of(inherent_to_string::INHERENT_TO_STRING),

clippy_lints/src/lib.register_pedantic.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ store.register_group(true, "clippy::pedantic", Some("clippy_pedantic"), vec![
3333
LintId::of(functions::TOO_MANY_LINES),
3434
LintId::of(if_not_else::IF_NOT_ELSE),
3535
LintId::of(implicit_hasher::IMPLICIT_HASHER),
36-
LintId::of(implicit_saturating_sub::IMPLICIT_SATURATING_SUB),
3736
LintId::of(inconsistent_struct_constructor::INCONSISTENT_STRUCT_CONSTRUCTOR),
3837
LintId::of(infinite_iter::MAYBE_INFINITE_ITER),
3938
LintId::of(invalid_upcast_comparisons::INVALID_UPCAST_COMPARISONS),

clippy_lints/src/lib.register_style.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ store.register_group(true, "clippy::style", Some("clippy_style"), vec![
3232
LintId::of(functions::MUST_USE_UNIT),
3333
LintId::of(functions::RESULT_UNIT_ERR),
3434
LintId::of(implicit_saturating_add::IMPLICIT_SATURATING_ADD),
35+
LintId::of(implicit_saturating_sub::IMPLICIT_SATURATING_SUB),
3536
LintId::of(inherent_to_string::INHERENT_TO_STRING),
3637
LintId::of(init_numbered_fields::INIT_NUMBERED_FIELDS),
3738
LintId::of(len_zero::COMPARISON_TO_EMPTY),

tests/ui/implicit_saturating_sub.fixed

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@
22
#![allow(unused_assignments, unused_mut, clippy::assign_op_pattern)]
33
#![warn(clippy::implicit_saturating_sub)]
44

5+
use std::cmp::PartialEq;
6+
use std::ops::SubAssign;
7+
// Mock type
8+
struct Mock;
9+
10+
impl PartialEq<u32> for Mock {
11+
fn eq(&self, _: &u32) -> bool {
12+
true
13+
}
14+
}
15+
16+
impl SubAssign<u32> for Mock {
17+
fn sub_assign(&mut self, _: u32) {}
18+
}
19+
520
fn main() {
621
// Tests for unsigned integers
722

@@ -165,4 +180,39 @@ fn main() {
165180
} else {
166181
println!("side effect");
167182
}
183+
184+
// Extended tests
185+
let mut m = Mock;
186+
let mut u_32 = 3000;
187+
let a = 200;
188+
let mut _b = 8;
189+
190+
if m != 0 {
191+
m -= 1;
192+
}
193+
194+
if a > 0 {
195+
_b -= 1;
196+
}
197+
198+
if 0 > a {
199+
_b -= 1;
200+
}
201+
202+
if u_32 > 0 {
203+
u_32 -= 1;
204+
} else {
205+
println!("don't lint this");
206+
}
207+
208+
if u_32 > 0 {
209+
println!("don't lint this");
210+
u_32 -= 1;
211+
}
212+
213+
if u_32 > 42 {
214+
println!("brace yourself!");
215+
} else if u_32 > 0 {
216+
u_32 -= 1;
217+
}
168218
}

tests/ui/implicit_saturating_sub.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@
22
#![allow(unused_assignments, unused_mut, clippy::assign_op_pattern)]
33
#![warn(clippy::implicit_saturating_sub)]
44

5+
use std::cmp::PartialEq;
6+
use std::ops::SubAssign;
7+
// Mock type
8+
struct Mock;
9+
10+
impl PartialEq<u32> for Mock {
11+
fn eq(&self, _: &u32) -> bool {
12+
true
13+
}
14+
}
15+
16+
impl SubAssign<u32> for Mock {
17+
fn sub_assign(&mut self, _: u32) {}
18+
}
19+
520
fn main() {
621
// Tests for unsigned integers
722

@@ -211,4 +226,39 @@ fn main() {
211226
} else {
212227
println!("side effect");
213228
}
229+
230+
// Extended tests
231+
let mut m = Mock;
232+
let mut u_32 = 3000;
233+
let a = 200;
234+
let mut _b = 8;
235+
236+
if m != 0 {
237+
m -= 1;
238+
}
239+
240+
if a > 0 {
241+
_b -= 1;
242+
}
243+
244+
if 0 > a {
245+
_b -= 1;
246+
}
247+
248+
if u_32 > 0 {
249+
u_32 -= 1;
250+
} else {
251+
println!("don't lint this");
252+
}
253+
254+
if u_32 > 0 {
255+
println!("don't lint this");
256+
u_32 -= 1;
257+
}
258+
259+
if u_32 > 42 {
260+
println!("brace yourself!");
261+
} else if u_32 > 0 {
262+
u_32 -= 1;
263+
}
214264
}

tests/ui/implicit_saturating_sub.stderr

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: implicitly performing saturating subtraction
2-
--> $DIR/implicit_saturating_sub.rs:13:5
2+
--> $DIR/implicit_saturating_sub.rs:28:5
33
|
44
LL | / if u_8 > 0 {
55
LL | | u_8 = u_8 - 1;
@@ -9,175 +9,175 @@ LL | | }
99
= note: `-D clippy::implicit-saturating-sub` implied by `-D warnings`
1010

1111
error: implicitly performing saturating subtraction
12-
--> $DIR/implicit_saturating_sub.rs:20:13
12+
--> $DIR/implicit_saturating_sub.rs:35:13
1313
|
1414
LL | / if u_8 > 0 {
1515
LL | | u_8 -= 1;
1616
LL | | }
1717
| |_____________^ help: try: `u_8 = u_8.saturating_sub(1);`
1818

1919
error: implicitly performing saturating subtraction
20-
--> $DIR/implicit_saturating_sub.rs:34:5
20+
--> $DIR/implicit_saturating_sub.rs:49:5
2121
|
2222
LL | / if u_16 > 0 {
2323
LL | | u_16 -= 1;
2424
LL | | }
2525
| |_____^ help: try: `u_16 = u_16.saturating_sub(1);`
2626

2727
error: implicitly performing saturating subtraction
28-
--> $DIR/implicit_saturating_sub.rs:44:5
28+
--> $DIR/implicit_saturating_sub.rs:59:5
2929
|
3030
LL | / if u_32 != 0 {
3131
LL | | u_32 -= 1;
3232
LL | | }
3333
| |_____^ help: try: `u_32 = u_32.saturating_sub(1);`
3434

3535
error: implicitly performing saturating subtraction
36-
--> $DIR/implicit_saturating_sub.rs:65:5
36+
--> $DIR/implicit_saturating_sub.rs:80:5
3737
|
3838
LL | / if u_64 > 0 {
3939
LL | | u_64 -= 1;
4040
LL | | }
4141
| |_____^ help: try: `u_64 = u_64.saturating_sub(1);`
4242

4343
error: implicitly performing saturating subtraction
44-
--> $DIR/implicit_saturating_sub.rs:70:5
44+
--> $DIR/implicit_saturating_sub.rs:85:5
4545
|
4646
LL | / if 0 < u_64 {
4747
LL | | u_64 -= 1;
4848
LL | | }
4949
| |_____^ help: try: `u_64 = u_64.saturating_sub(1);`
5050

5151
error: implicitly performing saturating subtraction
52-
--> $DIR/implicit_saturating_sub.rs:75:5
52+
--> $DIR/implicit_saturating_sub.rs:90:5
5353
|
5454
LL | / if 0 != u_64 {
5555
LL | | u_64 -= 1;
5656
LL | | }
5757
| |_____^ help: try: `u_64 = u_64.saturating_sub(1);`
5858

5959
error: implicitly performing saturating subtraction
60-
--> $DIR/implicit_saturating_sub.rs:96:5
60+
--> $DIR/implicit_saturating_sub.rs:111:5
6161
|
6262
LL | / if u_usize > 0 {
6363
LL | | u_usize -= 1;
6464
LL | | }
6565
| |_____^ help: try: `u_usize = u_usize.saturating_sub(1);`
6666

6767
error: implicitly performing saturating subtraction
68-
--> $DIR/implicit_saturating_sub.rs:108:5
68+
--> $DIR/implicit_saturating_sub.rs:123:5
6969
|
7070
LL | / if i_8 > i8::MIN {
7171
LL | | i_8 -= 1;
7272
LL | | }
7373
| |_____^ help: try: `i_8 = i_8.saturating_sub(1);`
7474

7575
error: implicitly performing saturating subtraction
76-
--> $DIR/implicit_saturating_sub.rs:113:5
76+
--> $DIR/implicit_saturating_sub.rs:128:5
7777
|
7878
LL | / if i_8 > i8::MIN {
7979
LL | | i_8 -= 1;
8080
LL | | }
8181
| |_____^ help: try: `i_8 = i_8.saturating_sub(1);`
8282

8383
error: implicitly performing saturating subtraction
84-
--> $DIR/implicit_saturating_sub.rs:118:5
84+
--> $DIR/implicit_saturating_sub.rs:133:5
8585
|
8686
LL | / if i_8 != i8::MIN {
8787
LL | | i_8 -= 1;
8888
LL | | }
8989
| |_____^ help: try: `i_8 = i_8.saturating_sub(1);`
9090

9191
error: implicitly performing saturating subtraction
92-
--> $DIR/implicit_saturating_sub.rs:123:5
92+
--> $DIR/implicit_saturating_sub.rs:138:5
9393
|
9494
LL | / if i_8 != i8::MIN {
9595
LL | | i_8 -= 1;
9696
LL | | }
9797
| |_____^ help: try: `i_8 = i_8.saturating_sub(1);`
9898

9999
error: implicitly performing saturating subtraction
100-
--> $DIR/implicit_saturating_sub.rs:133:5
100+
--> $DIR/implicit_saturating_sub.rs:148:5
101101
|
102102
LL | / if i_16 > i16::MIN {
103103
LL | | i_16 -= 1;
104104
LL | | }
105105
| |_____^ help: try: `i_16 = i_16.saturating_sub(1);`
106106

107107
error: implicitly performing saturating subtraction
108-
--> $DIR/implicit_saturating_sub.rs:138:5
108+
--> $DIR/implicit_saturating_sub.rs:153:5
109109
|
110110
LL | / if i_16 > i16::MIN {
111111
LL | | i_16 -= 1;
112112
LL | | }
113113
| |_____^ help: try: `i_16 = i_16.saturating_sub(1);`
114114

115115
error: implicitly performing saturating subtraction
116-
--> $DIR/implicit_saturating_sub.rs:143:5
116+
--> $DIR/implicit_saturating_sub.rs:158:5
117117
|
118118
LL | / if i_16 != i16::MIN {
119119
LL | | i_16 -= 1;
120120
LL | | }
121121
| |_____^ help: try: `i_16 = i_16.saturating_sub(1);`
122122

123123
error: implicitly performing saturating subtraction
124-
--> $DIR/implicit_saturating_sub.rs:148:5
124+
--> $DIR/implicit_saturating_sub.rs:163:5
125125
|
126126
LL | / if i_16 != i16::MIN {
127127
LL | | i_16 -= 1;
128128
LL | | }
129129
| |_____^ help: try: `i_16 = i_16.saturating_sub(1);`
130130

131131
error: implicitly performing saturating subtraction
132-
--> $DIR/implicit_saturating_sub.rs:158:5
132+
--> $DIR/implicit_saturating_sub.rs:173:5
133133
|
134134
LL | / if i_32 > i32::MIN {
135135
LL | | i_32 -= 1;
136136
LL | | }
137137
| |_____^ help: try: `i_32 = i_32.saturating_sub(1);`
138138

139139
error: implicitly performing saturating subtraction
140-
--> $DIR/implicit_saturating_sub.rs:163:5
140+
--> $DIR/implicit_saturating_sub.rs:178:5
141141
|
142142
LL | / if i_32 > i32::MIN {
143143
LL | | i_32 -= 1;
144144
LL | | }
145145
| |_____^ help: try: `i_32 = i_32.saturating_sub(1);`
146146

147147
error: implicitly performing saturating subtraction
148-
--> $DIR/implicit_saturating_sub.rs:168:5
148+
--> $DIR/implicit_saturating_sub.rs:183:5
149149
|
150150
LL | / if i_32 != i32::MIN {
151151
LL | | i_32 -= 1;
152152
LL | | }
153153
| |_____^ help: try: `i_32 = i_32.saturating_sub(1);`
154154

155155
error: implicitly performing saturating subtraction
156-
--> $DIR/implicit_saturating_sub.rs:173:5
156+
--> $DIR/implicit_saturating_sub.rs:188:5
157157
|
158158
LL | / if i_32 != i32::MIN {
159159
LL | | i_32 -= 1;
160160
LL | | }
161161
| |_____^ help: try: `i_32 = i_32.saturating_sub(1);`
162162

163163
error: implicitly performing saturating subtraction
164-
--> $DIR/implicit_saturating_sub.rs:183:5
164+
--> $DIR/implicit_saturating_sub.rs:198:5
165165
|
166166
LL | / if i64::MIN < i_64 {
167167
LL | | i_64 -= 1;
168168
LL | | }
169169
| |_____^ help: try: `i_64 = i_64.saturating_sub(1);`
170170

171171
error: implicitly performing saturating subtraction
172-
--> $DIR/implicit_saturating_sub.rs:188:5
172+
--> $DIR/implicit_saturating_sub.rs:203:5
173173
|
174174
LL | / if i64::MIN != i_64 {
175175
LL | | i_64 -= 1;
176176
LL | | }
177177
| |_____^ help: try: `i_64 = i_64.saturating_sub(1);`
178178

179179
error: implicitly performing saturating subtraction
180-
--> $DIR/implicit_saturating_sub.rs:193:5
180+
--> $DIR/implicit_saturating_sub.rs:208:5
181181
|
182182
LL | / if i64::MIN < i_64 {
183183
LL | | i_64 -= 1;

0 commit comments

Comments
 (0)