add suppress_lint_in_const conf

This commit is contained in:
naosense 2022-11-21 14:36:56 +08:00
parent 12074808c7
commit 96f1385fdd
3 changed files with 23 additions and 3 deletions

View File

@ -82,11 +82,24 @@
"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 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
// 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),
};

View File

@ -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));

View File

@ -406,6 +406,10 @@ pub(crate) fn get_configuration_metadata() -> Vec<ClippyConfiguration> {
///
/// 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.