-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Linting classical overflow checks #741
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,73 @@ | ||
#![allow(cyclomatic_complexity)] | ||
use rustc::lint::*; | ||
use rustc_front::hir::*; | ||
use utils::{span_lint}; | ||
|
||
/// **What it does:** This lint finds classic underflow / overflow checks. | ||
/// | ||
/// **Why is this bad?** Most classic C underflow / 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 { | ||
// a + b < a, a > a + b, a < a - b, a - b > a | ||
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) { | ||
if_let_chain! {[ | ||
let Expr_::ExprBinary(ref op, ref first, ref second) = expr.node, | ||
let Expr_::ExprBinary(ref op2, ref ident1, ref ident2) = first.node, | ||
let Expr_::ExprPath(_,ref path1) = ident1.node, | ||
let Expr_::ExprPath(_, ref path2) = ident2.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(ident1).is_integral(), | ||
cx.tcx.expr_ty(ident2).is_integral() | ||
], { | ||
if let BinOp_::BiLt = op.node { | ||
if let BinOp_::BiAdd = op2.node { | ||
span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C overflow conditons that will fail in Rust."); | ||
} | ||
} | ||
if let BinOp_::BiGt = op.node { | ||
if let BinOp_::BiSub = op2.node { | ||
span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C underflow conditons that will fail in Rust."); | ||
} | ||
} | ||
}} | ||
|
||
if_let_chain! {[ | ||
let Expr_::ExprBinary(ref op, ref first, ref second) = expr.node, | ||
let Expr_::ExprBinary(ref op2, ref ident1, ref ident2) = second.node, | ||
let Expr_::ExprPath(_,ref path1) = ident1.node, | ||
let Expr_::ExprPath(_, ref path2) = ident2.node, | ||
let Expr_::ExprPath(_, ref path3) = first.node, | ||
(&path1.segments[0]).identifier == (&path3.segments[0]).identifier || (&path2.segments[0]).identifier == (&path3.segments[0]).identifier, | ||
cx.tcx.expr_ty(ident1).is_integral(), | ||
cx.tcx.expr_ty(ident2).is_integral() | ||
], { | ||
if let BinOp_::BiGt = op.node { | ||
if let BinOp_::BiAdd = op2.node { | ||
span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C overflow conditons that will fail in Rust."); | ||
} | ||
} | ||
if let BinOp_::BiLt = op.node { | ||
if let BinOp_::BiSub = op2.node { | ||
span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C underflow 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,61 @@ | ||
#![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 > a + b { //~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 b > a + b { //~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 underflow conditons that will fail in Rust. | ||
|
||
} | ||
if b < a - b { //~ERROR You are trying to use classic C underflow conditons that will fail in Rust. | ||
|
||
} | ||
if a - b > a { //~ERROR You are trying to use classic C underflow conditons that will fail in Rust. | ||
|
||
} | ||
if a < a - b { //~ERROR You are trying to use classic C underflow conditons that will fail in Rust. | ||
|
||
} | ||
if a + b < c { | ||
|
||
} | ||
if c > a + b { | ||
|
||
} | ||
if a - b < c { | ||
|
||
} | ||
if c > a - b { | ||
|
||
} | ||
let i = 1.1; | ||
let j = 2.2; | ||
if i + j < i { | ||
|
||
} | ||
if i - j < i { | ||
|
||
} | ||
if i > i + j { | ||
|
||
} | ||
if i - j < i { | ||
|
||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We you write a test, add things that are similar but should not lint, eg.
a + b < c
. Alsoa + b < b
should lint as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a + b < c
should lint as well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, but you should add it in the test file to ensure it does not. (the test suite checks that only lines with a
//~ERROR
trigger errors, so just add something witha + b < c
with no such comment)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh alright, will do it in sometime 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see, I meant « Also
a + b < b
should lint as well. » 😄There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mcarton yeah was talking about that 😄 Getting this error while compiling https://pastebin.mozilla.org/8862494. Should I remove
use syntax::ast::*;
completely? What's the difference between the two?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rustc has several types of AST for different step of compilation. The
ast::*
stuff is just forEarlyLintPass
s and has no type information yet, you need to usehir::*
stuffs and aLateLintPass
which use slightly different names for everything since a few weeks (usually whenast
hasFooKind::Bar
,hir
hasFooBar
). You won’t needast::*
stuffs here.