2015-05-15 11:46:43 -05:00
|
|
|
use rustc::lint::*;
|
2015-09-03 09:42:17 -05:00
|
|
|
use rustc_front::hir::*;
|
2015-05-15 11:46:43 -05:00
|
|
|
use syntax::codemap::Span;
|
|
|
|
|
2016-02-01 05:51:33 -06:00
|
|
|
use consts::{constant_simple, is_negative, Constant};
|
2015-09-06 03:53:55 -05:00
|
|
|
use utils::{span_lint, snippet, in_macro};
|
2015-07-09 10:02:21 -05:00
|
|
|
|
2016-02-05 17:41:54 -06:00
|
|
|
/// **What it does:** This lint checks for identity operations, e.g. `x + 0`.
|
2015-12-10 18:22:27 -06:00
|
|
|
///
|
|
|
|
/// **Why is this bad?** This code can be removed without changing the meaning. So it just obscures what's going on. Delete it mercilessly.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None
|
|
|
|
///
|
|
|
|
/// **Example:** `x / 1 + 0 * 1 - 0 | 0`
|
2016-02-05 17:13:29 -06:00
|
|
|
declare_lint! {
|
|
|
|
pub IDENTITY_OP, Warn,
|
|
|
|
"using identity operations, e.g. `x + 0` or `y / 1`"
|
|
|
|
}
|
2015-08-11 13:22:20 -05:00
|
|
|
|
2015-05-15 11:46:43 -05:00
|
|
|
#[derive(Copy,Clone)]
|
|
|
|
pub struct IdentityOp;
|
|
|
|
|
|
|
|
impl LintPass for IdentityOp {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(IDENTITY_OP)
|
|
|
|
}
|
2015-09-18 21:53:04 -05:00
|
|
|
}
|
2015-05-22 17:49:13 -05:00
|
|
|
|
2015-09-18 21:53:04 -05:00
|
|
|
impl LateLintPass for IdentityOp {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
|
2016-01-03 22:26:12 -06:00
|
|
|
if in_macro(cx, e.span) {
|
|
|
|
return;
|
|
|
|
}
|
2015-05-22 17:49:13 -05:00
|
|
|
if let ExprBinary(ref cmp, ref left, ref right) = e.node {
|
|
|
|
match cmp.node {
|
|
|
|
BiAdd | BiBitOr | BiBitXor => {
|
|
|
|
check(cx, left, 0, e.span, right.span);
|
|
|
|
check(cx, right, 0, e.span, left.span);
|
2015-11-16 22:39:42 -06:00
|
|
|
}
|
2016-01-03 22:26:12 -06:00
|
|
|
BiShl | BiShr | BiSub => check(cx, right, 0, e.span, left.span),
|
2015-05-22 17:49:13 -05:00
|
|
|
BiMul => {
|
|
|
|
check(cx, left, 1, e.span, right.span);
|
|
|
|
check(cx, right, 1, e.span, left.span);
|
2015-11-16 22:39:42 -06:00
|
|
|
}
|
2016-01-03 22:26:12 -06:00
|
|
|
BiDiv => check(cx, right, 1, e.span, left.span),
|
2015-05-22 17:49:13 -05:00
|
|
|
BiBitAnd => {
|
|
|
|
check(cx, left, -1, e.span, right.span);
|
|
|
|
check(cx, right, -1, e.span, left.span);
|
2015-11-16 22:39:42 -06:00
|
|
|
}
|
2016-01-03 22:26:12 -06:00
|
|
|
_ => (),
|
2015-05-22 17:49:13 -05:00
|
|
|
}
|
|
|
|
}
|
2015-05-15 11:46:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-18 21:53:04 -05:00
|
|
|
fn check(cx: &LateContext, e: &Expr, m: i8, span: Span, arg: Span) {
|
2016-02-01 05:51:33 -06:00
|
|
|
if let Some(Constant::Int(v, ty)) = constant_simple(e) {
|
2015-09-06 03:53:55 -05:00
|
|
|
if match m {
|
|
|
|
0 => v == 0,
|
|
|
|
-1 => is_negative(ty) && v == 1,
|
|
|
|
1 => !is_negative(ty) && v == 1,
|
|
|
|
_ => unreachable!(),
|
|
|
|
} {
|
2016-01-03 22:26:12 -06:00
|
|
|
span_lint(cx,
|
|
|
|
IDENTITY_OP,
|
|
|
|
span,
|
|
|
|
&format!("the operation is ineffective. Consider reducing it to `{}`",
|
|
|
|
snippet(cx, arg, "..")));
|
2015-08-16 16:09:56 -05:00
|
|
|
}
|
2015-05-22 17:49:13 -05:00
|
|
|
}
|
2015-05-15 11:46:43 -05:00
|
|
|
}
|