Skip to content

Commit

Permalink
Update Clippy testcases
Browse files Browse the repository at this point in the history
Update the test `redundant_pattern_matching`: check if `is_some` and `is_none` are suggested within const contexts.
  • Loading branch information
CDirkx committed Sep 20, 2020
1 parent a334ae6 commit ed43385
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 65 deletions.
39 changes: 14 additions & 25 deletions tests/ui/redundant_pattern_matching.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ fn main() {
takes_bool(x);

issue5504();
issue5697();
issue6067();

let _ = if gen_opt().is_some() {
Expand Down Expand Up @@ -129,41 +128,31 @@ fn issue5504() {
while m!().is_some() {}
}

// None of these should be linted because none of the suggested methods
// are `const fn` without toggling a feature.
const fn issue5697() {
if let Some(_) = Some(42) {}

if let None = None::<()> {}

while let Some(_) = Some(42) {}

while let None = None::<()> {}

match Some(42) {
Some(_) => true,
None => false,
};

match None::<()> {
Some(_) => false,
None => true,
};
}

// Methods that are unstable const should not be suggested within a const context, see issue #5697.
// However, in Rust 1.48.0 the methods `is_ok` and `is_err` of `Result` were stabilized as const,
// so the following should be linted.
// However, in Rust 1.48.0 the methods `is_ok` and `is_err` of `Result`, and `is_some` and `is_none`
// of `Option` were stabilized as const, so the following should be linted.
const fn issue6067() {
if Ok::<i32, i32>(42).is_ok() {}

if Err::<i32, i32>(42).is_err() {}

if Some(42).is_some() {}

if None::<()>.is_none() {}

while Ok::<i32, i32>(10).is_ok() {}

while Ok::<i32, i32>(10).is_err() {}

while Some(42).is_some() {}

while None::<()>.is_none() {}

Ok::<i32, i32>(42).is_ok();

Err::<i32, i32>(42).is_err();

Some(42).is_some();

None::<()>.is_none();
}
45 changes: 20 additions & 25 deletions tests/ui/redundant_pattern_matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ fn main() {
takes_bool(x);

issue5504();
issue5697();
issue6067();

let _ = if let Some(_) = gen_opt() {
Expand Down Expand Up @@ -150,40 +149,26 @@ fn issue5504() {
while let Some(_) = m!() {}
}

// None of these should be linted because none of the suggested methods
// are `const fn` without toggling a feature.
const fn issue5697() {
if let Some(_) = Some(42) {}

if let None = None::<()> {}

while let Some(_) = Some(42) {}

while let None = None::<()> {}

match Some(42) {
Some(_) => true,
None => false,
};

match None::<()> {
Some(_) => false,
None => true,
};
}

// Methods that are unstable const should not be suggested within a const context, see issue #5697.
// However, in Rust 1.48.0 the methods `is_ok` and `is_err` of `Result` were stabilized as const,
// so the following should be linted.
// However, in Rust 1.48.0 the methods `is_ok` and `is_err` of `Result`, and `is_some` and `is_none`
// of `Option` were stabilized as const, so the following should be linted.
const fn issue6067() {
if let Ok(_) = Ok::<i32, i32>(42) {}

if let Err(_) = Err::<i32, i32>(42) {}

if let Some(_) = Some(42) {}

if let None = None::<()> {}

while let Ok(_) = Ok::<i32, i32>(10) {}

while let Err(_) = Ok::<i32, i32>(10) {}

while let Some(_) = Some(42) {}

while let None = None::<()> {}

match Ok::<i32, i32>(42) {
Ok(_) => true,
Err(_) => false,
Expand All @@ -193,4 +178,14 @@ const fn issue6067() {
Ok(_) => false,
Err(_) => true,
};

match Some(42) {
Some(_) => true,
None => false,
};

match None::<()> {
Some(_) => false,
None => true,
};
}
72 changes: 57 additions & 15 deletions tests/ui/redundant_pattern_matching.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -149,79 +149,103 @@ LL | let x = if let Some(_) = opt { true } else { false };
| -------^^^^^^^------ help: try this: `if opt.is_some()`

error: redundant pattern matching, consider using `is_some()`
--> $DIR/redundant_pattern_matching.rs:103:20
--> $DIR/redundant_pattern_matching.rs:102:20
|
LL | let _ = if let Some(_) = gen_opt() {
| -------^^^^^^^------------ help: try this: `if gen_opt().is_some()`

error: redundant pattern matching, consider using `is_none()`
--> $DIR/redundant_pattern_matching.rs:105:19
--> $DIR/redundant_pattern_matching.rs:104:19
|
LL | } else if let None = gen_opt() {
| -------^^^^------------ help: try this: `if gen_opt().is_none()`

error: redundant pattern matching, consider using `is_ok()`
--> $DIR/redundant_pattern_matching.rs:107:19
--> $DIR/redundant_pattern_matching.rs:106:19
|
LL | } else if let Ok(_) = gen_res() {
| -------^^^^^------------ help: try this: `if gen_res().is_ok()`

error: redundant pattern matching, consider using `is_err()`
--> $DIR/redundant_pattern_matching.rs:109:19
--> $DIR/redundant_pattern_matching.rs:108:19
|
LL | } else if let Err(_) = gen_res() {
| -------^^^^^^------------ help: try this: `if gen_res().is_err()`

error: redundant pattern matching, consider using `is_some()`
--> $DIR/redundant_pattern_matching.rs:142:19
--> $DIR/redundant_pattern_matching.rs:141:19
|
LL | while let Some(_) = r#try!(result_opt()) {}
| ----------^^^^^^^----------------------- help: try this: `while r#try!(result_opt()).is_some()`

error: redundant pattern matching, consider using `is_some()`
--> $DIR/redundant_pattern_matching.rs:143:16
--> $DIR/redundant_pattern_matching.rs:142:16
|
LL | if let Some(_) = r#try!(result_opt()) {}
| -------^^^^^^^----------------------- help: try this: `if r#try!(result_opt()).is_some()`

error: redundant pattern matching, consider using `is_some()`
--> $DIR/redundant_pattern_matching.rs:149:12
--> $DIR/redundant_pattern_matching.rs:148:12
|
LL | if let Some(_) = m!() {}
| -------^^^^^^^------- help: try this: `if m!().is_some()`

error: redundant pattern matching, consider using `is_some()`
--> $DIR/redundant_pattern_matching.rs:150:15
--> $DIR/redundant_pattern_matching.rs:149:15
|
LL | while let Some(_) = m!() {}
| ----------^^^^^^^------- help: try this: `while m!().is_some()`

error: redundant pattern matching, consider using `is_ok()`
--> $DIR/redundant_pattern_matching.rs:179:12
--> $DIR/redundant_pattern_matching.rs:156:12
|
LL | if let Ok(_) = Ok::<i32, i32>(42) {}
| -------^^^^^--------------------- help: try this: `if Ok::<i32, i32>(42).is_ok()`

error: redundant pattern matching, consider using `is_err()`
--> $DIR/redundant_pattern_matching.rs:181:12
--> $DIR/redundant_pattern_matching.rs:158:12
|
LL | if let Err(_) = Err::<i32, i32>(42) {}
| -------^^^^^^---------------------- help: try this: `if Err::<i32, i32>(42).is_err()`

error: redundant pattern matching, consider using `is_some()`
--> $DIR/redundant_pattern_matching.rs:160:12
|
LL | if let Some(_) = Some(42) {}
| -------^^^^^^^----------- help: try this: `if Some(42).is_some()`

error: redundant pattern matching, consider using `is_none()`
--> $DIR/redundant_pattern_matching.rs:162:12
|
LL | if let None = None::<()> {}
| -------^^^^------------- help: try this: `if None::<()>.is_none()`

error: redundant pattern matching, consider using `is_ok()`
--> $DIR/redundant_pattern_matching.rs:183:15
--> $DIR/redundant_pattern_matching.rs:164:15
|
LL | while let Ok(_) = Ok::<i32, i32>(10) {}
| ----------^^^^^--------------------- help: try this: `while Ok::<i32, i32>(10).is_ok()`

error: redundant pattern matching, consider using `is_err()`
--> $DIR/redundant_pattern_matching.rs:185:15
--> $DIR/redundant_pattern_matching.rs:166:15
|
LL | while let Err(_) = Ok::<i32, i32>(10) {}
| ----------^^^^^^--------------------- help: try this: `while Ok::<i32, i32>(10).is_err()`

error: redundant pattern matching, consider using `is_some()`
--> $DIR/redundant_pattern_matching.rs:168:15
|
LL | while let Some(_) = Some(42) {}
| ----------^^^^^^^----------- help: try this: `while Some(42).is_some()`

error: redundant pattern matching, consider using `is_none()`
--> $DIR/redundant_pattern_matching.rs:170:15
|
LL | while let None = None::<()> {}
| ----------^^^^------------- help: try this: `while None::<()>.is_none()`

error: redundant pattern matching, consider using `is_ok()`
--> $DIR/redundant_pattern_matching.rs:187:5
--> $DIR/redundant_pattern_matching.rs:172:5
|
LL | / match Ok::<i32, i32>(42) {
LL | | Ok(_) => true,
Expand All @@ -230,13 +254,31 @@ LL | | };
| |_____^ help: try this: `Ok::<i32, i32>(42).is_ok()`

error: redundant pattern matching, consider using `is_err()`
--> $DIR/redundant_pattern_matching.rs:192:5
--> $DIR/redundant_pattern_matching.rs:177:5
|
LL | / match Err::<i32, i32>(42) {
LL | | Ok(_) => false,
LL | | Err(_) => true,
LL | | };
| |_____^ help: try this: `Err::<i32, i32>(42).is_err()`

error: aborting due to 35 previous errors
error: redundant pattern matching, consider using `is_some()`
--> $DIR/redundant_pattern_matching.rs:182:5
|
LL | / match Some(42) {
LL | | Some(_) => true,
LL | | None => false,
LL | | };
| |_____^ help: try this: `Some(42).is_some()`

error: redundant pattern matching, consider using `is_none()`
--> $DIR/redundant_pattern_matching.rs:187:5
|
LL | / match None::<()> {
LL | | Some(_) => false,
LL | | None => true,
LL | | };
| |_____^ help: try this: `None::<()>.is_none()`

error: aborting due to 41 previous errors

0 comments on commit ed43385

Please sign in to comment.