about summary refs log tree commit diff
path: root/clippy_lints/src
diff options
context:
space:
mode:
authorllogiq <bogusandre@gmail.com>2024-12-19 20:11:48 +0000
committerGitHub <noreply@github.com>2024-12-19 20:11:48 +0000
commitb3fadd5a8ef45bcd24e9dc31d32926cb7ad26444 (patch)
treed9490e70b18667d3afbb09682031e639b8674f73 /clippy_lints/src
parenta775a1b41f3624bd970a08e74140db05d507476f (diff)
parent6f6ddd299c5ab87a8df5404d9ccbd16a5f358aa8 (diff)
downloadrust-b3fadd5a8ef45bcd24e9dc31d32926cb7ad26444.tar.gz
rust-b3fadd5a8ef45bcd24e9dc31d32926cb7ad26444.zip
Add allow-indexing-slicing-in-tests option (#13854)
Close #13842

changelog: [`indexing_slicing`]: add allow-indexing-slicing-in-tests
option to be able ignore at test
Diffstat (limited to 'clippy_lints/src')
-rw-r--r--clippy_lints/src/indexing_slicing.rs13
1 files changed, 12 insertions, 1 deletions
diff --git a/clippy_lints/src/indexing_slicing.rs b/clippy_lints/src/indexing_slicing.rs
index 58346f78013..f666ed0a440 100644
--- a/clippy_lints/src/indexing_slicing.rs
+++ b/clippy_lints/src/indexing_slicing.rs
@@ -2,7 +2,7 @@ use clippy_config::Conf;
 use clippy_utils::consts::{ConstEvalCtxt, Constant};
 use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
 use clippy_utils::ty::{deref_chain, get_adt_inherent_method};
-use clippy_utils::{higher, is_from_proc_macro};
+use clippy_utils::{higher, is_from_proc_macro, is_in_test};
 use rustc_ast::ast::RangeLimits;
 use rustc_hir::{Expr, ExprKind};
 use rustc_lint::{LateContext, LateLintPass};
@@ -96,12 +96,14 @@ declare_clippy_lint! {
 impl_lint_pass!(IndexingSlicing => [INDEXING_SLICING, OUT_OF_BOUNDS_INDEXING]);
 
 pub struct IndexingSlicing {
+    allow_indexing_slicing_in_tests: bool,
     suppress_restriction_lint_in_const: bool,
 }
 
 impl IndexingSlicing {
     pub fn new(conf: &'static Conf) -> Self {
         Self {
+            allow_indexing_slicing_in_tests: conf.allow_indexing_slicing_in_tests,
             suppress_restriction_lint_in_const: conf.suppress_restriction_lint_in_const,
         }
     }
@@ -122,6 +124,7 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
         {
             let note = "the suggestion might not be applicable in constant blocks";
             let ty = cx.typeck_results().expr_ty(array).peel_refs();
+            let allowed_in_tests = self.allow_indexing_slicing_in_tests && is_in_test(cx.tcx, expr.hir_id);
             if let Some(range) = higher::Range::hir(index) {
                 // Ranged indexes, i.e., &x[n..m], &x[n..], &x[..n] and &x[..]
                 if let ty::Array(_, s) = ty.kind() {
@@ -171,6 +174,10 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
                     (None, None) => return, // [..] is ok.
                 };
 
+                if allowed_in_tests {
+                    return;
+                }
+
                 span_lint_and_then(cx, INDEXING_SLICING, expr.span, "slicing may panic", |diag| {
                     diag.help(help_msg);
 
@@ -209,6 +216,10 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
                     }
                 }
 
+                if allowed_in_tests {
+                    return;
+                }
+
                 span_lint_and_then(cx, INDEXING_SLICING, expr.span, "indexing may panic", |diag| {
                     diag.help("consider using `.get(n)` or `.get_mut(n)` instead");