Skip to content

Commit 96f1385

Browse files
committed
add suppress_lint_in_const conf
1 parent 1207480 commit 96f1385

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

clippy_lints/src/indexing_slicing.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,24 @@ declare_clippy_lint! {
8282
"indexing/slicing usage"
8383
}
8484

85+
#[derive(Copy, Clone)]
86+
pub struct IndexingSlicing {
87+
suppress_lint_in_const: bool,
88+
}
89+
90+
impl IndexingSlicing {
91+
pub fn new(suppress_lint_in_const: bool) -> Self {
92+
Self {
93+
suppress_lint_in_const,
94+
}
95+
}
96+
}
97+
8598
declare_lint_pass!(IndexingSlicing => [INDEXING_SLICING, OUT_OF_BOUNDS_INDEXING]);
8699

87100
impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
88101
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
89-
if cx.tcx.hir().is_inside_const_context(expr.hir_id) {
102+
if self.suppress_lint_in_const && cx.tcx.hir().is_inside_const_context(expr.hir_id) {
90103
return;
91104
}
92105

@@ -146,7 +159,7 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
146159
// Catchall non-range index, i.e., [n] or [n << m]
147160
if let ty::Array(..) = ty.kind() {
148161
// Index is a const block.
149-
if let ExprKind::ConstBlock(..) = index.kind {
162+
if self.suppress_lint_in_const && let ExprKind::ConstBlock(..) = index.kind {
150163
return;
151164
}
152165
// Index is a constant uint.
@@ -191,7 +204,7 @@ fn to_const_range(cx: &LateContext<'_>, range: higher::Range<'_>, array_size: u1
191204
} else {
192205
Some(x)
193206
}
194-
},
207+
}
195208
Some(_) => None,
196209
None => Some(array_size),
197210
};

clippy_lints/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,9 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
562562
let avoid_breaking_exported_api = conf.avoid_breaking_exported_api;
563563
let allow_expect_in_tests = conf.allow_expect_in_tests;
564564
let allow_unwrap_in_tests = conf.allow_unwrap_in_tests;
565+
let suppress_lint_in_const = conf.suppress_lint_in_const;
565566
store.register_late_pass(move |_| Box::new(approx_const::ApproxConstant::new(msrv())));
567+
store.register_late_pass(move |_| Box::new(approx_const::ApproxConstant::new(msrv)));
566568
store.register_late_pass(move |_| {
567569
Box::new(methods::Methods::new(
568570
avoid_breaking_exported_api,
@@ -684,6 +686,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
684686
store.register_late_pass(|_| Box::new(neg_cmp_op_on_partial_ord::NoNegCompOpForPartialOrd));
685687
store.register_late_pass(|_| Box::new(unwrap::Unwrap));
686688
store.register_late_pass(|_| Box::new(indexing_slicing::IndexingSlicing));
689+
store.register_late_pass(|_| Box::new(indexing_slicing::IndexingSlicing::new(suppress_lint_in_const)));
687690
store.register_late_pass(|_| Box::new(non_copy_const::NonCopyConst));
688691
store.register_late_pass(|_| Box::new(ptr_offset_with_cast::PtrOffsetWithCast));
689692
store.register_late_pass(|_| Box::new(redundant_clone::RedundantClone));

clippy_lints/src/utils/conf.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,10 @@ define_Conf! {
406406
///
407407
/// Whether to allow mixed uninlined format args, e.g. `format!("{} {}", a, foo.bar)`
408408
(allow_mixed_uninlined_format_args: bool = true),
409+
/// Lint: SUPPRESS_LINT
410+
///
411+
/// Whether to suppress lint in const function
412+
(suppress_lint_in_const: bool = true),
409413
}
410414

411415
/// Search for the configuration file.

0 commit comments

Comments
 (0)