Skip to content
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 3 commits into from
Mar 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ A collection of lints to catch common mistakes and improve your Rust code.
[Jump to usage instructions](#usage)

##Lints
There are 130 lints included in this crate:
There are 131 lints included in this crate:

name | default | meaning
---------------------------------------------------------------------------------------------------------------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -92,6 +92,7 @@ name
[option_unwrap_used](https://github.com/Manishearth/rust-clippy/wiki#option_unwrap_used) | allow | using `Option.unwrap()`, which should at least get a better message using `expect()`
[or_fun_call](https://github.com/Manishearth/rust-clippy/wiki#or_fun_call) | warn | using any `*or` method when the `*or_else` would do
[out_of_bounds_indexing](https://github.com/Manishearth/rust-clippy/wiki#out_of_bounds_indexing) | deny | out of bound constant indexing
[overflow_check_conditional](https://github.com/Manishearth/rust-clippy/wiki#overflow_check_conditional) | warn | Using overflow checks which are likely to panic
[panic_params](https://github.com/Manishearth/rust-clippy/wiki#panic_params) | warn | missing parameters in `panic!`
[precedence](https://github.com/Manishearth/rust-clippy/wiki#precedence) | warn | catches operations where precedence may be unclear. See the wiki for a list of cases caught
[print_stdout](https://github.com/Manishearth/rust-clippy/wiki#print_stdout) | allow | printing on stdout
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub mod needless_features;
pub mod needless_update;
pub mod no_effect;
pub mod open_options;
pub mod overflow_check_conditional;
pub mod panic;
pub mod precedence;
pub mod print;
Expand Down Expand Up @@ -173,6 +174,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
reg.register_early_lint_pass(box formatting::Formatting);
reg.register_late_lint_pass(box swap::Swap);
reg.register_early_lint_pass(box if_not_else::IfNotElse);
reg.register_late_lint_pass(box overflow_check_conditional::OverflowCheckConditional);

reg.register_lint_group("clippy_pedantic", vec![
enum_glob_use::ENUM_GLOB_USE,
Expand Down Expand Up @@ -283,6 +285,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
needless_update::NEEDLESS_UPDATE,
no_effect::NO_EFFECT,
open_options::NONSENSICAL_OPEN_OPTIONS,
overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL,
panic::PANIC_PARAMS,
precedence::PRECEDENCE,
ptr_arg::PTR_ARG,
Expand Down
73 changes: 73 additions & 0 deletions src/overflow_check_conditional.rs
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.");
}
}
}}
}
}
61 changes: 61 additions & 0 deletions tests/compile-fail/overflow_check_conditional.rs
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 {

}
Copy link
Member

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. Also a + b < b should lint as well.

Copy link
Contributor Author

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?

Copy link
Member

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 with a + b < c with no such comment)

Copy link
Contributor Author

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 😄

Copy link
Member

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. » 😄

Copy link
Contributor Author

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?

Copy link
Member

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 for EarlyLintPasss and has no type information yet, you need to use hir::* stuffs and a LateLintPass which use slightly different names for everything since a few weeks (usually when ast has FooKind::Bar, hir has FooBar). You won’t need ast::* stuffs here.

}