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_ast::ast::RangeLimits;
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 @@ declare_clippy_lint! {
"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,14 +91,10 @@ 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<'_>) {
if self.suppress_lint_in_const && cx.tcx.hir().is_inside_const_context(expr.hir_id) {
@ -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 @@ 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
/// 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