Skip to content

Commit f18f779

Browse files
Add MSRV check for saturating_sub lints in const contexts
1 parent 8aa4e51 commit f18f779

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-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: 22 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! {
@@ -70,13 +74,28 @@ declare_clippy_lint! {
7074
"Check if a variable is smaller than another one and still subtract from it even if smaller"
7175
}
7276

73-
declare_lint_pass!(ImplicitSaturatingSub => [IMPLICIT_SATURATING_SUB, INVERTED_SATURATING_SUB]);
77+
pub struct ImplicitSaturatingSub {
78+
msrv: Msrv,
79+
}
80+
81+
impl_lint_pass!(ImplicitSaturatingSub => [IMPLICIT_SATURATING_SUB, INVERTED_SATURATING_SUB]);
82+
83+
impl ImplicitSaturatingSub {
84+
pub fn new(conf: &'static Conf) -> Self {
85+
Self {
86+
msrv: conf.msrv.clone(),
87+
}
88+
}
89+
}
7490

7591
impl<'tcx> LateLintPass<'tcx> for ImplicitSaturatingSub {
7692
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
7793
if expr.span.from_expansion() {
7894
return;
7995
}
96+
if in_constant(cx, expr.hir_id) && !self.msrv.meets(msrvs::SATURATING_SUB_CONST) {
97+
return;
98+
}
8099
if let Some(higher::If { cond, then, r#else: None }) = higher::If::hir(expr)
81100

82101
// Check if the conditional expression is a binary operation

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
600600
store.register_late_pass(|_| Box::new(unit_return_expecting_ord::UnitReturnExpectingOrd));
601601
store.register_late_pass(|_| Box::new(strings::StringAdd));
602602
store.register_late_pass(|_| Box::new(implicit_return::ImplicitReturn));
603-
store.register_late_pass(|_| Box::new(implicit_saturating_sub::ImplicitSaturatingSub));
603+
store.register_late_pass(move |_| Box::new(implicit_saturating_sub::ImplicitSaturatingSub::new(conf)));
604604
store.register_late_pass(|_| Box::new(default_numeric_fallback::DefaultNumericFallback));
605605
store.register_late_pass(|_| Box::new(inconsistent_struct_constructor::InconsistentStructConstructor));
606606
store.register_late_pass(|_| Box::new(non_octal_unix_permissions::NonOctalUnixPermissions));

0 commit comments

Comments
 (0)