]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/src/docs/cast_lossless.txt
New upstream version 1.66.0+dfsg1
[rustc.git] / src / tools / clippy / src / docs / cast_lossless.txt
CommitLineData
f2b60f7d
FG
1### What it does
2Checks for casts between numerical types that may
3be replaced by safe conversion functions.
4
5### Why is this bad?
6Rust's `as` keyword will perform many kinds of
7conversions, including silently lossy conversions. Conversion functions such
8as `i32::from` will only perform lossless conversions. Using the conversion
9functions prevents conversions from turning into silent lossy conversions if
10the types of the input expressions ever change, and make it easier for
11people reading the code to know that the conversion is lossless.
12
13### Example
14```
15fn as_u64(x: u8) -> u64 {
16 x as u64
17}
18```
19
20Using `::from` would look like this:
21
22```
23fn as_u64(x: u8) -> u64 {
24 u64::from(x)
25}
26```