Rules / TAU-N002

No lossy as casts

decided by Clippy kernel deny core deny edge warn
Applies to tests: no Exceptions: #[expect] + reason

Why

as silently truncates, wraps and changes sign.

Fix

Use From/TryFrom (u32::try_from(x)?) so out-of-range values become errors. In a kernel crate cargo tauten obligations lists it, but a recorded proof that the function never fails does not discharge it: an as cast never fails, it changes the value. Only a lossless or checked conversion does.

What it checks

Lints: clippy::as_conversions, clippy::cast_possible_truncation, clippy::cast_sign_loss, clippy::cast_possible_wrap

Seen in fixtures

rej-lossy-cast/src/lib.rs core

//! Lossy cast. /// Narrow a length.#[must_use]pub const fn narrow(x: u64) -> u32 {    x as u32}

$ cargo tauten check

error[TAU-N002]: casting `u64` to `u32` may truncate the value  --> rej-lossy-cast/src/lib.rs:6:5 (core crate `rej-lossy-cast`)  = rule: No lossy `as` casts (decided by Clippy)  = fix: Use `From`/`TryFrom` (`u32::try_from(x)?`) so out-of-range values become errors. In a kernel crate `cargo tauten obligations` lists it, but a recorded proof that the function never fails does not discharge it: an `as` cast never fails, it changes the value. Only a lossless or checked conversion does. error[TAU-N002]: using a potentially dangerous silent `as` conversion  --> rej-lossy-cast/src/lib.rs:6:5 (core crate `rej-lossy-cast`)  = rule: No lossy `as` casts (decided by Clippy)  = fix: Use `From`/`TryFrom` (`u32::try_from(x)?`) so out-of-range values become errors. In a kernel crate `cargo tauten obligations` lists it, but a recorded proof that the function never fails does not discharge it: an `as` cast never fails, it changes the value. Only a lossless or checked conversion does.
Fixture rej-lossy-cast (core) expects TAU-N002. Output captured at 0200db3.

In the terminal

cargo tauten explain TAU-N002

$ cargo tauten explain TAU-N002

TAU-N002: No lossy `as` casts Why: `as` silently truncates, wraps and changes sign. Fix: Use `From`/`TryFrom` (`u32::try_from(x)?`) so out-of-range values become errors. In a kernel crate `cargo tauten obligations` lists it, but a recorded proof that the function never fails does not discharge it: an `as` cast never fails, it changes the value. Only a lossless or checked conversion does. Decided by: ClippyDecides: No `as` conversion that can lose information in linted code.Sees: resolved; complete: yesDoes not see: Code expanded from macros defined in other crates (clippy skips it); code disabled by `cfg`; build scripts.Applies to tests: noExceptions allowed: with #[expect(.., reason)]  kernel deny  core deny  edge warnLints: clippy::as_conversions, clippy::cast_possible_truncation, clippy::cast_sign_loss, clippy::cast_possible_wrap