Skip to content

Commit d4c8cb6

Browse files
committed
Change cast_lossless message for bools only
1 parent 6e84f00 commit d4c8cb6

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

clippy_lints/src/casts/cast_lossless.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,23 @@ pub(super) fn check(
4040
},
4141
);
4242

43+
let message = if cast_from.is_bool() {
44+
format!(
45+
"casting `{0:}` to `{1:}` is more cleanly stated with `{1:}::from(_)`",
46+
cast_from, cast_to
47+
)
48+
} else {
49+
format!(
50+
"casting `{}` to `{}` may become silently lossy if you later change the type",
51+
cast_from, cast_to
52+
)
53+
};
54+
4355
span_lint_and_sugg(
4456
cx,
4557
CAST_LOSSLESS,
4658
expr.span,
47-
&format!(
48-
"casting `{}` to `{}` may become silently lossy if you later change the type",
49-
cast_from, cast_to
50-
),
59+
&message,
5160
"try",
5261
format!("{}::from({})", cast_to, sugg),
5362
applicability,

tests/ui/cast_lossless_bool.stderr

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,78 @@
1-
error: casting `bool` to `u8` may become silently lossy if you later change the type
1+
error: casting `bool` to `u8` is more cleanly stated with `u8::from(_)`
22
--> $DIR/cast_lossless_bool.rs:8:13
33
|
44
LL | let _ = true as u8;
55
| ^^^^^^^^^^ help: try: `u8::from(true)`
66
|
77
= note: `-D clippy::cast-lossless` implied by `-D warnings`
88

9-
error: casting `bool` to `u16` may become silently lossy if you later change the type
9+
error: casting `bool` to `u16` is more cleanly stated with `u16::from(_)`
1010
--> $DIR/cast_lossless_bool.rs:9:13
1111
|
1212
LL | let _ = true as u16;
1313
| ^^^^^^^^^^^ help: try: `u16::from(true)`
1414

15-
error: casting `bool` to `u32` may become silently lossy if you later change the type
15+
error: casting `bool` to `u32` is more cleanly stated with `u32::from(_)`
1616
--> $DIR/cast_lossless_bool.rs:10:13
1717
|
1818
LL | let _ = true as u32;
1919
| ^^^^^^^^^^^ help: try: `u32::from(true)`
2020

21-
error: casting `bool` to `u64` may become silently lossy if you later change the type
21+
error: casting `bool` to `u64` is more cleanly stated with `u64::from(_)`
2222
--> $DIR/cast_lossless_bool.rs:11:13
2323
|
2424
LL | let _ = true as u64;
2525
| ^^^^^^^^^^^ help: try: `u64::from(true)`
2626

27-
error: casting `bool` to `u128` may become silently lossy if you later change the type
27+
error: casting `bool` to `u128` is more cleanly stated with `u128::from(_)`
2828
--> $DIR/cast_lossless_bool.rs:12:13
2929
|
3030
LL | let _ = true as u128;
3131
| ^^^^^^^^^^^^ help: try: `u128::from(true)`
3232

33-
error: casting `bool` to `usize` may become silently lossy if you later change the type
33+
error: casting `bool` to `usize` is more cleanly stated with `usize::from(_)`
3434
--> $DIR/cast_lossless_bool.rs:13:13
3535
|
3636
LL | let _ = true as usize;
3737
| ^^^^^^^^^^^^^ help: try: `usize::from(true)`
3838

39-
error: casting `bool` to `i8` may become silently lossy if you later change the type
39+
error: casting `bool` to `i8` is more cleanly stated with `i8::from(_)`
4040
--> $DIR/cast_lossless_bool.rs:15:13
4141
|
4242
LL | let _ = true as i8;
4343
| ^^^^^^^^^^ help: try: `i8::from(true)`
4444

45-
error: casting `bool` to `i16` may become silently lossy if you later change the type
45+
error: casting `bool` to `i16` is more cleanly stated with `i16::from(_)`
4646
--> $DIR/cast_lossless_bool.rs:16:13
4747
|
4848
LL | let _ = true as i16;
4949
| ^^^^^^^^^^^ help: try: `i16::from(true)`
5050

51-
error: casting `bool` to `i32` may become silently lossy if you later change the type
51+
error: casting `bool` to `i32` is more cleanly stated with `i32::from(_)`
5252
--> $DIR/cast_lossless_bool.rs:17:13
5353
|
5454
LL | let _ = true as i32;
5555
| ^^^^^^^^^^^ help: try: `i32::from(true)`
5656

57-
error: casting `bool` to `i64` may become silently lossy if you later change the type
57+
error: casting `bool` to `i64` is more cleanly stated with `i64::from(_)`
5858
--> $DIR/cast_lossless_bool.rs:18:13
5959
|
6060
LL | let _ = true as i64;
6161
| ^^^^^^^^^^^ help: try: `i64::from(true)`
6262

63-
error: casting `bool` to `i128` may become silently lossy if you later change the type
63+
error: casting `bool` to `i128` is more cleanly stated with `i128::from(_)`
6464
--> $DIR/cast_lossless_bool.rs:19:13
6565
|
6666
LL | let _ = true as i128;
6767
| ^^^^^^^^^^^^ help: try: `i128::from(true)`
6868

69-
error: casting `bool` to `isize` may become silently lossy if you later change the type
69+
error: casting `bool` to `isize` is more cleanly stated with `isize::from(_)`
7070
--> $DIR/cast_lossless_bool.rs:20:13
7171
|
7272
LL | let _ = true as isize;
7373
| ^^^^^^^^^^^^^ help: try: `isize::from(true)`
7474

75-
error: casting `bool` to `u16` may become silently lossy if you later change the type
75+
error: casting `bool` to `u16` is more cleanly stated with `u16::from(_)`
7676
--> $DIR/cast_lossless_bool.rs:23:13
7777
|
7878
LL | let _ = (true | false) as u16;

0 commit comments

Comments
 (0)