-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
478 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
use crate::utils::{match_qpath, snippet_with_applicability, span_lint_and_sugg}; | ||
use if_chain::if_chain; | ||
use rustc::hir; | ||
use rustc::lint::LateContext; | ||
use rustc_errors::Applicability; | ||
use rustc_target::abi::LayoutOf; | ||
use syntax::ast; | ||
|
||
pub fn lint(cx: &LateContext<'_, '_>, expr: &hir::Expr, args: &[&[hir::Expr]], arith: &str) { | ||
let unwrap_arg = &args[0][1]; | ||
let arith_lhs = &args[1][0]; | ||
let arith_rhs = &args[1][1]; | ||
|
||
let ty = cx.tables.expr_ty(arith_lhs); | ||
if !ty.is_integral() { | ||
return; | ||
} | ||
|
||
let mm = if let Some(mm) = is_min_or_max(cx, unwrap_arg) { | ||
mm | ||
} else { | ||
return; | ||
}; | ||
|
||
if ty.is_signed() { | ||
use self::{MinMax::*, Sign::*}; | ||
|
||
let sign = if let Some(sign) = lit_sign(arith_rhs) { | ||
sign | ||
} else { | ||
return; | ||
}; | ||
|
||
match (arith, sign, mm) { | ||
("add", Pos, Max) | ("add", Neg, Min) | ("sub", Neg, Max) | ("sub", Pos, Min) => (), | ||
// "mul" is omitted because lhs can be negative. | ||
_ => return, | ||
} | ||
|
||
let mut applicability = Applicability::MachineApplicable; | ||
span_lint_and_sugg( | ||
cx, | ||
super::CHECKED_ARITH_UNWRAP_OR, | ||
expr.span, | ||
"manual saturating arithmetic", | ||
&format!("try using `saturating_{}`", arith), | ||
format!( | ||
"{}.saturating_{}({})", | ||
snippet_with_applicability(cx, arith_lhs.span, "..", &mut applicability), | ||
arith, | ||
snippet_with_applicability(cx, arith_rhs.span, "..", &mut applicability), | ||
), | ||
applicability, | ||
); | ||
} else { | ||
match (mm, arith) { | ||
(MinMax::Max, "add") | (MinMax::Max, "mul") | (MinMax::Min, "sub") => (), | ||
_ => return, | ||
} | ||
|
||
let mut applicability = Applicability::MachineApplicable; | ||
span_lint_and_sugg( | ||
cx, | ||
super::CHECKED_ARITH_UNWRAP_OR, | ||
expr.span, | ||
"manual saturating arithmetic", | ||
&format!("try using `saturating_{}`", arith), | ||
format!( | ||
"{}.saturating_{}({})", | ||
snippet_with_applicability(cx, arith_lhs.span, "..", &mut applicability), | ||
arith, | ||
snippet_with_applicability(cx, arith_rhs.span, "..", &mut applicability), | ||
), | ||
applicability, | ||
); | ||
} | ||
} | ||
|
||
#[derive(PartialEq, Eq)] | ||
enum MinMax { | ||
Min, | ||
Max, | ||
} | ||
|
||
fn is_min_or_max<'tcx>(cx: &LateContext<'_, 'tcx>, expr: &hir::Expr) -> Option<MinMax> { | ||
// `T::max_value()` `T::min_value()` inherent methods | ||
if_chain! { | ||
if let hir::ExprKind::Call(func, args) = &expr.node; | ||
if args.is_empty(); | ||
if let hir::ExprKind::Path(path) = &func.node; | ||
if let hir::QPath::TypeRelative(_, segment) = path; | ||
then { | ||
match &*segment.ident.as_str() { | ||
"max_value" => return Some(MinMax::Max), | ||
"min_value" => return Some(MinMax::Min), | ||
_ => {} | ||
} | ||
} | ||
} | ||
|
||
let ty = cx.tables.expr_ty(expr); | ||
let ty_str = ty.to_string(); | ||
|
||
// `std::T::MAX` `std::T::MIN` constants | ||
if let hir::ExprKind::Path(path) = &expr.node { | ||
if match_qpath(path, &["core", &ty_str, "MAX"][..]) { | ||
return Some(MinMax::Max); | ||
} | ||
|
||
if match_qpath(path, &["core", &ty_str, "MIN"][..]) { | ||
return Some(MinMax::Min); | ||
} | ||
} | ||
|
||
// Literals | ||
let bits = cx.layout_of(ty).unwrap().size.bits(); | ||
let (minval, maxval): (u128, u128) = if ty.is_signed() { | ||
let minval = 1 << (bits - 1); | ||
let mut maxval = !(1 << (bits - 1)); | ||
if bits != 128 { | ||
maxval &= (1 << bits) - 1; | ||
} | ||
(minval, maxval) | ||
} else { | ||
(0, if bits == 128 { !0 } else { (1 << bits) - 1 }) | ||
}; | ||
|
||
let check_lit = |expr: &hir::Expr, check_min: bool| { | ||
if let hir::ExprKind::Lit(lit) = &expr.node { | ||
if let ast::LitKind::Int(value, _) = lit.node { | ||
if value == maxval { | ||
return Some(MinMax::Max); | ||
} | ||
|
||
if check_min && value == minval { | ||
return Some(MinMax::Min); | ||
} | ||
} | ||
} | ||
|
||
None | ||
}; | ||
|
||
if let r @ Some(_) = check_lit(expr, !ty.is_signed()) { | ||
return r; | ||
} | ||
|
||
if ty.is_signed() { | ||
if let hir::ExprKind::Unary(hir::UnNeg, val) = &expr.node { | ||
return check_lit(val, true); | ||
} | ||
} | ||
|
||
None | ||
} | ||
|
||
#[derive(PartialEq, Eq)] | ||
enum Sign { | ||
Pos, | ||
Neg, | ||
} | ||
|
||
fn lit_sign(expr: &hir::Expr) -> Option<Sign> { | ||
if let hir::ExprKind::Unary(hir::UnNeg, inner) = &expr.node { | ||
if let hir::ExprKind::Lit(..) = &inner.node { | ||
return Some(Sign::Neg); | ||
} | ||
} else if let hir::ExprKind::Lit(..) = &expr.node { | ||
return Some(Sign::Pos); | ||
} | ||
|
||
None | ||
} |
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,44 @@ | ||
// run-rustfix | ||
|
||
#[allow(unused_imports)] // they become unnecessary after fix | ||
use std::{i128, i32, u128, u32}; | ||
|
||
fn main() { | ||
let _ = 1u32.saturating_add(1); | ||
let _ = 1u32.saturating_add(1); | ||
let _ = 1u8.saturating_add(1); | ||
let _ = 1u128.saturating_add(1); | ||
let _ = 1u32.checked_add(1).unwrap_or(1234); // ok | ||
let _ = 1u8.checked_add(1).unwrap_or(0); // ok | ||
let _ = 1u32.saturating_mul(1); | ||
|
||
let _ = 1u32.saturating_sub(1); | ||
let _ = 1u32.saturating_sub(1); | ||
let _ = 1u8.saturating_sub(1); | ||
let _ = 1u32.checked_sub(1).unwrap_or(1234); // ok | ||
let _ = 1u8.checked_sub(1).unwrap_or(255); // ok | ||
|
||
let _ = 1i32.saturating_add(1); | ||
let _ = 1i32.saturating_add(1); | ||
let _ = 1i8.saturating_add(1); | ||
let _ = 1i128.saturating_add(1); | ||
let _ = 1i32.saturating_add(-1); | ||
let _ = 1i32.saturating_add(-1); | ||
let _ = 1i8.saturating_add(-1); | ||
let _ = 1i128.saturating_add(-1); | ||
let _ = 1i32.checked_add(1).unwrap_or(1234); // ok | ||
let _ = 1i8.checked_add(1).unwrap_or(-128); // ok | ||
let _ = 1i8.checked_add(-1).unwrap_or(127); // ok | ||
|
||
let _ = 1i32.saturating_sub(1); | ||
let _ = 1i32.saturating_sub(1); | ||
let _ = 1i8.saturating_sub(1); | ||
let _ = 1i128.saturating_sub(1); | ||
let _ = 1i32.saturating_sub(-1); | ||
let _ = 1i32.saturating_sub(-1); | ||
let _ = 1i8.saturating_sub(-1); | ||
let _ = 1i128.saturating_sub(-1); | ||
let _ = 1i32.checked_sub(1).unwrap_or(1234); // ok | ||
let _ = 1i8.checked_sub(1).unwrap_or(127); // ok | ||
let _ = 1i8.checked_sub(-1).unwrap_or(-128); // ok | ||
} |
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,54 @@ | ||
// run-rustfix | ||
|
||
#[allow(unused_imports)] // they become unnecessary after fix | ||
use std::{i128, i32, u128, u32}; | ||
|
||
fn main() { | ||
let _ = 1u32.checked_add(1).unwrap_or(u32::max_value()); | ||
let _ = 1u32.checked_add(1).unwrap_or(u32::MAX); | ||
let _ = 1u8.checked_add(1).unwrap_or(255); | ||
let _ = 1u128 | ||
.checked_add(1) | ||
.unwrap_or(340_282_366_920_938_463_463_374_607_431_768_211_455); | ||
let _ = 1u32.checked_add(1).unwrap_or(1234); // ok | ||
let _ = 1u8.checked_add(1).unwrap_or(0); // ok | ||
let _ = 1u32.checked_mul(1).unwrap_or(u32::MAX); | ||
|
||
let _ = 1u32.checked_sub(1).unwrap_or(u32::min_value()); | ||
let _ = 1u32.checked_sub(1).unwrap_or(u32::MIN); | ||
let _ = 1u8.checked_sub(1).unwrap_or(0); | ||
let _ = 1u32.checked_sub(1).unwrap_or(1234); // ok | ||
let _ = 1u8.checked_sub(1).unwrap_or(255); // ok | ||
|
||
let _ = 1i32.checked_add(1).unwrap_or(i32::max_value()); | ||
let _ = 1i32.checked_add(1).unwrap_or(i32::MAX); | ||
let _ = 1i8.checked_add(1).unwrap_or(127); | ||
let _ = 1i128 | ||
.checked_add(1) | ||
.unwrap_or(170_141_183_460_469_231_731_687_303_715_884_105_727); | ||
let _ = 1i32.checked_add(-1).unwrap_or(i32::min_value()); | ||
let _ = 1i32.checked_add(-1).unwrap_or(i32::MIN); | ||
let _ = 1i8.checked_add(-1).unwrap_or(-128); | ||
let _ = 1i128 | ||
.checked_add(-1) | ||
.unwrap_or(-170_141_183_460_469_231_731_687_303_715_884_105_728); | ||
let _ = 1i32.checked_add(1).unwrap_or(1234); // ok | ||
let _ = 1i8.checked_add(1).unwrap_or(-128); // ok | ||
let _ = 1i8.checked_add(-1).unwrap_or(127); // ok | ||
|
||
let _ = 1i32.checked_sub(1).unwrap_or(i32::min_value()); | ||
let _ = 1i32.checked_sub(1).unwrap_or(i32::MIN); | ||
let _ = 1i8.checked_sub(1).unwrap_or(-128); | ||
let _ = 1i128 | ||
.checked_sub(1) | ||
.unwrap_or(-170_141_183_460_469_231_731_687_303_715_884_105_728); | ||
let _ = 1i32.checked_sub(-1).unwrap_or(i32::max_value()); | ||
let _ = 1i32.checked_sub(-1).unwrap_or(i32::MAX); | ||
let _ = 1i8.checked_sub(-1).unwrap_or(127); | ||
let _ = 1i128 | ||
.checked_sub(-1) | ||
.unwrap_or(170_141_183_460_469_231_731_687_303_715_884_105_727); | ||
let _ = 1i32.checked_sub(1).unwrap_or(1234); // ok | ||
let _ = 1i8.checked_sub(1).unwrap_or(127); // ok | ||
let _ = 1i8.checked_sub(-1).unwrap_or(-128); // ok | ||
} |
Oops, something went wrong.