Skip to content

Commit e845366

Browse files
Add MSRV check for saturating_sub lints in const contexts
1 parent d20fc38 commit e845366

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

clippy_config/src/msrvs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ msrv_aliases! {
3535
1,52,0 { STR_SPLIT_ONCE, REM_EUCLID_CONST }
3636
1,51,0 { BORROW_AS_PTR, SEEK_FROM_CURRENT, UNSIGNED_ABS }
3737
1,50,0 { BOOL_THEN, CLAMP }
38-
1,47,0 { TAU, IS_ASCII_DIGIT_CONST, ARRAY_IMPL_ANY_LEN }
38+
1,47,0 { TAU, IS_ASCII_DIGIT_CONST, ARRAY_IMPL_ANY_LEN, SATURATING_SUB_CONST }
3939
1,46,0 { CONST_IF_MATCH }
4040
1,45,0 { STR_STRIP_PREFIX }
4141
1,43,0 { LOG2_10, LOG10_2, NUMERIC_ASSOCIATED_CONSTANTS }

clippy_lints/src/implicit_saturating_sub.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
use clippy_config::msrvs::{self, Msrv};
2+
use clippy_config::Conf;
13
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
24
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+
};
48
use rustc_ast::ast::LitKind;
59
use rustc_data_structures::packed::Pu128;
610
use rustc_errors::Applicability;
711
use rustc_hir::{BinOp, BinOpKind, Expr, ExprKind, HirId, QPath};
812
use rustc_lint::{LateContext, LateLintPass};
9-
use rustc_session::declare_lint_pass;
13+
use rustc_session::impl_lint_pass;
1014
use rustc_span::Span;
1115

1216
declare_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

7692
impl<'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

99120
fn check_manual_check<'tcx>(

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
623623
store.register_late_pass(|_| Box::new(unit_return_expecting_ord::UnitReturnExpectingOrd));
624624
store.register_late_pass(|_| Box::new(strings::StringAdd));
625625
store.register_late_pass(|_| Box::new(implicit_return::ImplicitReturn));
626-
store.register_late_pass(|_| Box::new(implicit_saturating_sub::ImplicitSaturatingSub));
626+
store.register_late_pass(move |_| Box::new(implicit_saturating_sub::ImplicitSaturatingSub::new(conf)));
627627
store.register_late_pass(|_| Box::new(default_numeric_fallback::DefaultNumericFallback));
628628
store.register_late_pass(|_| Box::new(inconsistent_struct_constructor::InconsistentStructConstructor));
629629
store.register_late_pass(|_| Box::new(non_octal_unix_permissions::NonOctalUnixPermissions));

0 commit comments

Comments
 (0)