forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
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#5677 - lzutao:checked_conv, r=matthiaskrgr
Fix false negative of `checked_conversion` lint Closes rust-lang#5675 changelog: none
- Loading branch information
Showing
5 changed files
with
188 additions
and
200 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,76 @@ | ||
// run-rustfix | ||
|
||
#![allow( | ||
clippy::cast_lossless, | ||
// Int::max_value will be deprecated in the future | ||
deprecated, | ||
)] | ||
#![warn(clippy::checked_conversions)] | ||
#![allow(clippy::cast_lossless)] | ||
#![allow(dead_code)] | ||
|
||
use std::convert::TryFrom; | ||
|
||
// Positive tests | ||
|
||
// Signed to unsigned | ||
|
||
fn i64_to_u32(value: i64) -> Option<u32> { | ||
if u32::try_from(value).is_ok() { | ||
Some(value as u32) | ||
} else { | ||
None | ||
} | ||
pub fn i64_to_u32(value: i64) { | ||
let _ = u32::try_from(value).is_ok(); | ||
let _ = u32::try_from(value).is_ok(); | ||
} | ||
|
||
fn i64_to_u16(value: i64) -> Option<u16> { | ||
if u16::try_from(value).is_ok() { | ||
Some(value as u16) | ||
} else { | ||
None | ||
} | ||
pub fn i64_to_u16(value: i64) { | ||
let _ = u16::try_from(value).is_ok(); | ||
let _ = u16::try_from(value).is_ok(); | ||
} | ||
|
||
fn isize_to_u8(value: isize) -> Option<u8> { | ||
if u8::try_from(value).is_ok() { | ||
Some(value as u8) | ||
} else { | ||
None | ||
} | ||
pub fn isize_to_u8(value: isize) { | ||
let _ = u8::try_from(value).is_ok(); | ||
let _ = u8::try_from(value).is_ok(); | ||
} | ||
|
||
// Signed to signed | ||
|
||
fn i64_to_i32(value: i64) -> Option<i32> { | ||
if i32::try_from(value).is_ok() { | ||
Some(value as i32) | ||
} else { | ||
None | ||
} | ||
pub fn i64_to_i32(value: i64) { | ||
let _ = i32::try_from(value).is_ok(); | ||
let _ = i32::try_from(value).is_ok(); | ||
} | ||
|
||
fn i64_to_i16(value: i64) -> Option<i16> { | ||
if i16::try_from(value).is_ok() { | ||
Some(value as i16) | ||
} else { | ||
None | ||
} | ||
pub fn i64_to_i16(value: i64) { | ||
let _ = i16::try_from(value).is_ok(); | ||
let _ = i16::try_from(value).is_ok(); | ||
} | ||
|
||
// Unsigned to X | ||
|
||
fn u32_to_i32(value: u32) -> Option<i32> { | ||
if i32::try_from(value).is_ok() { | ||
Some(value as i32) | ||
} else { | ||
None | ||
} | ||
pub fn u32_to_i32(value: u32) { | ||
let _ = i32::try_from(value).is_ok(); | ||
let _ = i32::try_from(value).is_ok(); | ||
} | ||
|
||
fn usize_to_isize(value: usize) -> isize { | ||
if isize::try_from(value).is_ok() && value as i32 == 5 { | ||
5 | ||
} else { | ||
1 | ||
} | ||
pub fn usize_to_isize(value: usize) { | ||
let _ = isize::try_from(value).is_ok() && value as i32 == 5; | ||
let _ = isize::try_from(value).is_ok() && value as i32 == 5; | ||
} | ||
|
||
fn u32_to_u16(value: u32) -> isize { | ||
if u16::try_from(value).is_ok() && value as i32 == 5 { | ||
5 | ||
} else { | ||
1 | ||
} | ||
pub fn u32_to_u16(value: u32) { | ||
let _ = u16::try_from(value).is_ok() && value as i32 == 5; | ||
let _ = u16::try_from(value).is_ok() && value as i32 == 5; | ||
} | ||
|
||
// Negative tests | ||
|
||
fn no_i64_to_i32(value: i64) -> Option<i32> { | ||
if value <= (i32::max_value() as i64) && value >= 0 { | ||
Some(value as i32) | ||
} else { | ||
None | ||
} | ||
pub fn no_i64_to_i32(value: i64) { | ||
let _ = value <= (i32::max_value() as i64) && value >= 0; | ||
let _ = value <= (i32::MAX as i64) && value >= 0; | ||
} | ||
|
||
fn no_isize_to_u8(value: isize) -> Option<u8> { | ||
if value <= (u8::max_value() as isize) && value >= (u8::min_value() as isize) { | ||
Some(value as u8) | ||
} else { | ||
None | ||
} | ||
pub fn no_isize_to_u8(value: isize) { | ||
let _ = value <= (u8::max_value() as isize) && value >= (u8::min_value() as isize); | ||
let _ = value <= (u8::MAX as isize) && value >= (u8::MIN as isize); | ||
} | ||
|
||
fn i8_to_u8(value: i8) -> Option<u8> { | ||
if value >= 0 { | ||
Some(value as u8) | ||
} else { | ||
None | ||
} | ||
pub fn i8_to_u8(value: i8) { | ||
let _ = value >= 0; | ||
} | ||
|
||
fn main() {} |
Oops, something went wrong.