Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ztlpn committed Sep 22, 2019
1 parent 3f85ff4 commit c60c0cb
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/test/ui/try-block/try-block-bad-type.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ LL | let res: Result<i32, i32> = try { };
found type `()`

error[E0277]: the trait bound `(): std::ops::Try` is not satisfied
--> $DIR/try-block-bad-type.rs:17:23
--> $DIR/try-block-bad-type.rs:17:25
|
LL | let res: () = try { };
| ^^^ the trait `std::ops::Try` is not implemented for `()`
| ^ the trait `std::ops::Try` is not implemented for `()`
|
= note: required by `std::ops::Try::from_ok`

error[E0277]: the trait bound `i32: std::ops::Try` is not satisfied
--> $DIR/try-block-bad-type.rs:19:24
--> $DIR/try-block-bad-type.rs:19:26
|
LL | let res: i32 = try { 5 };
| ^^^^^ the trait `std::ops::Try` is not implemented for `i32`
| ^ the trait `std::ops::Try` is not implemented for `i32`
|
= note: required by `std::ops::Try::from_ok`

Expand Down
76 changes: 76 additions & 0 deletions src/test/ui/try-block/try-block-unreachable-code-lint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Test unreachable_code lint for `try {}` block ok-wrapping. See issues #54165, #63324.

// compile-flags: --edition 2018
// check-pass
#![feature(try_blocks)]
#![warn(unreachable_code)]

fn err() -> Result<u32, ()> {
Err(())
}

// In the following cases unreachable code is autogenerated and should not be reported.

fn test_ok_wrapped_divergent_expr_1() {
let res: Result<u32, ()> = try {
loop {
err()?;
}
};
println!("res: {:?}", res);
}

fn test_ok_wrapped_divergent_expr_2() {
let _: Result<u32, ()> = try {
return
};
}

fn test_autogenerated_unit_after_divergent_expr() {
let _: Result<(), ()> = try {
return;
};
}

// In the following cases unreachable code should be reported.

fn test_try_block_after_divergent_stmt() {
let _: Result<u32, ()> = {
return;

try {
loop {
err()?;
}
}
// ~^^^^^ WARNING unreachable expression
};
}

fn test_wrapped_divergent_expr() {
let _: Result<u32, ()> = {
Err(return)
// ~^ WARNING unreachable call
};
}

fn test_expr_after_divergent_stmt_in_try_block() {
let res: Result<u32, ()> = try {
loop {
err()?;
}

42
// ~^ WARNING unreachable expression
};
println!("res: {:?}", res);
}

fn main() {
test_ok_wrapped_divergent_expr_1();
test_ok_wrapped_divergent_expr_2();
test_autogenerated_unit_after_divergent_expr();
test_try_block_after_divergent_stmt();
test_wrapped_divergent_expr();
test_expr_after_divergent_stmt_in_try_block();
}
28 changes: 28 additions & 0 deletions src/test/ui/try-block/try-block-unreachable-code-lint.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
warning: unreachable expression
--> $DIR/try-block-unreachable-code-lint.rs:41:9
|
LL | / try {
LL | | loop {
LL | | err()?;
LL | | }
LL | | }
| |_________^
|
note: lint level defined here
--> $DIR/try-block-unreachable-code-lint.rs:6:9
|
LL | #![warn(unreachable_code)]
| ^^^^^^^^^^^^^^^^

warning: unreachable call
--> $DIR/try-block-unreachable-code-lint.rs:52:9
|
LL | Err(return)
| ^^^

warning: unreachable expression
--> $DIR/try-block-unreachable-code-lint.rs:63:9
|
LL | 42
| ^^

0 comments on commit c60c0cb

Please sign in to comment.