declare_lint_pass!(OverflowCheckConditional => [OVERFLOW_CHECK_CONDITIONAL]);
+const OVERFLOW_MSG: &str = "you are trying to use classic C overflow conditions that will fail in Rust";
+const UNDERFLOW_MSG: &str = "you are trying to use classic C underflow conditions that will fail in Rust";
+
impl<'tcx> LateLintPass<'tcx> for OverflowCheckConditional {
// a + b < a, a > a + b, a < a - b, a - b > a
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
if cx.typeck_results().expr_ty(ident1).is_integral();
if cx.typeck_results().expr_ty(ident2).is_integral();
then {
- if let BinOpKind::Lt = op.node {
- if let BinOpKind::Add = op2.node {
- span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
- "you are trying to use classic C overflow conditions that will fail in Rust");
- }
+ if op.node == BinOpKind::Lt && op2.node == BinOpKind::Add {
+ span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, OVERFLOW_MSG);
}
- if let BinOpKind::Gt = op.node {
- if let BinOpKind::Sub = op2.node {
- span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
- "you are trying to use classic C underflow conditions that will fail in Rust");
- }
+ if op.node == BinOpKind::Gt && op2.node == BinOpKind::Sub {
+ span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, UNDERFLOW_MSG);
}
}
}
if cx.typeck_results().expr_ty(ident1).is_integral();
if cx.typeck_results().expr_ty(ident2).is_integral();
then {
- if let BinOpKind::Gt = op.node {
- if let BinOpKind::Add = op2.node {
- span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
- "you are trying to use classic C overflow conditions that will fail in Rust");
- }
+ if op.node == BinOpKind::Gt && op2.node == BinOpKind::Add {
+ span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, OVERFLOW_MSG);
}
- if let BinOpKind::Lt = op.node {
- if let BinOpKind::Sub = op2.node {
- span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
- "you are trying to use classic C underflow conditions that will fail in Rust");
- }
+ if op.node == BinOpKind::Lt && op2.node == BinOpKind::Sub {
+ span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, UNDERFLOW_MSG);
}
}
}