1
+ use clippy_config:: msrvs:: { self , Msrv } ;
2
+ use clippy_config:: Conf ;
1
3
use clippy_utils:: diagnostics:: { span_lint_and_sugg, span_lint_and_then} ;
2
4
use clippy_utils:: source:: snippet_opt;
3
- use clippy_utils:: { higher, is_integer_literal, path_to_local, peel_blocks, peel_blocks_with_stmt, SpanlessEq } ;
5
+ use clippy_utils:: {
6
+ higher, in_constant, is_integer_literal, path_to_local, peel_blocks, peel_blocks_with_stmt, SpanlessEq ,
7
+ } ;
4
8
use rustc_ast:: ast:: LitKind ;
5
9
use rustc_data_structures:: packed:: Pu128 ;
6
10
use rustc_errors:: Applicability ;
7
11
use rustc_hir:: { BinOp , BinOpKind , Expr , ExprKind , HirId , QPath } ;
8
12
use rustc_lint:: { LateContext , LateLintPass } ;
9
- use rustc_session:: declare_lint_pass ;
13
+ use rustc_session:: impl_lint_pass ;
10
14
use rustc_span:: Span ;
11
15
12
16
declare_clippy_lint ! {
@@ -71,13 +75,28 @@ declare_clippy_lint! {
71
75
"Check if a variable is smaller than another one and still subtract from it even if smaller"
72
76
}
73
77
74
- declare_lint_pass ! ( ImplicitSaturatingSub => [ IMPLICIT_SATURATING_SUB , INVERTED_SATURATING_SUB ] ) ;
78
+ pub struct ImplicitSaturatingSub {
79
+ msrv : Msrv ,
80
+ }
81
+
82
+ impl_lint_pass ! ( ImplicitSaturatingSub => [ IMPLICIT_SATURATING_SUB , INVERTED_SATURATING_SUB ] ) ;
83
+
84
+ impl ImplicitSaturatingSub {
85
+ pub fn new ( conf : & ' static Conf ) -> Self {
86
+ Self {
87
+ msrv : conf. msrv . clone ( ) ,
88
+ }
89
+ }
90
+ }
75
91
76
92
impl < ' tcx > LateLintPass < ' tcx > for ImplicitSaturatingSub {
77
93
fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' tcx > ) {
78
94
if expr. span . from_expansion ( ) {
79
95
return ;
80
96
}
97
+ if in_constant ( cx, expr. hir_id ) && !self . msrv . meets ( msrvs:: SATURATING_SUB_CONST ) {
98
+ return ;
99
+ }
81
100
if let Some ( higher:: If { cond, then, r#else : None } ) = higher:: If :: hir ( expr)
82
101
83
102
// Check if the conditional expression is a binary operation
@@ -94,6 +113,8 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitSaturatingSub {
94
113
check_manual_check ( cx, expr, cond_op, cond_left, cond_right, if_block, else_block) ;
95
114
}
96
115
}
116
+
117
+ extract_msrv_attr ! ( LateContext ) ;
97
118
}
98
119
99
120
fn check_manual_check < ' tcx > (
0 commit comments