about summary refs log tree commit diff
diff options
context:
space:
mode:
author5225225 <5225225@mailbox.org>2021-11-12 17:01:35 +0000
committer5225225 <5225225@mailbox.org>2021-11-12 17:01:35 +0000
commitd4c8cb63a474c2365664a68e35153adef3cdd9b9 (patch)
tree727ec2e0c7cfeff9824043d75879d83978453d39
parent6e84f00045e20cbd9f9d2695f67e9fc0852134d4 (diff)
downloadrust-d4c8cb63a474c2365664a68e35153adef3cdd9b9.tar.gz
rust-d4c8cb63a474c2365664a68e35153adef3cdd9b9.zip
Change cast_lossless message for bools only
-rw-r--r--clippy_lints/src/casts/cast_lossless.rs17
-rw-r--r--tests/ui/cast_lossless_bool.stderr26
2 files changed, 26 insertions, 17 deletions
diff --git a/clippy_lints/src/casts/cast_lossless.rs b/clippy_lints/src/casts/cast_lossless.rs
index cc360fc9c76..4a95bed1148 100644
--- a/clippy_lints/src/casts/cast_lossless.rs
+++ b/clippy_lints/src/casts/cast_lossless.rs
@@ -40,14 +40,23 @@ pub(super) fn check(
         },
     );
 
+    let message = if cast_from.is_bool() {
+        format!(
+            "casting `{0:}` to `{1:}` is more cleanly stated with `{1:}::from(_)`",
+            cast_from, cast_to
+        )
+    } else {
+        format!(
+            "casting `{}` to `{}` may become silently lossy if you later change the type",
+            cast_from, cast_to
+        )
+    };
+
     span_lint_and_sugg(
         cx,
         CAST_LOSSLESS,
         expr.span,
-        &format!(
-            "casting `{}` to `{}` may become silently lossy if you later change the type",
-            cast_from, cast_to
-        ),
+        &message,
         "try",
         format!("{}::from({})", cast_to, sugg),
         applicability,
diff --git a/tests/ui/cast_lossless_bool.stderr b/tests/ui/cast_lossless_bool.stderr
index a0d11f08acf..6b148336011 100644
--- a/tests/ui/cast_lossless_bool.stderr
+++ b/tests/ui/cast_lossless_bool.stderr
@@ -1,4 +1,4 @@
-error: casting `bool` to `u8` may become silently lossy if you later change the type
+error: casting `bool` to `u8` is more cleanly stated with `u8::from(_)`
   --> $DIR/cast_lossless_bool.rs:8:13
    |
 LL |     let _ = true as u8;
@@ -6,73 +6,73 @@ LL |     let _ = true as u8;
    |
    = note: `-D clippy::cast-lossless` implied by `-D warnings`
 
-error: casting `bool` to `u16` may become silently lossy if you later change the type
+error: casting `bool` to `u16` is more cleanly stated with `u16::from(_)`
   --> $DIR/cast_lossless_bool.rs:9:13
    |
 LL |     let _ = true as u16;
    |             ^^^^^^^^^^^ help: try: `u16::from(true)`
 
-error: casting `bool` to `u32` may become silently lossy if you later change the type
+error: casting `bool` to `u32` is more cleanly stated with `u32::from(_)`
   --> $DIR/cast_lossless_bool.rs:10:13
    |
 LL |     let _ = true as u32;
    |             ^^^^^^^^^^^ help: try: `u32::from(true)`
 
-error: casting `bool` to `u64` may become silently lossy if you later change the type
+error: casting `bool` to `u64` is more cleanly stated with `u64::from(_)`
   --> $DIR/cast_lossless_bool.rs:11:13
    |
 LL |     let _ = true as u64;
    |             ^^^^^^^^^^^ help: try: `u64::from(true)`
 
-error: casting `bool` to `u128` may become silently lossy if you later change the type
+error: casting `bool` to `u128` is more cleanly stated with `u128::from(_)`
   --> $DIR/cast_lossless_bool.rs:12:13
    |
 LL |     let _ = true as u128;
    |             ^^^^^^^^^^^^ help: try: `u128::from(true)`
 
-error: casting `bool` to `usize` may become silently lossy if you later change the type
+error: casting `bool` to `usize` is more cleanly stated with `usize::from(_)`
   --> $DIR/cast_lossless_bool.rs:13:13
    |
 LL |     let _ = true as usize;
    |             ^^^^^^^^^^^^^ help: try: `usize::from(true)`
 
-error: casting `bool` to `i8` may become silently lossy if you later change the type
+error: casting `bool` to `i8` is more cleanly stated with `i8::from(_)`
   --> $DIR/cast_lossless_bool.rs:15:13
    |
 LL |     let _ = true as i8;
    |             ^^^^^^^^^^ help: try: `i8::from(true)`
 
-error: casting `bool` to `i16` may become silently lossy if you later change the type
+error: casting `bool` to `i16` is more cleanly stated with `i16::from(_)`
   --> $DIR/cast_lossless_bool.rs:16:13
    |
 LL |     let _ = true as i16;
    |             ^^^^^^^^^^^ help: try: `i16::from(true)`
 
-error: casting `bool` to `i32` may become silently lossy if you later change the type
+error: casting `bool` to `i32` is more cleanly stated with `i32::from(_)`
   --> $DIR/cast_lossless_bool.rs:17:13
    |
 LL |     let _ = true as i32;
    |             ^^^^^^^^^^^ help: try: `i32::from(true)`
 
-error: casting `bool` to `i64` may become silently lossy if you later change the type
+error: casting `bool` to `i64` is more cleanly stated with `i64::from(_)`
   --> $DIR/cast_lossless_bool.rs:18:13
    |
 LL |     let _ = true as i64;
    |             ^^^^^^^^^^^ help: try: `i64::from(true)`
 
-error: casting `bool` to `i128` may become silently lossy if you later change the type
+error: casting `bool` to `i128` is more cleanly stated with `i128::from(_)`
   --> $DIR/cast_lossless_bool.rs:19:13
    |
 LL |     let _ = true as i128;
    |             ^^^^^^^^^^^^ help: try: `i128::from(true)`
 
-error: casting `bool` to `isize` may become silently lossy if you later change the type
+error: casting `bool` to `isize` is more cleanly stated with `isize::from(_)`
   --> $DIR/cast_lossless_bool.rs:20:13
    |
 LL |     let _ = true as isize;
    |             ^^^^^^^^^^^^^ help: try: `isize::from(true)`
 
-error: casting `bool` to `u16` may become silently lossy if you later change the type
+error: casting `bool` to `u16` is more cleanly stated with `u16::from(_)`
   --> $DIR/cast_lossless_bool.rs:23:13
    |
 LL |     let _ = (true | false) as u16;