add test and stderr

This commit is contained in:
naosense 2022-11-22 17:12:50 +08:00
parent 96f1385fdd
commit aed9497978
7 changed files with 35 additions and 10 deletions

View File

@ -7,7 +7,7 @@
use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty;
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_session::{declare_tool_lint, impl_lint_pass};
declare_clippy_lint! {
/// ### What it does
@ -82,6 +82,8 @@
"indexing/slicing usage"
}
impl_lint_pass!(IndexingSlicing => [INDEXING_SLICING, OUT_OF_BOUNDS_INDEXING]);
#[derive(Copy, Clone)]
pub struct IndexingSlicing {
suppress_lint_in_const: bool,
@ -89,13 +91,9 @@ pub struct IndexingSlicing {
impl IndexingSlicing {
pub fn new(suppress_lint_in_const: bool) -> Self {
Self {
suppress_lint_in_const,
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<'_>) {
@ -204,7 +202,7 @@ fn to_const_range(cx: &LateContext<'_>, range: higher::Range<'_>, array_size: u1
} else {
Some(x)
}
}
},
Some(_) => None,
None => Some(array_size),
};

View File

@ -685,8 +685,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(|_| Box::new(inherent_impl::MultipleInherentImpl));
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(move |_| 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,7 +406,7 @@ 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
/// Lint: INDEXING_SLICING
///
/// Whether to suppress lint in const function
(suppress_lint_in_const: bool = true),

View File

@ -0,0 +1 @@
suppress-lint-in-const = false

View File

@ -0,0 +1,15 @@
#![warn(clippy::indexing_slicing)]
/// An opaque integer representation
pub struct Integer<'a> {
/// The underlying data
value: &'a [u8],
}
impl<'a> Integer<'a> {
// Check whether `self` holds a negative number or not
pub const fn is_negative(&self) -> bool {
self.value[0] & 0b1000_0000 != 0
}
}
fn main() {}

View File

@ -0,0 +1,11 @@
error: indexing may panic
--> $DIR/test.rs:11:9
|
LL | self.value[0] & 0b1000_0000 != 0
| ^^^^^^^^^^^^^
|
= help: consider using `.get(n)` or `.get_mut(n)` instead
= note: `-D clippy::indexing-slicing` implied by `-D warnings`
error: aborting due to previous error

View File

@ -35,6 +35,7 @@ error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown fie
pass-by-value-size-limit
single-char-binding-names-threshold
standard-macro-braces
suppress-lint-in-const
third-party
too-large-for-stack
too-many-arguments-threshold