Skip to content

Commit 938273f

Browse files
committed
add suppress_lint_in_const conf
1 parent dac600e commit 938273f

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
599599
let avoid_breaking_exported_api = conf.avoid_breaking_exported_api;
600600
let allow_expect_in_tests = conf.allow_expect_in_tests;
601601
let allow_unwrap_in_tests = conf.allow_unwrap_in_tests;
602+
let suppress_lint_in_const = conf.suppress_lint_in_const;
602603
store.register_late_pass(move |_| Box::new(approx_const::ApproxConstant::new(msrv)));
603604
store.register_late_pass(move |_| {
604605
Box::new(methods::Methods::new(
@@ -721,6 +722,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
721722
store.register_late_pass(|_| Box::new(neg_cmp_op_on_partial_ord::NoNegCompOpForPartialOrd));
722723
store.register_late_pass(|_| Box::new(unwrap::Unwrap));
723724
store.register_late_pass(|_| Box::new(indexing_slicing::IndexingSlicing));
725+
store.register_late_pass(|_| Box::new(indexing_slicing::IndexingSlicing::new(suppress_lint_in_const)));
724726
store.register_late_pass(|_| Box::new(non_copy_const::NonCopyConst));
725727
store.register_late_pass(|_| Box::new(ptr_offset_with_cast::PtrOffsetWithCast));
726728
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
@@ -402,6 +402,10 @@ define_Conf! {
402402
/// A list of paths to types that should be treated like `Arc`, i.e. ignored but
403403
/// for the generic parameters for determining interior mutability
404404
(ignore_interior_mutability: Vec<String> = Vec::from(["bytes::Bytes".into()])),
405+
/// Lint: SUPPRESS_LINT
406+
///
407+
/// Whether to suppress lint in const function
408+
(suppress_lint_in_const: bool = true),
405409
}
406410

407411
/// Search for the configuration file.

0 commit comments

Comments
 (0)