mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 21:23:56 +00:00
add test and stderr
This commit is contained in:
parent
96f1385fdd
commit
aed9497978
7 changed files with 35 additions and 10 deletions
|
@ -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),
|
||||
};
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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),
|
||||
|
|
1
tests/ui-toml/suppress_lint_in_const/clippy.toml
Normal file
1
tests/ui-toml/suppress_lint_in_const/clippy.toml
Normal file
|
@ -0,0 +1 @@
|
|||
suppress-lint-in-const = false
|
15
tests/ui-toml/suppress_lint_in_const/test.rs
Normal file
15
tests/ui-toml/suppress_lint_in_const/test.rs
Normal 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() {}
|
11
tests/ui-toml/suppress_lint_in_const/test.stderr
Normal file
11
tests/ui-toml/suppress_lint_in_const/test.stderr
Normal 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
|
||||
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue