E0012

E0012#

Warning name: unreachable_code

Unreachable code. This usually happens when a return statement is followed by more code. The return statement will exit the function immediately, so any code after it will not be executed.

Erroneous example#

///|
/// Example function showing unreachable code after an early return
fn unreachable_early_return() -> Unit {
  return
  println("Hello, World!") // Warning: Unreachable code
}
///|
// Example function showing unreachable pattern matching
fn unreachable_match_pattern() -> Unit {
  match Some(1) {
    _ => println("_")
    Some(a) => println("Some(\{a})") // Warning: Unreachable code
  }
}

Suggestion#

Remove the unreachable code. Or adjust the branching statements.

///|
fn example_execute_before_return() -> Unit {
  println("Hello World!")
  return
}

You can also swap the order of the branches in the pattern matching. If the order of the branches is important, then you may want to refine the first pattern so that it excludes what the second pattern covers.

///|
fn example_proper_order() -> Unit {
  match Some(1) {
    Some(a) => println("Some(\{a})")
    _ => println("_")
  }
}

Or,

///|
fn example_specific_pattern() -> Unit {
  match Some(1) {
    None => println("_")
    Some(a) => println("Some(\{a})")
  }
}