W1013

W1013 — An as cast to the type the value already has, such as x as i32 when x is an i32.

What the compiler reports

Warning[W1013] at {{.*}}warning_for_a_cast_to_the_same_type.vx:22:16: this value is already i32, so the cast 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
//
//
// `x as i32` when `x` is already an `i32` does nothing (W1013). A widening cast, a cast of a
// literal, and `v as T` in a generic function used with `T = i32` get no warning.

fn convert<T>(v : i32) -> T {
  return v as T;
}

fn main() -> i32 {
  let x : i32 = 1;
  let same = x as i32;
  let wide = x as i64;
  let one = 1 as i32;
  let minus_one = -1 as i32;
  let y : i32 = convert(x);
  print(wide);
  return same + one + minus_one + y;
}

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

Related