]> git.lizzy.rs Git - rust.git/commitdiff
improved precedence messages (fixes #389)
authorAndre Bogus <bogusandre@gmail.com>
Tue, 13 Oct 2015 11:48:48 +0000 (13:48 +0200)
committerAndre Bogus <bogusandre@gmail.com>
Tue, 13 Oct 2015 11:59:20 +0000 (13:59 +0200)
src/precedence.rs

index ce06278b78208880fa4fd507404af9c7a14238b7..b7dbe26855733dfd70bd8b06bbab9b6273aa9fdd 100644 (file)
@@ -1,7 +1,9 @@
 use rustc::lint::*;
 use syntax::codemap::Spanned;
 use syntax::ast::*;
-use utils::span_lint;
+use syntax::ast_util::binop_to_string;
+
+use utils::{span_lint, snippet};
 
 declare_lint!(pub PRECEDENCE, Warn,
               "catches operations where precedence may be unclear. See the wiki for a \
@@ -19,10 +21,24 @@ fn get_lints(&self) -> LintArray {
 impl EarlyLintPass for Precedence {
     fn check_expr(&mut self, cx: &EarlyContext, expr: &Expr) {
         if let ExprBinary(Spanned { node: op, ..}, ref left, ref right) = expr.node {
-            if is_bit_op(op) && (is_arith_expr(left) || is_arith_expr(right)) {
-                span_lint(cx, PRECEDENCE, expr.span,
-                    "operator precedence can trip the unwary. Consider adding parentheses \
-                     to the subexpression");
+            if !is_bit_op(op) { return; }
+            match (is_arith_expr(left), is_arith_expr(right)) {
+                (true, true) =>  span_lint(cx, PRECEDENCE, expr.span, 
+                    &format!("operator precedence can trip the unwary. \
+                         Consider parenthesizing your expression:\
+                         `({}) {} ({})`", snippet(cx, left.span, ".."),
+                         binop_to_string(op), snippet(cx, right.span, ".."))),
+                (true, false) => span_lint(cx, PRECEDENCE, expr.span, 
+                    &format!("operator precedence can trip the unwary. \
+                         Consider parenthesizing your expression:\
+                         `({}) {} {}`", snippet(cx, left.span, ".."),
+                         binop_to_string(op), snippet(cx, right.span, ".."))),
+                (false, true) => span_lint(cx, PRECEDENCE, expr.span, 
+                    &format!("operator precedence can trip the unwary. \
+                         Consider parenthesizing your expression:\
+                         `{} {} ({})`", snippet(cx, left.span, ".."),
+                         binop_to_string(op), snippet(cx, right.span, ".."))),
+                _ => (),
             }
         }
 
@@ -32,9 +48,11 @@ fn check_expr(&mut self, cx: &EarlyContext, expr: &Expr) {
                     if let ExprLit(ref lit) = slf.node {
                         match lit.node {
                             LitInt(..) | LitFloat(..) | LitFloatUnsuffixed(..) =>
-                                span_lint(cx, PRECEDENCE, expr.span,
-                                    "unary minus has lower precedence than method call. Consider \
-                                     adding parentheses to clarify your intent"),
+                                span_lint(cx, PRECEDENCE, expr.span, &format!(
+                                    "unary minus has lower precedence than \
+                                     method call. Consider adding parentheses \
+                                     to clarify your intent: -({})",
+                                     snippet(cx, rhs.span, ".."))),
                                 _ => ()
                         }
                     }