1+ use clippy_config:: msrvs:: { self , Msrv } ;
2+ use clippy_config:: Conf ;
13use clippy_utils:: diagnostics:: { span_lint_and_sugg, span_lint_and_then} ;
24use 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+ } ;
48use rustc_ast:: ast:: LitKind ;
59use rustc_data_structures:: packed:: Pu128 ;
610use rustc_errors:: Applicability ;
711use rustc_hir:: { BinOp , BinOpKind , Expr , ExprKind , HirId , QPath } ;
812use rustc_lint:: { LateContext , LateLintPass } ;
9- use rustc_session:: declare_lint_pass ;
13+ use rustc_session:: impl_lint_pass ;
1014use rustc_span:: Span ;
1115
1216declare_clippy_lint ! {
@@ -71,13 +75,28 @@ declare_clippy_lint! {
7175 "Check if a variable is smaller than another one and still subtract from it even if smaller"
7276}
7377
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+ }
7591
7692impl < ' tcx > LateLintPass < ' tcx > for ImplicitSaturatingSub {
7793 fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' tcx > ) {
7894 if expr. span . from_expansion ( ) {
7995 return ;
8096 }
97+ if in_constant ( cx, expr. hir_id ) && !self . msrv . meets ( msrvs:: SATURATING_SUB_CONST ) {
98+ return ;
99+ }
81100 if let Some ( higher:: If { cond, then, r#else : None } ) = higher:: If :: hir ( expr)
82101
83102 // Check if the conditional expression is a binary operation
@@ -94,6 +113,8 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitSaturatingSub {
94113 check_manual_check ( cx, expr, cond_op, cond_left, cond_right, if_block, else_block) ;
95114 }
96115 }
116+
117+ extract_msrv_attr ! ( LateContext ) ;
97118}
98119
99120fn check_manual_check < ' tcx > (
0 commit comments