summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/src/docs/cast_lossless.txt
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/clippy/src/docs/cast_lossless.txt')
-rw-r--r--src/tools/clippy/src/docs/cast_lossless.txt26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/tools/clippy/src/docs/cast_lossless.txt b/src/tools/clippy/src/docs/cast_lossless.txt
new file mode 100644
index 000000000..c3a61dd47
--- /dev/null
+++ b/src/tools/clippy/src/docs/cast_lossless.txt
@@ -0,0 +1,26 @@
+### What it does
+Checks for casts between numerical types that may
+be replaced by safe conversion functions.
+
+### Why is this bad?
+Rust's `as` keyword will perform many kinds of
+conversions, including silently lossy conversions. Conversion functions such
+as `i32::from` will only perform lossless conversions. Using the conversion
+functions prevents conversions from turning into silent lossy conversions if
+the types of the input expressions ever change, and make it easier for
+people reading the code to know that the conversion is lossless.
+
+### Example
+```
+fn as_u64(x: u8) -> u64 {
+ x as u64
+}
+```
+
+Using `::from` would look like this:
+
+```
+fn as_u64(x: u8) -> u64 {
+ u64::from(x)
+}
+``` \ No newline at end of file