W1008

W1008 — A match arm whose body is empty, such as 1 => {}.

What the compiler reports

Warning[W1008] at {{.*}}warning_for_an_empty_match_arm.vx:30:5: this match arm's body is empty, so this case does nothing

The fragments the test suite holds the compiler to. An ellipsis marks text the test does not constrain; the emitted message also carries a source location.

A program that triggers it

//
// Part of the Vx Project, under the Apache License v2.0 with LLVM Exceptions.
// See LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//
// A `match` arm with a literal pattern and an empty body, such as `2 => {}`, does nothing for
// its case (W1008). An empty arm whose pattern is `_`, an enum variant or a `bool`, such as
// `Color::Green => {}` or `false => {}`, gets no warning: it is there so the match covers every
// case. Nor does an arm with a statement in its body. This warning does not look at a `match` on a tuple
// yet, so the empty arm in `blue_one` gets no warning either.

enum Color {
  Red, Green, Blue,
}

fn count_ones(x : i32) -> i32 {
  let mut n = 0;
  match x {
    1 => {
      n = n + 1;
    }
    2 => {}
    _ => {}
  }
  return n;
}

fn is_red(c : Color) -> i32 {
  let mut r = 0;
  match c {
    Color::Red => {
      r = 1;
    }
    Color::Green => {}
    _ => {}
  }
  return r;
}

fn is_true(b : bool) -> i32 {
  let mut r = 0;
  match b {
    true => {
      r = 1;
    }
    false => {}
  }
  return r;
}

fn blue_one(x : i32, c : Color) -> i32 {
  let mut r = 1;
  match (x, c) {
    (1, Color::Blue) => {}
    _ => {
      r = 0;
    }
  }
  return r;
}

fn main() -> i32 {
  return count_ones(1) + is_red(Color::Red) + is_true(true) + blue_one(1, Color::Blue);
}

From tests/frontend/pass/warning_for_an_empty_match_arm.vx, which asserts this diagnostic on every commit.

Related