1 use rustc::plugin::Registry;
3 use rustc::middle::const_eval::lookup_const_by_id;
4 use rustc::middle::def::*;
6 use syntax::ast_util::{is_comparison_binop, binop_to_string};
8 use syntax::codemap::Span;
10 use utils::{span_lint, snippet};
12 declare_lint! { pub IDENTITY_OP, Warn,
13 "Warn on identity operations, e.g. '_ + 0'"}
16 pub struct IdentityOp;
18 impl LintPass for IdentityOp {
19 fn get_lints(&self) -> LintArray {
20 lint_array!(IDENTITY_OP)
23 fn check_expr(&mut self, cx: &Context, e: &Expr) {
24 if let ExprBinary(ref cmp, ref left, ref right) = e.node {
26 BiAdd | BiBitOr | BiBitXor => {
27 check(cx, left, 0, e.span, right.span);
28 check(cx, right, 0, e.span, left.span);
30 BiShl | BiShr | BiSub =>
31 check(cx, right, 0, e.span, left.span),
33 check(cx, left, 1, e.span, right.span);
34 check(cx, right, 1, e.span, left.span);
37 check(cx, right, 1, e.span, left.span),
39 check(cx, left, -1, e.span, right.span);
40 check(cx, right, -1, e.span, left.span);
49 fn check(cx: &Context, e: &Expr, m: i8, span: Span, arg: Span) {
50 if have_lit(cx, e, m) {
51 span_lint(cx, IDENTITY_OP, span, &format!(
52 "The operation is ineffective. Consider reducing it to '{}'",
53 snippet(cx, arg, "..")));
57 fn have_lit(cx: &Context, e : &Expr, m: i8) -> bool {
59 &ExprUnary(UnNeg, ref litexp) => have_lit(cx, litexp, -m),
60 &ExprLit(ref lit) => {
61 match (&lit.node, m) {
62 (&LitInt(0, _), 0) => true,
63 (&LitInt(1, SignedIntLit(_, Plus)), 1) => true,
64 (&LitInt(1, UnsuffixedIntLit(Plus)), 1) => true,
65 (&LitInt(1, SignedIntLit(_, Minus)), -1) => true,
66 (&LitInt(1, UnsuffixedIntLit(Minus)), -1) => true,
70 &ExprParen(ref p) => have_lit(cx, p, m),
72 match cx.tcx.def_map.borrow().get(&e.id) {
73 Some(&PathResolution { base_def: DefConst(id), ..}) =>
74 lookup_const_by_id(cx.tcx, id, Option::None)
75 .map_or(false, |l| have_lit(cx, l, m)),