forked from rust-lang/rust
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#7948 - 5225225:castlosslessbool, r=llogiq
Lint for bool to integer casts in `cast_lossless` The lint description says > Checks for casts between *numerical* types that may be replaced by safe conversion functions. Which is strictly speaking being violated here, but it seems within the spirit of the lint. I think it is still a useful lint to have, and having a different lint for just this feels excessive. Thoughts? Fixes rust-lang#7947 changelog: Lint for bool to integer casts in [`cast_lossless`]
- Loading branch information
Showing
10 changed files
with
221 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// run-rustfix | ||
|
||
#![allow(dead_code)] | ||
#![warn(clippy::cast_lossless)] | ||
|
||
fn main() { | ||
// Test clippy::cast_lossless with casts to integer types | ||
let _ = u8::from(true); | ||
let _ = u16::from(true); | ||
let _ = u32::from(true); | ||
let _ = u64::from(true); | ||
let _ = u128::from(true); | ||
let _ = usize::from(true); | ||
|
||
let _ = i8::from(true); | ||
let _ = i16::from(true); | ||
let _ = i32::from(true); | ||
let _ = i64::from(true); | ||
let _ = i128::from(true); | ||
let _ = isize::from(true); | ||
|
||
// Test with an expression wrapped in parens | ||
let _ = u16::from(true | false); | ||
} | ||
|
||
// The lint would suggest using `u32::from(input)` here but the `XX::from` function is not const, | ||
// so we skip the lint if the expression is in a const fn. | ||
// See #3656 | ||
const fn abc(input: bool) -> u32 { | ||
input as u32 | ||
} | ||
|
||
// Same as the above issue. We can't suggest `::from` in const fns in impls | ||
mod cast_lossless_in_impl { | ||
struct A; | ||
|
||
impl A { | ||
pub const fn convert(x: bool) -> u64 { | ||
x as u64 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// run-rustfix | ||
|
||
#![allow(dead_code)] | ||
#![warn(clippy::cast_lossless)] | ||
|
||
fn main() { | ||
// Test clippy::cast_lossless with casts to integer types | ||
let _ = true as u8; | ||
let _ = true as u16; | ||
let _ = true as u32; | ||
let _ = true as u64; | ||
let _ = true as u128; | ||
let _ = true as usize; | ||
|
||
let _ = true as i8; | ||
let _ = true as i16; | ||
let _ = true as i32; | ||
let _ = true as i64; | ||
let _ = true as i128; | ||
let _ = true as isize; | ||
|
||
// Test with an expression wrapped in parens | ||
let _ = (true | false) as u16; | ||
} | ||
|
||
// The lint would suggest using `u32::from(input)` here but the `XX::from` function is not const, | ||
// so we skip the lint if the expression is in a const fn. | ||
// See #3656 | ||
const fn abc(input: bool) -> u32 { | ||
input as u32 | ||
} | ||
|
||
// Same as the above issue. We can't suggest `::from` in const fns in impls | ||
mod cast_lossless_in_impl { | ||
struct A; | ||
|
||
impl A { | ||
pub const fn convert(x: bool) -> u64 { | ||
x as u64 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
error: casting `bool` to `u8` is more cleanly stated with `u8::from(_)` | ||
--> $DIR/cast_lossless_bool.rs:8:13 | ||
| | ||
LL | let _ = true as u8; | ||
| ^^^^^^^^^^ help: try: `u8::from(true)` | ||
| | ||
= note: `-D clippy::cast-lossless` implied by `-D warnings` | ||
|
||
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` 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` 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` 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` 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` 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` 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` 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` 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` 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` 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` is more cleanly stated with `u16::from(_)` | ||
--> $DIR/cast_lossless_bool.rs:23:13 | ||
| | ||
LL | let _ = (true | false) as u16; | ||
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `u16::from(true | false)` | ||
|
||
error: aborting due to 13 previous errors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters