Merge pull request #2364 from flip1995/precedence

Add macro check to precedence lint
This commit is contained in:
Oliver Schneider 2018-01-19 12:07:00 +01:00 committed by GitHub
commit 9a2c50f3d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 29 deletions

View file

@ -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(

View file

@ -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);
}

View file

@ -1,57 +1,57 @@
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())`
error: aborting due to 9 previous errors