Auto merge of #5677 - lzutao:checked_conv, r=matthiaskrgr

Fix false negative of `checked_conversion` lint

Closes  #5675
changelog: none
This commit is contained in:
bors 2020-06-03 16:06:39 +00:00
commit e2fdeecc22
5 changed files with 188 additions and 200 deletions

View file

@ -58,24 +58,18 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CheckedConversions {
}
};
if_chain! {
if let Some(cv) = result;
if let Some(to_type) = cv.to_type;
then {
if let Some(cv) = result {
if let Some(to_type) = cv.to_type {
let mut applicability = Applicability::MachineApplicable;
let snippet = snippet_with_applicability(cx, cv.expr_to_cast.span, "_", &mut
applicability);
let snippet = snippet_with_applicability(cx, cv.expr_to_cast.span, "_", &mut applicability);
span_lint_and_sugg(
cx,
CHECKED_CONVERSIONS,
item.span,
"Checked cast can be simplified.",
"try",
format!("{}::try_from({}).is_ok()",
to_type,
snippet),
applicability
format!("{}::try_from({}).is_ok()", to_type, snippet),
applicability,
);
}
}
@ -184,7 +178,7 @@ fn check_upper_bound<'tcx>(expr: &'tcx Expr<'tcx>) -> Option<Conversion<'tcx>> {
if_chain! {
if let ExprKind::Binary(ref op, ref left, ref right) = &expr.kind;
if let Some((candidate, check)) = normalize_le_ge(op, left, right);
if let Some((from, to)) = get_types_from_cast(check, MAX_VALUE, INTS);
if let Some((from, to)) = get_types_from_cast(check, INTS, "max_value", "MAX");
then {
Conversion::try_new(candidate, from, to)
@ -224,7 +218,7 @@ fn check_lower_bound_zero<'a>(candidate: &'a Expr<'_>, check: &'a Expr<'_>) -> O
/// Check for `expr >= (to_type::MIN as from_type)`
fn check_lower_bound_min<'a>(candidate: &'a Expr<'_>, check: &'a Expr<'_>) -> Option<Conversion<'a>> {
if let Some((from, to)) = get_types_from_cast(check, MIN_VALUE, SINTS) {
if let Some((from, to)) = get_types_from_cast(check, SINTS, "min_value", "MIN") {
Conversion::try_new(candidate, from, to)
} else {
None
@ -232,10 +226,16 @@ fn check_lower_bound_min<'a>(candidate: &'a Expr<'_>, check: &'a Expr<'_>) -> Op
}
/// Tries to extract the from- and to-type from a cast expression
fn get_types_from_cast<'a>(expr: &'a Expr<'_>, func: &'a str, types: &'a [&str]) -> Option<(&'a str, &'a str)> {
// `to_type::maxmin_value() as from_type`
fn get_types_from_cast<'a>(
expr: &'a Expr<'_>,
types: &'a [&str],
func: &'a str,
assoc_const: &'a str,
) -> Option<(&'a str, &'a str)> {
// `to_type::max_value() as from_type`
// or `to_type::MAX as from_type`
let call_from_cast: Option<(&Expr<'_>, &str)> = if_chain! {
// to_type::maxmin_value(), from_type
// to_type::max_value(), from_type
if let ExprKind::Cast(ref limit, ref from_type) = &expr.kind;
if let TyKind::Path(ref from_type_path) = &from_type.kind;
if let Some(from_sym) = int_ty_to_sym(from_type_path);
@ -247,17 +247,17 @@ fn get_types_from_cast<'a>(expr: &'a Expr<'_>, func: &'a str, types: &'a [&str])
}
};
// `from_type::from(to_type::maxmin_value())`
// `from_type::from(to_type::max_value())`
let limit_from: Option<(&Expr<'_>, &str)> = call_from_cast.or_else(|| {
if_chain! {
// `from_type::from, to_type::maxmin_value()`
// `from_type::from, to_type::max_value()`
if let ExprKind::Call(ref from_func, ref args) = &expr.kind;
// `to_type::maxmin_value()`
// `to_type::max_value()`
if args.len() == 1;
if let limit = &args[0];
// `from_type::from`
if let ExprKind::Path(ref path) = &from_func.kind;
if let Some(from_sym) = get_implementing_type(path, INTS, FROM);
if let Some(from_sym) = get_implementing_type(path, INTS, "from");
then {
Some((limit, from_sym))
@ -268,22 +268,26 @@ fn get_types_from_cast<'a>(expr: &'a Expr<'_>, func: &'a str, types: &'a [&str])
});
if let Some((limit, from_type)) = limit_from {
if_chain! {
if let ExprKind::Call(ref fun_name, _) = &limit.kind;
// `to_type, maxmin_value`
if let ExprKind::Path(ref path) = &fun_name.kind;
// `to_type`
if let Some(to_type) = get_implementing_type(path, types, func);
then {
Some((from_type, to_type))
} else {
None
}
match limit.kind {
// `from_type::from(_)`
ExprKind::Call(path, _) => {
if let ExprKind::Path(ref path) = path.kind {
// `to_type`
if let Some(to_type) = get_implementing_type(path, types, func) {
return Some((from_type, to_type));
}
}
},
// `to_type::MAX`
ExprKind::Path(ref path) => {
if let Some(to_type) = get_implementing_type(path, types, assoc_const) {
return Some((from_type, to_type));
}
},
_ => {},
}
} else {
None
}
};
None
}
/// Gets the type which implements the called function
@ -336,10 +340,6 @@ fn normalize_le_ge<'a>(op: &BinOp, left: &'a Expr<'a>, right: &'a Expr<'a>) -> O
}
// Constants
const FROM: &str = "from";
const MAX_VALUE: &str = "max_value";
const MIN_VALUE: &str = "min_value";
const UINTS: &[&str] = &["u8", "u16", "u32", "u64", "usize"];
const SINTS: &[&str] = &["i8", "i16", "i32", "i64", "isize"];
const INTS: &[&str] = &["u8", "u16", "u32", "u64", "usize", "i8", "i16", "i32", "i64", "isize"];

View file

@ -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() {}

View file

@ -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 value <= (u32::max_value() as i64) && value >= 0 {
Some(value as u32)
} else {
None
}
pub fn i64_to_u32(value: i64) {
let _ = value <= (u32::max_value() as i64) && value >= 0;
let _ = value <= (u32::MAX as i64) && value >= 0;
}
fn i64_to_u16(value: i64) -> Option<u16> {
if value <= i64::from(u16::max_value()) && value >= 0 {
Some(value as u16)
} else {
None
}
pub fn i64_to_u16(value: i64) {
let _ = value <= i64::from(u16::max_value()) && value >= 0;
let _ = value <= i64::from(u16::MAX) && value >= 0;
}
fn isize_to_u8(value: isize) -> Option<u8> {
if value <= (u8::max_value() as isize) && value >= 0 {
Some(value as u8)
} else {
None
}
pub fn isize_to_u8(value: isize) {
let _ = value <= (u8::max_value() as isize) && value >= 0;
let _ = value <= (u8::MAX as isize) && value >= 0;
}
// Signed to signed
fn i64_to_i32(value: i64) -> Option<i32> {
if value <= (i32::max_value() as i64) && value >= (i32::min_value() as i64) {
Some(value as i32)
} else {
None
}
pub fn i64_to_i32(value: i64) {
let _ = value <= (i32::max_value() as i64) && value >= (i32::min_value() as i64);
let _ = value <= (i32::MAX as i64) && value >= (i32::MIN as i64);
}
fn i64_to_i16(value: i64) -> Option<i16> {
if value <= i64::from(i16::max_value()) && value >= i64::from(i16::min_value()) {
Some(value as i16)
} else {
None
}
pub fn i64_to_i16(value: i64) {
let _ = value <= i64::from(i16::max_value()) && value >= i64::from(i16::min_value());
let _ = value <= i64::from(i16::MAX) && value >= i64::from(i16::MIN);
}
// Unsigned to X
fn u32_to_i32(value: u32) -> Option<i32> {
if value <= i32::max_value() as u32 {
Some(value as i32)
} else {
None
}
pub fn u32_to_i32(value: u32) {
let _ = value <= i32::max_value() as u32;
let _ = value <= i32::MAX as u32;
}
fn usize_to_isize(value: usize) -> isize {
if value <= isize::max_value() as usize && value as i32 == 5 {
5
} else {
1
}
pub fn usize_to_isize(value: usize) {
let _ = value <= isize::max_value() as usize && value as i32 == 5;
let _ = value <= isize::MAX as usize && value as i32 == 5;
}
fn u32_to_u16(value: u32) -> isize {
if value <= u16::max_value() as u32 && value as i32 == 5 {
5
} else {
1
}
pub fn u32_to_u16(value: u32) {
let _ = value <= u16::max_value() as u32 && value as i32 == 5;
let _ = value <= u16::MAX as u32 && 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() {}

View file

@ -1,52 +1,100 @@
error: Checked cast can be simplified.
--> $DIR/checked_conversions.rs:13:8
--> $DIR/checked_conversions.rs:17:13
|
LL | if value <= (u32::max_value() as i64) && value >= 0 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u32::try_from(value).is_ok()`
LL | let _ = value <= (u32::max_value() as i64) && value >= 0;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u32::try_from(value).is_ok()`
|
= note: `-D clippy::checked-conversions` implied by `-D warnings`
error: Checked cast can be simplified.
--> $DIR/checked_conversions.rs:21:8
--> $DIR/checked_conversions.rs:18:13
|
LL | if value <= i64::from(u16::max_value()) && value >= 0 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u16::try_from(value).is_ok()`
LL | let _ = value <= (u32::MAX as i64) && value >= 0;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u32::try_from(value).is_ok()`
error: Checked cast can be simplified.
--> $DIR/checked_conversions.rs:29:8
--> $DIR/checked_conversions.rs:22:13
|
LL | if value <= (u8::max_value() as isize) && value >= 0 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u8::try_from(value).is_ok()`
LL | let _ = value <= i64::from(u16::max_value()) && value >= 0;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u16::try_from(value).is_ok()`
error: Checked cast can be simplified.
--> $DIR/checked_conversions.rs:39:8
--> $DIR/checked_conversions.rs:23:13
|
LL | if value <= (i32::max_value() as i64) && value >= (i32::min_value() as i64) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i32::try_from(value).is_ok()`
LL | let _ = value <= i64::from(u16::MAX) && value >= 0;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u16::try_from(value).is_ok()`
error: Checked cast can be simplified.
--> $DIR/checked_conversions.rs:47:8
--> $DIR/checked_conversions.rs:27:13
|
LL | if value <= i64::from(i16::max_value()) && value >= i64::from(i16::min_value()) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i16::try_from(value).is_ok()`
LL | let _ = value <= (u8::max_value() as isize) && value >= 0;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u8::try_from(value).is_ok()`
error: Checked cast can be simplified.
--> $DIR/checked_conversions.rs:57:8
--> $DIR/checked_conversions.rs:28:13
|
LL | if value <= i32::max_value() as u32 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i32::try_from(value).is_ok()`
LL | let _ = value <= (u8::MAX as isize) && value >= 0;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u8::try_from(value).is_ok()`
error: Checked cast can be simplified.
--> $DIR/checked_conversions.rs:65:8
--> $DIR/checked_conversions.rs:34:13
|
LL | if value <= isize::max_value() as usize && value as i32 == 5 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `isize::try_from(value).is_ok()`
LL | let _ = value <= (i32::max_value() as i64) && value >= (i32::min_value() as i64);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i32::try_from(value).is_ok()`
error: Checked cast can be simplified.
--> $DIR/checked_conversions.rs:73:8
--> $DIR/checked_conversions.rs:35:13
|
LL | if value <= u16::max_value() as u32 && value as i32 == 5 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u16::try_from(value).is_ok()`
LL | let _ = value <= (i32::MAX as i64) && value >= (i32::MIN as i64);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i32::try_from(value).is_ok()`
error: aborting due to 8 previous errors
error: Checked cast can be simplified.
--> $DIR/checked_conversions.rs:39:13
|
LL | let _ = value <= i64::from(i16::max_value()) && value >= i64::from(i16::min_value());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i16::try_from(value).is_ok()`
error: Checked cast can be simplified.
--> $DIR/checked_conversions.rs:40:13
|
LL | let _ = value <= i64::from(i16::MAX) && value >= i64::from(i16::MIN);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i16::try_from(value).is_ok()`
error: Checked cast can be simplified.
--> $DIR/checked_conversions.rs:46:13
|
LL | let _ = value <= i32::max_value() as u32;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i32::try_from(value).is_ok()`
error: Checked cast can be simplified.
--> $DIR/checked_conversions.rs:47:13
|
LL | let _ = value <= i32::MAX as u32;
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i32::try_from(value).is_ok()`
error: Checked cast can be simplified.
--> $DIR/checked_conversions.rs:51:13
|
LL | let _ = value <= isize::max_value() as usize && value as i32 == 5;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `isize::try_from(value).is_ok()`
error: Checked cast can be simplified.
--> $DIR/checked_conversions.rs:52:13
|
LL | let _ = value <= isize::MAX as usize && value as i32 == 5;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `isize::try_from(value).is_ok()`
error: Checked cast can be simplified.
--> $DIR/checked_conversions.rs:56:13
|
LL | let _ = value <= u16::max_value() as u32 && value as i32 == 5;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u16::try_from(value).is_ok()`
error: Checked cast can be simplified.
--> $DIR/checked_conversions.rs:57:13
|
LL | let _ = value <= u16::MAX as u32 && value as i32 == 5;
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u16::try_from(value).is_ok()`
error: aborting due to 16 previous errors