Skip to content

Commit

Permalink
test: external crate consts
Browse files Browse the repository at this point in the history
  • Loading branch information
vohoanglong0107 committed Apr 29, 2024
1 parent 4b0eb1a commit 9d33968
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 53 deletions.
27 changes: 27 additions & 0 deletions tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
//@aux-build:../../ui/auxiliary/external_consts.rs

#![allow(unused)]
#![warn(clippy::unnecessary_min_or_max)]
#![allow(clippy::identity_op)]

extern crate external_consts;

const X: i32 = 1;

fn main() {
// Both are Literals
let _ = (-6_i32);
Expand All @@ -9,6 +17,15 @@ fn main() {
let _ = 6;
let _ = 7_u8;

let _ = 12;
let _ = X;
let _ = X;
let _ = 12;
let _ = 12;
let _ = (X + 1);
let _ = X - 1;
let _ = 12;

let x: i32 = 42;
// signed MIN
let _ = i32::MIN;
Expand Down Expand Up @@ -43,6 +60,16 @@ fn main() {

let _ = std::f64::consts::E;

let _ = external_consts::MAGIC_NUMBER;
let _ = 2;
let _ = external_consts::MAGIC_NUMBER;
let _ = 2;

let _ = external_consts::MAGIC_NUMBER;
let _ = external_consts::MAGIC_NUMBER;
let _ = X;
let _ = X;

// The below cases shouldn't be lint
let mut min = u32::MAX;
for _ in 0..1000 {
Expand Down
27 changes: 27 additions & 0 deletions tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
//@aux-build:../../ui/auxiliary/external_consts.rs

#![allow(unused)]
#![warn(clippy::unnecessary_min_or_max)]
#![allow(clippy::identity_op)]

extern crate external_consts;

const X: i32 = 1;

fn main() {
// Both are Literals
let _ = (-6_i32).min(9);
Expand All @@ -9,6 +17,15 @@ fn main() {
let _ = 6.min(7_u8);
let _ = 6.max(7_u8);

let _ = X.max(12);
let _ = X.min(12);
let _ = 12.min(X);
let _ = 12.max(X);
let _ = (X + 1).max(12);
let _ = (X + 1).min(12);
let _ = 12.min(X - 1);
let _ = 12.max(X - 1);

let x: i32 = 42;
// signed MIN
let _ = i32::MIN.min(x);
Expand Down Expand Up @@ -43,6 +60,16 @@ fn main() {

let _ = (6.0_f64).min(std::f64::consts::E);

let _ = 2.min(external_consts::MAGIC_NUMBER);
let _ = 2.max(external_consts::MAGIC_NUMBER);
let _ = external_consts::MAGIC_NUMBER.min(2);
let _ = external_consts::MAGIC_NUMBER.max(2);

let _ = X.min(external_consts::MAGIC_NUMBER);
let _ = X.max(external_consts::MAGIC_NUMBER);
let _ = external_consts::MAGIC_NUMBER.min(X);
let _ = external_consts::MAGIC_NUMBER.max(X);

// The below cases shouldn't be lint
let mut min = u32::MAX;
for _ in 0..1000 {
Expand Down
152 changes: 124 additions & 28 deletions tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: `(-6_i32)` is never greater than `9` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:5:13
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:13:13
|
LL | let _ = (-6_i32).min(9);
| ^^^^^^^^^^^^^^^ help: try: `(-6_i32)`
Expand All @@ -8,160 +8,256 @@ LL | let _ = (-6_i32).min(9);
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_min_or_max)]`

error: `(-6_i32)` is never greater than `9` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:6:13
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:14:13
|
LL | let _ = (-6_i32).max(9);
| ^^^^^^^^^^^^^^^ help: try: `9`

error: `9_u32` is never smaller than `6` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:7:13
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:15:13
|
LL | let _ = 9_u32.min(6);
| ^^^^^^^^^^^^ help: try: `6`

error: `9_u32` is never smaller than `6` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:8:13
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:16:13
|
LL | let _ = 9_u32.max(6);
| ^^^^^^^^^^^^ help: try: `9_u32`

error: `6` is never greater than `7_u8` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:9:13
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:17:13
|
LL | let _ = 6.min(7_u8);
| ^^^^^^^^^^^ help: try: `6`

error: `6` is never greater than `7_u8` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:10:13
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:18:13
|
LL | let _ = 6.max(7_u8);
| ^^^^^^^^^^^ help: try: `7_u8`

error: `X` is never greater than `12` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:20:13
|
LL | let _ = X.max(12);
| ^^^^^^^^^ help: try: `12`

error: `X` is never greater than `12` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:21:13
|
LL | let _ = X.min(12);
| ^^^^^^^^^ help: try: `X`

error: `12` is never smaller than `X` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:22:13
|
LL | let _ = 12.min(X);
| ^^^^^^^^^ help: try: `X`

error: `12` is never smaller than `X` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:23:13
|
LL | let _ = 12.max(X);
| ^^^^^^^^^ help: try: `12`

error: `(X + 1)` is never greater than `12` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:24:13
|
LL | let _ = (X + 1).max(12);
| ^^^^^^^^^^^^^^^ help: try: `12`

error: `(X + 1)` is never greater than `12` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:25:13
|
LL | let _ = (X + 1).min(12);
| ^^^^^^^^^^^^^^^ help: try: `(X + 1)`

error: `12` is never smaller than `X - 1` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:26:13
|
LL | let _ = 12.min(X - 1);
| ^^^^^^^^^^^^^ help: try: `X - 1`

error: `12` is never smaller than `X - 1` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:27:13
|
LL | let _ = 12.max(X - 1);
| ^^^^^^^^^^^^^ help: try: `12`

error: `i32::MIN` is never greater than `x` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:14:13
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:31:13
|
LL | let _ = i32::MIN.min(x);
| ^^^^^^^^^^^^^^^ help: try: `i32::MIN`

error: `i32::MIN` is never greater than `x` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:15:13
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:32:13
|
LL | let _ = i32::MIN.max(x);
| ^^^^^^^^^^^^^^^ help: try: `x`

error: `x` is never smaller than `i32::MIN` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:16:13
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:33:13
|
LL | let _ = x.min(i32::MIN);
| ^^^^^^^^^^^^^^^ help: try: `i32::MIN`

error: `x` is never smaller than `i32::MIN` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:17:13
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:34:13
|
LL | let _ = x.max(i32::MIN);
| ^^^^^^^^^^^^^^^ help: try: `x`

error: `i32::MAX` is never smaller than `x` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:20:13
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:37:13
|
LL | let _ = i32::MAX.min(x);
| ^^^^^^^^^^^^^^^ help: try: `x`

error: `i32::MAX` is never smaller than `x` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:21:13
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:38:13
|
LL | let _ = i32::MAX.max(x);
| ^^^^^^^^^^^^^^^ help: try: `i32::MAX`

error: `x` is never greater than `i32::MAX` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:22:13
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:39:13
|
LL | let _ = x.min(i32::MAX);
| ^^^^^^^^^^^^^^^ help: try: `x`

error: `x` is never greater than `i32::MAX` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:23:13
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:40:13
|
LL | let _ = x.max(i32::MAX);
| ^^^^^^^^^^^^^^^ help: try: `i32::MAX`

error: `u32::MAX` is never smaller than `x` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:27:13
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:44:13
|
LL | let _ = u32::MAX.min(x);
| ^^^^^^^^^^^^^^^ help: try: `x`

error: `u32::MAX` is never smaller than `x` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:28:13
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:45:13
|
LL | let _ = u32::MAX.max(x);
| ^^^^^^^^^^^^^^^ help: try: `u32::MAX`

error: `x` is never greater than `u32::MAX` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:29:13
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:46:13
|
LL | let _ = x.min(u32::MAX);
| ^^^^^^^^^^^^^^^ help: try: `x`

error: `x` is never greater than `u32::MAX` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:30:13
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:47:13
|
LL | let _ = x.max(u32::MAX);
| ^^^^^^^^^^^^^^^ help: try: `u32::MAX`

error: `u32::MIN` is never greater than `x` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:33:13
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:50:13
|
LL | let _ = u32::MIN.min(x);
| ^^^^^^^^^^^^^^^ help: try: `u32::MIN`

error: `u32::MIN` is never greater than `x` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:34:13
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:51:13
|
LL | let _ = u32::MIN.max(x);
| ^^^^^^^^^^^^^^^ help: try: `x`

error: `x` is never smaller than `u32::MIN` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:35:13
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:52:13
|
LL | let _ = x.min(u32::MIN);
| ^^^^^^^^^^^^^^^ help: try: `u32::MIN`

error: `x` is never smaller than `u32::MIN` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:36:13
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:53:13
|
LL | let _ = x.max(u32::MIN);
| ^^^^^^^^^^^^^^^ help: try: `x`

error: `0` is never greater than `x` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:39:13
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:56:13
|
LL | let _ = 0.min(x);
| ^^^^^^^^ help: try: `0`

error: `0` is never greater than `x` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:40:13
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:57:13
|
LL | let _ = 0.max(x);
| ^^^^^^^^ help: try: `x`

error: `x` is never smaller than `0_u32` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:41:13
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:58:13
|
LL | let _ = x.min(0_u32);
| ^^^^^^^^^^^^ help: try: `0_u32`

error: `x` is never smaller than `0_u32` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:42:13
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:59:13
|
LL | let _ = x.max(0_u32);
| ^^^^^^^^^^^^ help: try: `x`

error: `(6.0_f64)` is never smaller than `std::f64::consts::E` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:44:13
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:61:13
|
LL | let _ = (6.0_f64).min(std::f64::consts::E);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::f64::consts::E`

error: aborting due to 27 previous errors
error: `2` is never smaller than `external_consts::MAGIC_NUMBER` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:63:13
|
LL | let _ = 2.min(external_consts::MAGIC_NUMBER);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `external_consts::MAGIC_NUMBER`

error: `2` is never smaller than `external_consts::MAGIC_NUMBER` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:64:13
|
LL | let _ = 2.max(external_consts::MAGIC_NUMBER);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `2`

error: `external_consts::MAGIC_NUMBER` is never greater than `2` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:65:13
|
LL | let _ = external_consts::MAGIC_NUMBER.min(2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `external_consts::MAGIC_NUMBER`

error: `external_consts::MAGIC_NUMBER` is never greater than `2` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:66:13
|
LL | let _ = external_consts::MAGIC_NUMBER.max(2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `2`

error: `X` is never smaller than `external_consts::MAGIC_NUMBER` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:68:13
|
LL | let _ = X.min(external_consts::MAGIC_NUMBER);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `external_consts::MAGIC_NUMBER`

error: `X` is never smaller than `external_consts::MAGIC_NUMBER` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:69:13
|
LL | let _ = X.max(external_consts::MAGIC_NUMBER);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `external_consts::MAGIC_NUMBER`

error: `external_consts::MAGIC_NUMBER` is never smaller than `X` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:70:13
|
LL | let _ = external_consts::MAGIC_NUMBER.min(X);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `X`

error: `external_consts::MAGIC_NUMBER` is never smaller than `X` and has therefore no effect
--> tests/ui-toml/unnecessary_min_or_max/unnecessary_min_or_max.rs:71:13
|
LL | let _ = external_consts::MAGIC_NUMBER.max(X);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `X`

error: aborting due to 43 previous errors

1 change: 1 addition & 0 deletions tests/ui/auxiliary/external_consts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub const MAGIC_NUMBER: i32 = 1;

0 comments on commit 9d33968

Please sign in to comment.