1
1
use rustc:: lint:: * ;
2
2
use syntax:: ast:: * ;
3
3
use syntax:: codemap:: Spanned ;
4
- use utils:: { span_lint , snippet} ;
4
+ use utils:: { span_lint_and_then , snippet} ;
5
5
6
6
/// **What it does:** Checks for operations where precedence may be unclear
7
7
/// and suggests to add parentheses. Currently it catches the following:
@@ -36,41 +36,39 @@ impl LintPass for Precedence {
36
36
impl EarlyLintPass for Precedence {
37
37
fn check_expr ( & mut self , cx : & EarlyContext , expr : & Expr ) {
38
38
if let ExprKind :: Binary ( Spanned { node : op, .. } , ref left, ref right) = expr. node {
39
+ let span_sugg =
40
+ |expr : & Expr , sugg| {
41
+ span_lint_and_then ( cx, PRECEDENCE , expr. span , "operator precedence can trip the unwary" , |db| {
42
+ db. span_suggestion ( expr. span , "consider parenthesizing your expression" , sugg) ;
43
+ } ) ;
44
+ } ;
45
+
39
46
if !is_bit_op ( op) {
40
47
return ;
41
48
}
42
49
match ( is_arith_expr ( left) , is_arith_expr ( right) ) {
43
50
( true , true ) => {
44
- span_lint ( cx,
45
- PRECEDENCE ,
46
- expr. span ,
47
- & format ! ( "operator precedence can trip the unwary. Consider parenthesizing your \
48
- expression:`({}) {} ({})`",
51
+ let sugg = format ! ( "({}) {} ({})" ,
49
52
snippet( cx, left. span, ".." ) ,
50
53
op. to_string( ) ,
51
- snippet( cx, right. span, ".." ) ) ) ;
54
+ snippet( cx, right. span, ".." ) ) ;
55
+ span_sugg ( expr, sugg) ;
52
56
} ,
53
57
( true , false ) => {
54
- span_lint ( cx,
55
- PRECEDENCE ,
56
- expr. span ,
57
- & format ! ( "operator precedence can trip the unwary. Consider parenthesizing your \
58
- expression:`({}) {} {}`",
58
+ let sugg = format ! ( "({}) {} {}" ,
59
59
snippet( cx, left. span, ".." ) ,
60
60
op. to_string( ) ,
61
- snippet( cx, right. span, ".." ) ) ) ;
61
+ snippet( cx, right. span, ".." ) ) ;
62
+ span_sugg ( expr, sugg) ;
62
63
} ,
63
64
( false , true ) => {
64
- span_lint ( cx,
65
- PRECEDENCE ,
66
- expr. span ,
67
- & format ! ( "operator precedence can trip the unwary. Consider parenthesizing your \
68
- expression:`{} {} ({})`",
65
+ let sugg = format ! ( "{} {} ({})" ,
69
66
snippet( cx, left. span, ".." ) ,
70
67
op. to_string( ) ,
71
- snippet( cx, right. span, ".." ) ) ) ;
68
+ snippet( cx, right. span, ".." ) ) ;
69
+ span_sugg ( expr, sugg) ;
72
70
} ,
73
- _ => ( ) ,
71
+ ( false , false ) => ( ) ,
74
72
}
75
73
}
76
74
@@ -82,12 +80,15 @@ impl EarlyLintPass for Precedence {
82
80
LitKind :: Int ( ..) |
83
81
LitKind :: Float ( ..) |
84
82
LitKind :: FloatUnsuffixed ( ..) => {
85
- span_lint ( cx,
86
- PRECEDENCE ,
87
- expr. span ,
88
- & format ! ( "unary minus has lower precedence than method call. Consider \
89
- adding parentheses to clarify your intent: -({})",
90
- snippet( cx, rhs. span, ".." ) ) ) ;
83
+ span_lint_and_then ( cx,
84
+ PRECEDENCE ,
85
+ expr. span ,
86
+ "unary minus has lower precedence than method call" ,
87
+ |db| {
88
+ db. span_suggestion ( expr. span ,
89
+ "consider adding parentheses to clarify your intent" ,
90
+ format ! ( "-({})" , snippet( cx, rhs. span, ".." ) ) ) ;
91
+ } ) ;
91
92
} ,
92
93
_ => ( ) ,
93
94
}
0 commit comments