diff --git a/clippy_lints/src/precedence.rs b/clippy_lints/src/precedence.rs index e06c571b6f6..5d56a927bc0 100644 --- a/clippy_lints/src/precedence.rs +++ b/clippy_lints/src/precedence.rs @@ -1,7 +1,7 @@ use rustc::lint::*; use syntax::ast::*; use syntax::codemap::Spanned; -use utils::{snippet, span_lint_and_sugg}; +use utils::{in_macro, snippet, span_lint_and_sugg}; /// **What it does:** Checks for operations where precedence may be unclear /// and suggests to add parentheses. Currently it catches the following: @@ -37,6 +37,10 @@ impl LintPass for Precedence { impl EarlyLintPass for Precedence { fn check_expr(&mut self, cx: &EarlyContext, expr: &Expr) { + if in_macro(expr.span) { + return; + } + if let ExprKind::Binary(Spanned { node: op, .. }, ref left, ref right) = expr.node { let span_sugg = |expr: &Expr, sugg| { span_lint_and_sugg( diff --git a/tests/ui/precedence.rs b/tests/ui/precedence.rs index 720637c94b5..aacd90cdf92 100644 --- a/tests/ui/precedence.rs +++ b/tests/ui/precedence.rs @@ -4,6 +4,16 @@ #[warn(precedence)] #[allow(identity_op)] #[allow(eq_op)] + +macro_rules! trip { + ($a:expr) => { + match $a & 0b1111_1111i8 { + 0 => println!("a is zero ({})", $a), + _ => println!("a is {}", $a), + } + }; +} + fn main() { 1 << 2 + 3; 1 + 2 << 3; @@ -22,4 +32,7 @@ fn main() { let _ = -(1f32).abs(); let _ = -(1i32.abs()); let _ = -(1f32.abs()); + + let b = 3; + trip!(b * 8); } diff --git a/tests/ui/precedence.stderr b/tests/ui/precedence.stderr index 26fbd75164d..768983ac35a 100644 --- a/tests/ui/precedence.stderr +++ b/tests/ui/precedence.stderr @@ -1,56 +1,56 @@ error: operator precedence can trip the unwary - --> $DIR/precedence.rs:8:5 - | -8 | 1 << 2 + 3; - | ^^^^^^^^^^ help: consider parenthesizing your expression: `1 << (2 + 3)` - | - = note: `-D precedence` implied by `-D warnings` - -error: operator precedence can trip the unwary - --> $DIR/precedence.rs:9:5 - | -9 | 1 + 2 << 3; - | ^^^^^^^^^^ help: consider parenthesizing your expression: `(1 + 2) << 3` - -error: operator precedence can trip the unwary - --> $DIR/precedence.rs:10:5 + --> $DIR/precedence.rs:18:5 | -10 | 4 >> 1 + 1; +18 | 1 << 2 + 3; + | ^^^^^^^^^^ help: consider parenthesizing your expression: `1 << (2 + 3)` + | + = note: `-D precedence` implied by `-D warnings` + +error: operator precedence can trip the unwary + --> $DIR/precedence.rs:19:5 + | +19 | 1 + 2 << 3; + | ^^^^^^^^^^ help: consider parenthesizing your expression: `(1 + 2) << 3` + +error: operator precedence can trip the unwary + --> $DIR/precedence.rs:20:5 + | +20 | 4 >> 1 + 1; | ^^^^^^^^^^ help: consider parenthesizing your expression: `4 >> (1 + 1)` error: operator precedence can trip the unwary - --> $DIR/precedence.rs:11:5 + --> $DIR/precedence.rs:21:5 | -11 | 1 + 3 >> 2; +21 | 1 + 3 >> 2; | ^^^^^^^^^^ help: consider parenthesizing your expression: `(1 + 3) >> 2` error: operator precedence can trip the unwary - --> $DIR/precedence.rs:12:5 + --> $DIR/precedence.rs:22:5 | -12 | 1 ^ 1 - 1; +22 | 1 ^ 1 - 1; | ^^^^^^^^^ help: consider parenthesizing your expression: `1 ^ (1 - 1)` error: operator precedence can trip the unwary - --> $DIR/precedence.rs:13:5 + --> $DIR/precedence.rs:23:5 | -13 | 3 | 2 - 1; +23 | 3 | 2 - 1; | ^^^^^^^^^ help: consider parenthesizing your expression: `3 | (2 - 1)` error: operator precedence can trip the unwary - --> $DIR/precedence.rs:14:5 + --> $DIR/precedence.rs:24:5 | -14 | 3 & 5 - 2; +24 | 3 & 5 - 2; | ^^^^^^^^^ help: consider parenthesizing your expression: `3 & (5 - 2)` error: unary minus has lower precedence than method call - --> $DIR/precedence.rs:15:5 + --> $DIR/precedence.rs:25:5 | -15 | -1i32.abs(); +25 | -1i32.abs(); | ^^^^^^^^^^^ help: consider adding parentheses to clarify your intent: `-(1i32.abs())` error: unary minus has lower precedence than method call - --> $DIR/precedence.rs:16:5 + --> $DIR/precedence.rs:26:5 | -16 | -1f32.abs(); +26 | -1f32.abs(); | ^^^^^^^^^^^ help: consider adding parentheses to clarify your intent: `-(1f32.abs())`