-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ui regression tests for
implicit_saturation_sub
lint extension
- Loading branch information
1 parent
6984930
commit ef6144b
Showing
6 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//@no-rustfix | ||
#![warn(clippy::implicit_saturating_sub)] | ||
#![allow(arithmetic_overflow)] | ||
|
||
fn main() { | ||
let a = 12u32; | ||
let b = 13u32; | ||
|
||
let result = if a > b { b - a } else { 0 }; | ||
//~^ ERROR: inverted arithmetic check before subtraction | ||
let result = if b < a { b - a } else { 0 }; | ||
//~^ ERROR: inverted arithmetic check before subtraction | ||
|
||
let result = if a > b { 0 } else { a - b }; | ||
//~^ ERROR: inverted arithmetic check before subtraction | ||
let result = if a >= b { 0 } else { a - b }; | ||
//~^ ERROR: inverted arithmetic check before subtraction | ||
let result = if b < a { 0 } else { a - b }; | ||
//~^ ERROR: inverted arithmetic check before subtraction | ||
let result = if b <= a { 0 } else { a - b }; | ||
//~^ ERROR: inverted arithmetic check before subtraction | ||
|
||
let af = 12f32; | ||
let bf = 13f32; | ||
// Should not lint! | ||
let result = if bf < af { 0. } else { af - bf }; | ||
|
||
// Should not lint! | ||
let result = if a < b { | ||
println!("we shouldn't remove this"); | ||
0 | ||
} else { | ||
a - b | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
error: inverted arithmetic check before subtraction | ||
--> tests/ui/manual_arithmetic_check-2.rs:9:23 | ||
| | ||
LL | let result = if a > b { b - a } else { 0 }; | ||
| ^ ----- help: try replacing it with: `a - b` | ||
| | ||
note: this subtraction underflows when `b < a` | ||
--> tests/ui/manual_arithmetic_check-2.rs:9:29 | ||
| | ||
LL | let result = if a > b { b - a } else { 0 }; | ||
| ^^^^^ | ||
= note: `-D clippy::implicit-saturating-sub` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::implicit_saturating_sub)]` | ||
|
||
error: inverted arithmetic check before subtraction | ||
--> tests/ui/manual_arithmetic_check-2.rs:11:23 | ||
| | ||
LL | let result = if b < a { b - a } else { 0 }; | ||
| ^ ----- help: try replacing it with: `a - b` | ||
| | ||
note: this subtraction underflows when `b < a` | ||
--> tests/ui/manual_arithmetic_check-2.rs:11:29 | ||
| | ||
LL | let result = if b < a { b - a } else { 0 }; | ||
| ^^^^^ | ||
|
||
error: inverted arithmetic check before subtraction | ||
--> tests/ui/manual_arithmetic_check-2.rs:14:23 | ||
| | ||
LL | let result = if a > b { 0 } else { a - b }; | ||
| ^ ----- help: try replacing it with: `b - a` | ||
| | ||
note: this subtraction underflows when `a < b` | ||
--> tests/ui/manual_arithmetic_check-2.rs:14:40 | ||
| | ||
LL | let result = if a > b { 0 } else { a - b }; | ||
| ^^^^^ | ||
|
||
error: inverted arithmetic check before subtraction | ||
--> tests/ui/manual_arithmetic_check-2.rs:16:23 | ||
| | ||
LL | let result = if a >= b { 0 } else { a - b }; | ||
| ^^ ----- help: try replacing it with: `b - a` | ||
| | ||
note: this subtraction underflows when `a < b` | ||
--> tests/ui/manual_arithmetic_check-2.rs:16:41 | ||
| | ||
LL | let result = if a >= b { 0 } else { a - b }; | ||
| ^^^^^ | ||
|
||
error: inverted arithmetic check before subtraction | ||
--> tests/ui/manual_arithmetic_check-2.rs:18:23 | ||
| | ||
LL | let result = if b < a { 0 } else { a - b }; | ||
| ^ ----- help: try replacing it with: `b - a` | ||
| | ||
note: this subtraction underflows when `a < b` | ||
--> tests/ui/manual_arithmetic_check-2.rs:18:40 | ||
| | ||
LL | let result = if b < a { 0 } else { a - b }; | ||
| ^^^^^ | ||
|
||
error: inverted arithmetic check before subtraction | ||
--> tests/ui/manual_arithmetic_check-2.rs:20:23 | ||
| | ||
LL | let result = if b <= a { 0 } else { a - b }; | ||
| ^^ ----- help: try replacing it with: `b - a` | ||
| | ||
note: this subtraction underflows when `a < b` | ||
--> tests/ui/manual_arithmetic_check-2.rs:20:41 | ||
| | ||
LL | let result = if b <= a { 0 } else { a - b }; | ||
| ^^^^^ | ||
|
||
error: aborting due to 6 previous errors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#![warn(clippy::implicit_saturating_sub)] | ||
|
||
fn main() { | ||
let a = 12u32; | ||
let b = 13u32; | ||
|
||
let result = a.saturating_sub(b); | ||
//~^ ERROR: manual arithmetic check found | ||
let result = a.saturating_sub(b); | ||
//~^ ERROR: manual arithmetic check found | ||
|
||
let result = a.saturating_sub(b); | ||
//~^ ERROR: manual arithmetic check found | ||
let result = a.saturating_sub(b); | ||
//~^ ERROR: manual arithmetic check found | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#![warn(clippy::implicit_saturating_sub)] | ||
|
||
fn main() { | ||
let a = 12u32; | ||
let b = 13u32; | ||
let c = 8u32; | ||
|
||
let result = if a > b { a - b } else { 0 }; | ||
//~^ ERROR: manual arithmetic check found | ||
let result = if b < a { a - b } else { 0 }; | ||
//~^ ERROR: manual arithmetic check found | ||
|
||
let result = if a < b { 0 } else { a - b }; | ||
//~^ ERROR: manual arithmetic check found | ||
let result = if b > a { 0 } else { a - b }; | ||
//~^ ERROR: manual arithmetic check found | ||
|
||
// Should not warn! | ||
let result = if a > b { a - b } else { a - c }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
error: manual arithmetic check found | ||
--> tests/ui/manual_arithmetic_check.rs:7:18 | ||
| | ||
LL | let result = if a > b { a - b } else { 0 }; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `a.saturating_sub(b)` | ||
| | ||
= note: `-D clippy::implicit-saturating-sub` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::implicit_saturating_sub)]` | ||
|
||
error: manual arithmetic check found | ||
--> tests/ui/manual_arithmetic_check.rs:9:18 | ||
| | ||
LL | let result = if b < a { a - b } else { 0 }; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `a.saturating_sub(b)` | ||
|
||
error: manual arithmetic check found | ||
--> tests/ui/manual_arithmetic_check.rs:12:18 | ||
| | ||
LL | let result = if a < b { 0 } else { a - b }; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `a.saturating_sub(b)` | ||
|
||
error: manual arithmetic check found | ||
--> tests/ui/manual_arithmetic_check.rs:14:18 | ||
| | ||
LL | let result = if b > a { 0 } else { a - b }; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `a.saturating_sub(b)` | ||
|
||
error: aborting due to 4 previous errors | ||
|