From 7a8de35abc32478fa84406b0939137333fe9a6b5 Mon Sep 17 00:00:00 2001 From: llogiq Date: Wed, 6 May 2015 13:20:47 +0200 Subject: [PATCH 1/2] refactored precedence lint --- src/misc.rs | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/misc.rs b/src/misc.rs index eae6d518950a..5059a87bccff 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -183,23 +183,21 @@ impl LintPass for Precedence { fn check_expr(&mut self, cx: &Context, expr: &Expr) { if let ExprBinary(Spanned { node: op, ..}, ref left, ref right) = expr.node { - if is_bit_op(op) { - if let ExprBinary(Spanned { node: lop, ..}, _, _) = left.node { - if is_arith_op(lop) { - cx.span_lint(PRECEDENCE, expr.span, "Operator precedence can trip the unwary. Please consider adding parenthesis to the subexpression to make the meaning more clear."); - } - } else { - if let ExprBinary(Spanned { node: rop, ..}, _, _) = right.node { - if is_arith_op(rop) { - cx.span_lint(PRECEDENCE, expr.span, "Operator precedence can trip the unwary. Please consider adding parenthesis to the subexpression to make the meaning more clear."); - } - } - } + if is_bit_op(op) && (is_arith_expr(left) || is_arith_expr(right)) { + cx.span_lint(PRECEDENCE, expr.span, + "Operator precedence can trip the unwary. Consider adding parenthesis to the subexpression."); } } } } +fn is_arith_expr(expr : &Expr) -> bool { + match expr.node { + ExprBinary(Spanned { node: op, ..}, _, _) => is_arith_op(lop), + _ => false + } +} + fn is_bit_op(op : BinOp_) -> bool { match op { BiBitXor | BiBitAnd | BiBitOr | BiShl | BiShr => true, From 23525081f951c92e4c8ad0b0bfed7a2b03c0531e Mon Sep 17 00:00:00 2001 From: llogiq Date: Wed, 6 May 2015 14:19:02 +0200 Subject: [PATCH 2/2] fixed typo from last commit --- src/misc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/misc.rs b/src/misc.rs index 5059a87bccff..8cde5104ff75 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -193,7 +193,7 @@ impl LintPass for Precedence { fn is_arith_expr(expr : &Expr) -> bool { match expr.node { - ExprBinary(Spanned { node: op, ..}, _, _) => is_arith_op(lop), + ExprBinary(Spanned { node: op, ..}, _, _) => is_arith_op(op), _ => false } }