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
1 parent
88ac226
commit 55fbf59
Showing
4 changed files
with
71 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use rustc::lint::*; | ||
use rustc_front::hir::*; | ||
use utils::{span_lint}; | ||
|
||
/// **What it does:** This lint finds classic overflow checks. | ||
/// | ||
/// **Why is this bad?** Most classic C overflow checks will fail in Rust. Users can use functions like `overflowing_*` and `wrapping_*` instead. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** `a + b < a` | ||
declare_lint!(pub OVERFLOW_CHECK_CONDITIONAL, Warn, | ||
"Using overflow checks which are likely to panic"); | ||
|
||
#[derive(Copy, Clone)] | ||
pub struct OverflowCheckConditional; | ||
|
||
impl LintPass for OverflowCheckConditional { | ||
fn get_lints(&self) -> LintArray { | ||
lint_array!(OVERFLOW_CHECK_CONDITIONAL) | ||
} | ||
} | ||
|
||
impl LateLintPass for OverflowCheckConditional { | ||
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) { | ||
if_let_chain! {[ | ||
let Expr_::ExprBinary(ref op, ref first, ref second) = expr.node, | ||
let BinOp_::BiLt = op.node, | ||
let Expr_::ExprBinary(ref op2, ref add1, ref add2) = first.node, | ||
let BinOp_::BiAdd = op2.node, | ||
let Expr_::ExprPath(_,ref path1) = add1.node, | ||
let Expr_::ExprPath(_, ref path2) = add2.node, | ||
let Expr_::ExprPath(_, ref path3) = second.node, | ||
(&path1.segments[0]).identifier == (&path3.segments[0]).identifier || (&path2.segments[0]).identifier == (&path3.segments[0]).identifier, | ||
cx.tcx.expr_ty(add1).is_integral(), | ||
cx.tcx.expr_ty(add2).is_integral() | ||
], { | ||
span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C overflow conditons that will fail in Rust."); | ||
}} | ||
} | ||
} |
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,25 @@ | ||
#![feature(plugin)] | ||
#![plugin(clippy)] | ||
|
||
#![deny(overflow_check_conditional)] | ||
|
||
fn main() { | ||
let a: u32 = 1; | ||
let b: u32 = 2; | ||
let c: u32 = 3; | ||
if a + b < a { //~ERROR You are trying to use classic C overflow conditons that will fail in Rust. | ||
|
||
} | ||
if a + b < b { //~ERROR You are trying to use classic C overflow conditons that will fail in Rust. | ||
|
||
} | ||
if a + b < c { | ||
|
||
} | ||
let i = 1.1; | ||
let j = 2.2; | ||
if i + j < i { | ||
|
||
} | ||
} | ||
|