From 96f1385fdd75af35ea888eeb68dc3631d7637a2b Mon Sep 17 00:00:00 2001 From: naosense Date: Mon, 21 Nov 2022 14:36:56 +0800 Subject: [PATCH] add `suppress_lint_in_const` conf --- clippy_lints/src/indexing_slicing.rs | 19 ++++++++++++++++--- clippy_lints/src/lib.rs | 3 +++ clippy_lints/src/utils/conf.rs | 4 ++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/indexing_slicing.rs b/clippy_lints/src/indexing_slicing.rs index 4cd7dff4c..23beeb445 100644 --- a/clippy_lints/src/indexing_slicing.rs +++ b/clippy_lints/src/indexing_slicing.rs @@ -82,11 +82,24 @@ declare_clippy_lint! { "indexing/slicing usage" } +#[derive(Copy, Clone)] +pub struct IndexingSlicing { + suppress_lint_in_const: bool, +} + +impl IndexingSlicing { + pub fn new(suppress_lint_in_const: bool) -> Self { + Self { + suppress_lint_in_const, + } + } +} + declare_lint_pass!(IndexingSlicing => [INDEXING_SLICING, OUT_OF_BOUNDS_INDEXING]); impl<'tcx> LateLintPass<'tcx> for IndexingSlicing { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { - if cx.tcx.hir().is_inside_const_context(expr.hir_id) { + if self.suppress_lint_in_const && cx.tcx.hir().is_inside_const_context(expr.hir_id) { return; } @@ -146,7 +159,7 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing { // Catchall non-range index, i.e., [n] or [n << m] if let ty::Array(..) = ty.kind() { // Index is a const block. - if let ExprKind::ConstBlock(..) = index.kind { + if self.suppress_lint_in_const && let ExprKind::ConstBlock(..) = index.kind { return; } // Index is a constant uint. @@ -191,7 +204,7 @@ fn to_const_range(cx: &LateContext<'_>, range: higher::Range<'_>, array_size: u1 } else { Some(x) } - }, + } Some(_) => None, None => Some(array_size), }; diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 3fe39488a..3bac39458 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -562,7 +562,9 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: let avoid_breaking_exported_api = conf.avoid_breaking_exported_api; let allow_expect_in_tests = conf.allow_expect_in_tests; let allow_unwrap_in_tests = conf.allow_unwrap_in_tests; + let suppress_lint_in_const = conf.suppress_lint_in_const; store.register_late_pass(move |_| Box::new(approx_const::ApproxConstant::new(msrv()))); + store.register_late_pass(move |_| Box::new(approx_const::ApproxConstant::new(msrv))); store.register_late_pass(move |_| { Box::new(methods::Methods::new( avoid_breaking_exported_api, @@ -684,6 +686,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|_| Box::new(neg_cmp_op_on_partial_ord::NoNegCompOpForPartialOrd)); store.register_late_pass(|_| Box::new(unwrap::Unwrap)); store.register_late_pass(|_| Box::new(indexing_slicing::IndexingSlicing)); + store.register_late_pass(|_| Box::new(indexing_slicing::IndexingSlicing::new(suppress_lint_in_const))); store.register_late_pass(|_| Box::new(non_copy_const::NonCopyConst)); store.register_late_pass(|_| Box::new(ptr_offset_with_cast::PtrOffsetWithCast)); store.register_late_pass(|_| Box::new(redundant_clone::RedundantClone)); diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs index b6dc8cd7a..1352632b3 100644 --- a/clippy_lints/src/utils/conf.rs +++ b/clippy_lints/src/utils/conf.rs @@ -406,6 +406,10 @@ define_Conf! { /// /// Whether to allow mixed uninlined format args, e.g. `format!("{} {}", a, foo.bar)` (allow_mixed_uninlined_format_args: bool = true), + /// Lint: SUPPRESS_LINT + /// + /// Whether to suppress lint in const function + (suppress_lint_in_const: bool = true), } /// Search for the configuration file.