Skip to content

Commit 10ec6d4

Browse files
committed
add test and stderr
1 parent 938273f commit 10ec6d4

File tree

7 files changed

+35
-10
lines changed

7 files changed

+35
-10
lines changed

clippy_lints/src/indexing_slicing.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_ast::ast::RangeLimits;
77
use rustc_hir::{Expr, ExprKind};
88
use rustc_lint::{LateContext, LateLintPass};
99
use rustc_middle::ty;
10-
use rustc_session::{declare_lint_pass, declare_tool_lint};
10+
use rustc_session::{declare_tool_lint, impl_lint_pass};
1111

1212
declare_clippy_lint! {
1313
/// ### What it does
@@ -82,21 +82,19 @@ declare_clippy_lint! {
8282
"indexing/slicing usage"
8383
}
8484

85+
impl_lint_pass!(IndexingSlicing => [INDEXING_SLICING, OUT_OF_BOUNDS_INDEXING]);
86+
8587
#[derive(Copy, Clone)]
8688
pub struct IndexingSlicing {
8789
suppress_lint_in_const: bool,
8890
}
8991

9092
impl IndexingSlicing {
9193
pub fn new(suppress_lint_in_const: bool) -> Self {
92-
Self {
93-
suppress_lint_in_const,
94-
}
94+
Self { suppress_lint_in_const }
9595
}
9696
}
9797

98-
declare_lint_pass!(IndexingSlicing => [INDEXING_SLICING, OUT_OF_BOUNDS_INDEXING]);
99-
10098
impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
10199
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
102100
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
204202
} else {
205203
Some(x)
206204
}
207-
}
205+
},
208206
Some(_) => None,
209207
None => Some(array_size),
210208
};

clippy_lints/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,8 +721,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
721721
store.register_late_pass(|_| Box::new(inherent_impl::MultipleInherentImpl));
722722
store.register_late_pass(|_| Box::new(neg_cmp_op_on_partial_ord::NoNegCompOpForPartialOrd));
723723
store.register_late_pass(|_| Box::new(unwrap::Unwrap));
724-
store.register_late_pass(|_| Box::new(indexing_slicing::IndexingSlicing));
725-
store.register_late_pass(|_| Box::new(indexing_slicing::IndexingSlicing::new(suppress_lint_in_const)));
724+
store.register_late_pass(move |_| Box::new(indexing_slicing::IndexingSlicing::new(suppress_lint_in_const)));
726725
store.register_late_pass(|_| Box::new(non_copy_const::NonCopyConst));
727726
store.register_late_pass(|_| Box::new(ptr_offset_with_cast::PtrOffsetWithCast));
728727
store.register_late_pass(|_| Box::new(redundant_clone::RedundantClone));

clippy_lints/src/utils/conf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ 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
405+
/// Lint: INDEXING_SLICING
406406
///
407407
/// Whether to suppress lint in const function
408408
(suppress_lint_in_const: bool = true),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
suppress-lint-in-const = false
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![warn(clippy::indexing_slicing)]
2+
3+
/// An opaque integer representation
4+
pub struct Integer<'a> {
5+
/// The underlying data
6+
value: &'a [u8],
7+
}
8+
impl<'a> Integer<'a> {
9+
// Check whether `self` holds a negative number or not
10+
pub const fn is_negative(&self) -> bool {
11+
self.value[0] & 0b1000_0000 != 0
12+
}
13+
}
14+
15+
fn main() {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: indexing may panic
2+
--> $DIR/test.rs:11:9
3+
|
4+
LL | self.value[0] & 0b1000_0000 != 0
5+
| ^^^^^^^^^^^^^
6+
|
7+
= help: consider using `.get(n)` or `.get_mut(n)` instead
8+
= note: `-D clippy::indexing-slicing` implied by `-D warnings`
9+
10+
error: aborting due to previous error
11+

tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown fie
3434
pass-by-value-size-limit
3535
single-char-binding-names-threshold
3636
standard-macro-braces
37+
suppress-lint-in-const
3738
third-party
3839
too-large-for-stack
3940
too-many-arguments-threshold

0 commit comments

Comments
 (0)