Skip to content

Commit

Permalink
Rollup merge of rust-lang#64229 - kawa-yoiko:unreachable-call-lint, r…
Browse files Browse the repository at this point in the history
…=estebank

Reduce span to function name in unreachable calls

As title suggests, this might close rust-lang#64103. Refer to the updated tests for expected output.

There is potential to further improve usability. In particular, is it favourable that the exact diverging expression/statement be pointed out (not only in this case, but for all unreachable code)? Certainly that would deserve another issue, but I'm interested in the opinions.
  • Loading branch information
Centril authored Sep 7, 2019
2 parents 77e1a7c + e1d27eb commit cab2d84
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 13 deletions.
4 changes: 4 additions & 0 deletions src/librustc_typeck/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Warn for non-block expressions with diverging children.
match expr.node {
ExprKind::Block(..) | ExprKind::Loop(..) | ExprKind::Match(..) => {},
ExprKind::Call(ref callee, _) =>
self.warn_if_unreachable(expr.hir_id, callee.span, "call"),
ExprKind::MethodCall(_, ref span, _) =>
self.warn_if_unreachable(expr.hir_id, *span, "call"),
_ => self.warn_if_unreachable(expr.hir_id, expr.span, "expression"),
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/never-assign-dead-code.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ LL | #![warn(unused)]
| ^^^^^^
= note: `#[warn(unreachable_code)]` implied by `#[warn(unused)]`

warning: unreachable expression
warning: unreachable call
--> $DIR/never-assign-dead-code.rs:10:5
|
LL | drop(x);
| ^^^^^^^
| ^^^^

warning: unused variable: `x`
--> $DIR/never-assign-dead-code.rs:9:9
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/reachable/expr_call.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ note: lint level defined here
LL | #![deny(unreachable_code)]
| ^^^^^^^^^^^^^^^^

error: unreachable expression
error: unreachable call
--> $DIR/expr_call.rs:18:5
|
LL | bar(return);
| ^^^^^^^^^^^
| ^^^

error: aborting due to 2 previous errors

6 changes: 3 additions & 3 deletions src/test/ui/reachable/expr_method.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ note: lint level defined here
LL | #![deny(unreachable_code)]
| ^^^^^^^^^^^^^^^^

error: unreachable expression
--> $DIR/expr_method.rs:21:5
error: unreachable call
--> $DIR/expr_method.rs:21:9
|
LL | Foo.bar(return);
| ^^^^^^^^^^^^^^^
| ^^^

error: aborting due to 2 previous errors

2 changes: 1 addition & 1 deletion src/test/ui/unreachable/unreachable-in-call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn diverge_first() {
get_u8()); //~ ERROR unreachable expression
}
fn diverge_second() {
call( //~ ERROR unreachable expression
call( //~ ERROR unreachable call
get_u8(),
diverge());
}
Expand Down
8 changes: 3 additions & 5 deletions src/test/ui/unreachable/unreachable-in-call.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ note: lint level defined here
LL | #![deny(unreachable_code)]
| ^^^^^^^^^^^^^^^^

error: unreachable expression
error: unreachable call
--> $DIR/unreachable-in-call.rs:17:5
|
LL | / call(
LL | | get_u8(),
LL | | diverge());
| |__________________^
LL | call(
| ^^^^

error: aborting due to 2 previous errors

0 comments on commit cab2d84

Please sign in to comment.