1
1
use clippy_utils:: diagnostics:: span_lint_and_sugg;
2
2
use clippy_utils:: source:: snippet_with_applicability;
3
- use rustc_ast:: ast:: { BinOpKind , Expr , ExprKind , MethodCall , UnOp } ;
4
- use rustc_ast:: token;
3
+ use rustc_ast:: ast:: { BinOpKind , Expr , ExprKind } ;
5
4
use rustc_errors:: Applicability ;
6
5
use rustc_lint:: { EarlyContext , EarlyLintPass } ;
7
6
use rustc_session:: declare_lint_pass;
8
7
use rustc_span:: source_map:: Spanned ;
9
8
10
- const ALLOWED_ODD_FUNCTIONS : [ & str ; 14 ] = [
11
- "asin" ,
12
- "asinh" ,
13
- "atan" ,
14
- "atanh" ,
15
- "cbrt" ,
16
- "fract" ,
17
- "round" ,
18
- "signum" ,
19
- "sin" ,
20
- "sinh" ,
21
- "tan" ,
22
- "tanh" ,
23
- "to_degrees" ,
24
- "to_radians" ,
25
- ] ;
26
-
27
9
declare_clippy_lint ! {
28
10
/// ### What it does
29
11
/// Checks for operations where precedence may be unclear
30
12
/// and suggests to add parentheses. Currently it catches the following:
31
13
/// * mixed usage of arithmetic and bit shifting/combining operators without
32
14
/// parentheses
33
- /// * a "negative" numeric literal (which is really a unary `-` followed by a
34
- /// numeric literal)
35
- /// followed by a method call
36
15
///
37
16
/// ### Why is this bad?
38
17
/// Not everyone knows the precedence of those operators by
@@ -41,7 +20,6 @@ declare_clippy_lint! {
41
20
///
42
21
/// ### Example
43
22
/// * `1 << 2 + 3` equals 32, while `(1 << 2) + 3` equals 7
44
- /// * `-1i32.abs()` equals -1, while `(-1i32).abs()` equals 1
45
23
#[ clippy:: version = "pre 1.29.0" ]
46
24
pub PRECEDENCE ,
47
25
complexity,
@@ -104,38 +82,6 @@ impl EarlyLintPass for Precedence {
104
82
( false , false ) => ( ) ,
105
83
}
106
84
}
107
-
108
- if let ExprKind :: Unary ( UnOp :: Neg , operand) = & expr. kind {
109
- let mut arg = operand;
110
-
111
- let mut all_odd = true ;
112
- while let ExprKind :: MethodCall ( box MethodCall { seg, receiver, .. } ) = & arg. kind {
113
- let seg_str = seg. ident . name . as_str ( ) ;
114
- all_odd &= ALLOWED_ODD_FUNCTIONS
115
- . iter ( )
116
- . any ( |odd_function| * * odd_function == * seg_str) ;
117
- arg = receiver;
118
- }
119
-
120
- if !all_odd
121
- && let ExprKind :: Lit ( lit) = & arg. kind
122
- && let token:: LitKind :: Integer | token:: LitKind :: Float = & lit. kind
123
- {
124
- let mut applicability = Applicability :: MachineApplicable ;
125
- span_lint_and_sugg (
126
- cx,
127
- PRECEDENCE ,
128
- expr. span ,
129
- "unary minus has lower precedence than method call" ,
130
- "consider adding parentheses to clarify your intent" ,
131
- format ! (
132
- "-({})" ,
133
- snippet_with_applicability( cx, operand. span, ".." , & mut applicability)
134
- ) ,
135
- applicability,
136
- ) ;
137
- }
138
- }
139
85
}
140
86
}
141
87
0 commit comments