@@ -3,61 +3,40 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
3
3
use clippy_utils:: source:: snippet_with_applicability;
4
4
use clippy_utils:: { clip, unsext} ;
5
5
use rustc_errors:: Applicability ;
6
- use rustc_hir:: { BinOp , BinOpKind , Expr , ExprKind , Node } ;
7
- use rustc_lint:: { LateContext , LateLintPass } ;
6
+ use rustc_hir:: { BinOpKind , Expr , ExprKind , Node } ;
7
+ use rustc_lint:: LateContext ;
8
8
use rustc_middle:: ty;
9
- use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
10
9
use rustc_span:: source_map:: Span ;
11
10
12
- declare_clippy_lint ! {
13
- /// ### What it does
14
- /// Checks for identity operations, e.g., `x + 0`.
15
- ///
16
- /// ### Why is this bad?
17
- /// This code can be removed without changing the
18
- /// meaning. So it just obscures what's going on. Delete it mercilessly.
19
- ///
20
- /// ### Example
21
- /// ```rust
22
- /// # let x = 1;
23
- /// x / 1 + 0 * 1 - 0 | 0;
24
- /// ```
25
- #[ clippy:: version = "pre 1.29.0" ]
26
- pub IDENTITY_OP ,
27
- complexity,
28
- "using identity operations, e.g., `x + 0` or `y / 1`"
29
- }
30
-
31
- declare_lint_pass ! ( IdentityOp => [ IDENTITY_OP ] ) ;
11
+ use super :: IDENTITY_OP ;
32
12
33
- impl < ' tcx > LateLintPass < ' tcx > for IdentityOp {
34
- fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
35
- if expr. span . from_expansion ( ) {
36
- return ;
37
- }
38
- if let ExprKind :: Binary ( cmp, left, right) = & expr. kind {
39
- if !is_allowed ( cx, * cmp, left, right) {
40
- match cmp. node {
41
- BinOpKind :: Add | BinOpKind :: BitOr | BinOpKind :: BitXor => {
42
- check ( cx, left, 0 , expr. span , right. span , needs_parenthesis ( cx, expr, right) ) ;
43
- check ( cx, right, 0 , expr. span , left. span , Parens :: Unneeded ) ;
44
- } ,
45
- BinOpKind :: Shl | BinOpKind :: Shr | BinOpKind :: Sub => {
46
- check ( cx, right, 0 , expr. span , left. span , Parens :: Unneeded ) ;
47
- } ,
48
- BinOpKind :: Mul => {
49
- check ( cx, left, 1 , expr. span , right. span , needs_parenthesis ( cx, expr, right) ) ;
50
- check ( cx, right, 1 , expr. span , left. span , Parens :: Unneeded ) ;
51
- } ,
52
- BinOpKind :: Div => check ( cx, right, 1 , expr. span , left. span , Parens :: Unneeded ) ,
53
- BinOpKind :: BitAnd => {
54
- check ( cx, left, -1 , expr. span , right. span , needs_parenthesis ( cx, expr, right) ) ;
55
- check ( cx, right, -1 , expr. span , left. span , Parens :: Unneeded ) ;
56
- } ,
57
- BinOpKind :: Rem => check_remainder ( cx, left, right, expr. span , left. span ) ,
58
- _ => ( ) ,
59
- }
60
- }
13
+ pub ( crate ) fn check < ' tcx > (
14
+ cx : & LateContext < ' tcx > ,
15
+ expr : & ' tcx Expr < ' _ > ,
16
+ op : BinOpKind ,
17
+ left : & ' tcx Expr < ' _ > ,
18
+ right : & ' tcx Expr < ' _ > ,
19
+ ) {
20
+ if !is_allowed ( cx, op, left, right) {
21
+ match op {
22
+ BinOpKind :: Add | BinOpKind :: BitOr | BinOpKind :: BitXor => {
23
+ check_op ( cx, left, 0 , expr. span , right. span , needs_parenthesis ( cx, expr, right) ) ;
24
+ check_op ( cx, right, 0 , expr. span , left. span , Parens :: Unneeded ) ;
25
+ } ,
26
+ BinOpKind :: Shl | BinOpKind :: Shr | BinOpKind :: Sub => {
27
+ check_op ( cx, right, 0 , expr. span , left. span , Parens :: Unneeded ) ;
28
+ } ,
29
+ BinOpKind :: Mul => {
30
+ check_op ( cx, left, 1 , expr. span , right. span , needs_parenthesis ( cx, expr, right) ) ;
31
+ check_op ( cx, right, 1 , expr. span , left. span , Parens :: Unneeded ) ;
32
+ } ,
33
+ BinOpKind :: Div => check_op ( cx, right, 1 , expr. span , left. span , Parens :: Unneeded ) ,
34
+ BinOpKind :: BitAnd => {
35
+ check_op ( cx, left, -1 , expr. span , right. span , needs_parenthesis ( cx, expr, right) ) ;
36
+ check_op ( cx, right, -1 , expr. span , left. span , Parens :: Unneeded ) ;
37
+ } ,
38
+ BinOpKind :: Rem => check_remainder ( cx, left, right, expr. span , left. span ) ,
39
+ _ => ( ) ,
61
40
}
62
41
}
63
42
}
@@ -108,12 +87,12 @@ fn needs_parenthesis(cx: &LateContext<'_>, binary: &Expr<'_>, right: &Expr<'_>)
108
87
Parens :: Needed
109
88
}
110
89
111
- fn is_allowed ( cx : & LateContext < ' _ > , cmp : BinOp , left : & Expr < ' _ > , right : & Expr < ' _ > ) -> bool {
90
+ fn is_allowed ( cx : & LateContext < ' _ > , cmp : BinOpKind , left : & Expr < ' _ > , right : & Expr < ' _ > ) -> bool {
112
91
// This lint applies to integers
113
92
!cx. typeck_results ( ) . expr_ty ( left) . peel_refs ( ) . is_integral ( )
114
93
|| !cx. typeck_results ( ) . expr_ty ( right) . peel_refs ( ) . is_integral ( )
115
94
// `1 << 0` is a common pattern in bit manipulation code
116
- || ( cmp. node == BinOpKind :: Shl
95
+ || ( cmp == BinOpKind :: Shl
117
96
&& constant_simple ( cx, cx. typeck_results ( ) , right) == Some ( Constant :: Int ( 0 ) )
118
97
&& constant_simple ( cx, cx. typeck_results ( ) , left) == Some ( Constant :: Int ( 1 ) ) )
119
98
}
@@ -130,7 +109,7 @@ fn check_remainder(cx: &LateContext<'_>, left: &Expr<'_>, right: &Expr<'_>, span
130
109
}
131
110
}
132
111
133
- fn check ( cx : & LateContext < ' _ > , e : & Expr < ' _ > , m : i8 , span : Span , arg : Span , parens : Parens ) {
112
+ fn check_op ( cx : & LateContext < ' _ > , e : & Expr < ' _ > , m : i8 , span : Span , arg : Span , parens : Parens ) {
134
113
if let Some ( Constant :: Int ( v) ) = constant_simple ( cx, cx. typeck_results ( ) , e) . map ( Constant :: peel_refs) {
135
114
let check = match * cx. typeck_results ( ) . expr_ty ( e) . peel_refs ( ) . kind ( ) {
136
115
ty:: Int ( ity) => unsext ( cx. tcx , -1_i128 , ity) ,
0 commit comments