Skip to content

Commit 48f7b5e

Browse files
committed
cast_lossless: lint when converting char as well
1 parent e9aed87 commit 48f7b5e

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

clippy_lints/src/casts/cast_lossless.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,16 @@ fn should_lint(cx: &LateContext<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>, msrv: M
9696
};
9797
!is_isize_or_usize(cast_from) && from_nbits < to_nbits
9898
},
99-
(false, true) if matches!(cast_from.kind(), ty::Bool) && msrv.meets(cx, msrvs::FROM_BOOL) => true,
99+
(false, true)
100+
if (*cast_from.kind() == ty::Char
101+
&& matches!(
102+
cast_to.kind(),
103+
ty::Uint(ty::UintTy::U32 | ty::UintTy::U64 | ty::UintTy::U128)
104+
))
105+
|| (*cast_from.kind() == ty::Bool && msrv.meets(cx, msrvs::FROM_BOOL)) =>
106+
{
107+
true
108+
},
100109
(_, _) => {
101110
matches!(cast_from.kind(), ty::Float(FloatTy::F32)) && matches!(cast_to.kind(), ty::Float(FloatTy::F64))
102111
},

tests/ui/cast_lossless_char.fixed

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![warn(clippy::cast_lossless)]
2+
#![allow(clippy::char_lit_as_u8)]
3+
4+
type I32 = i32;
5+
type U32 = u32;
6+
7+
fn main() {
8+
let _ = u32::from('a');
9+
//~^ cast_lossless
10+
let _ = 'a' as i32;
11+
let _ = u64::from('a');
12+
//~^ cast_lossless
13+
let _ = 'a' as i64;
14+
let _ = U32::from('a');
15+
//~^ cast_lossless
16+
let _ = 'a' as I32;
17+
18+
let _ = 'a' as u8;
19+
let _ = 'a' as i8;
20+
}

tests/ui/cast_lossless_char.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![warn(clippy::cast_lossless)]
2+
#![allow(clippy::char_lit_as_u8)]
3+
4+
type I32 = i32;
5+
type U32 = u32;
6+
7+
fn main() {
8+
let _ = 'a' as u32;
9+
//~^ cast_lossless
10+
let _ = 'a' as i32;
11+
let _ = 'a' as u64;
12+
//~^ cast_lossless
13+
let _ = 'a' as i64;
14+
let _ = 'a' as U32;
15+
//~^ cast_lossless
16+
let _ = 'a' as I32;
17+
18+
let _ = 'a' as u8;
19+
let _ = 'a' as i8;
20+
}

tests/ui/cast_lossless_char.stderr

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
error: casts from `char` to `u32` can be expressed infallibly using `From`
2+
--> tests/ui/cast_lossless_char.rs:8:13
3+
|
4+
LL | let _ = 'a' as u32;
5+
| ^^^^^^^^^^
6+
|
7+
= help: an `as` cast can become silently lossy if the types change in the future
8+
= note: `-D clippy::cast-lossless` implied by `-D warnings`
9+
= help: to override `-D warnings` add `#[allow(clippy::cast_lossless)]`
10+
help: use `u32::from` instead
11+
|
12+
LL - let _ = 'a' as u32;
13+
LL + let _ = u32::from('a');
14+
|
15+
16+
error: casts from `char` to `u64` can be expressed infallibly using `From`
17+
--> tests/ui/cast_lossless_char.rs:11:13
18+
|
19+
LL | let _ = 'a' as u64;
20+
| ^^^^^^^^^^
21+
|
22+
= help: an `as` cast can become silently lossy if the types change in the future
23+
help: use `u64::from` instead
24+
|
25+
LL - let _ = 'a' as u64;
26+
LL + let _ = u64::from('a');
27+
|
28+
29+
error: casts from `char` to `u32` can be expressed infallibly using `From`
30+
--> tests/ui/cast_lossless_char.rs:14:13
31+
|
32+
LL | let _ = 'a' as U32;
33+
| ^^^^^^^^^^
34+
|
35+
= help: an `as` cast can become silently lossy if the types change in the future
36+
help: use `U32::from` instead
37+
|
38+
LL - let _ = 'a' as U32;
39+
LL + let _ = U32::from('a');
40+
|
41+
42+
error: aborting due to 3 previous errors
43+

0 commit comments

Comments
 (0)