Skip to content

Commit b3fadd5

Browse files
authored
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
2 parents a775a1b + 6f6ddd2 commit b3fadd5

File tree

8 files changed

+61
-1
lines changed

8 files changed

+61
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6214,6 +6214,7 @@ Released 2018-09-13
62146214
[`allow-comparison-to-zero`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-comparison-to-zero
62156215
[`allow-dbg-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-dbg-in-tests
62166216
[`allow-expect-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-expect-in-tests
6217+
[`allow-indexing-slicing-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-indexing-slicing-in-tests
62176218
[`allow-mixed-uninlined-format-args`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-mixed-uninlined-format-args
62186219
[`allow-one-hash-in-raw-strings`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-one-hash-in-raw-strings
62196220
[`allow-panic-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-panic-in-tests

book/src/lint_configuration.md

+10
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ Whether `expect` should be allowed in test functions or `#[cfg(test)]`
8181
* [`expect_used`](https://rust-lang.github.io/rust-clippy/master/index.html#expect_used)
8282

8383

84+
## `allow-indexing-slicing-in-tests`
85+
Whether `indexing_slicing` should be allowed in test functions or `#[cfg(test)]`
86+
87+
**Default Value:** `false`
88+
89+
---
90+
**Affected lints:**
91+
* [`indexing_slicing`](https://rust-lang.github.io/rust-clippy/master/index.html#indexing_slicing)
92+
93+
8494
## `allow-mixed-uninlined-format-args`
8595
Whether to allow mixed uninlined format args, e.g. `format!("{} {}", a, foo.bar)`
8696

clippy_config/src/conf.rs

+3
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ define_Conf! {
291291
/// Whether `expect` should be allowed in test functions or `#[cfg(test)]`
292292
#[lints(expect_used)]
293293
allow_expect_in_tests: bool = false,
294+
/// Whether `indexing_slicing` should be allowed in test functions or `#[cfg(test)]`
295+
#[lints(indexing_slicing)]
296+
allow_indexing_slicing_in_tests: bool = false,
294297
/// Whether to allow mixed uninlined format args, e.g. `format!("{} {}", a, foo.bar)`
295298
#[lints(uninlined_format_args)]
296299
allow_mixed_uninlined_format_args: bool = true,

clippy_lints/src/indexing_slicing.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_config::Conf;
22
use clippy_utils::consts::{ConstEvalCtxt, Constant};
33
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
44
use clippy_utils::ty::{deref_chain, get_adt_inherent_method};
5-
use clippy_utils::{higher, is_from_proc_macro};
5+
use clippy_utils::{higher, is_from_proc_macro, is_in_test};
66
use rustc_ast::ast::RangeLimits;
77
use rustc_hir::{Expr, ExprKind};
88
use rustc_lint::{LateContext, LateLintPass};
@@ -96,12 +96,14 @@ declare_clippy_lint! {
9696
impl_lint_pass!(IndexingSlicing => [INDEXING_SLICING, OUT_OF_BOUNDS_INDEXING]);
9797

9898
pub struct IndexingSlicing {
99+
allow_indexing_slicing_in_tests: bool,
99100
suppress_restriction_lint_in_const: bool,
100101
}
101102

102103
impl IndexingSlicing {
103104
pub fn new(conf: &'static Conf) -> Self {
104105
Self {
106+
allow_indexing_slicing_in_tests: conf.allow_indexing_slicing_in_tests,
105107
suppress_restriction_lint_in_const: conf.suppress_restriction_lint_in_const,
106108
}
107109
}
@@ -122,6 +124,7 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
122124
{
123125
let note = "the suggestion might not be applicable in constant blocks";
124126
let ty = cx.typeck_results().expr_ty(array).peel_refs();
127+
let allowed_in_tests = self.allow_indexing_slicing_in_tests && is_in_test(cx.tcx, expr.hir_id);
125128
if let Some(range) = higher::Range::hir(index) {
126129
// Ranged indexes, i.e., &x[n..m], &x[n..], &x[..n] and &x[..]
127130
if let ty::Array(_, s) = ty.kind() {
@@ -171,6 +174,10 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
171174
(None, None) => return, // [..] is ok.
172175
};
173176

177+
if allowed_in_tests {
178+
return;
179+
}
180+
174181
span_lint_and_then(cx, INDEXING_SLICING, expr.span, "slicing may panic", |diag| {
175182
diag.help(help_msg);
176183

@@ -209,6 +216,10 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
209216
}
210217
}
211218

219+
if allowed_in_tests {
220+
return;
221+
}
222+
212223
span_lint_and_then(cx, INDEXING_SLICING, expr.span, "indexing may panic", |diag| {
213224
diag.help("consider using `.get(n)` or `.get_mut(n)` instead");
214225

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
allow-indexing-slicing-in-tests = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//@compile-flags: --test
2+
#![warn(clippy::indexing_slicing)]
3+
#![allow(clippy::no_effect)]
4+
5+
fn main() {
6+
let x = [1, 2, 3, 4];
7+
let index: usize = 1;
8+
&x[index..];
9+
}
10+
11+
#[cfg(test)]
12+
mod tests {
13+
#[test]
14+
fn test_fn() {
15+
let x = [1, 2, 3, 4];
16+
let index: usize = 1;
17+
&x[index..];
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: slicing may panic
2+
--> tests/ui-toml/indexing_slicing/indexing_slicing.rs:8:6
3+
|
4+
LL | &x[index..];
5+
| ^^^^^^^^^^
6+
|
7+
= help: consider using `.get(n..)` or .get_mut(n..)` instead
8+
= note: `-D clippy::indexing-slicing` implied by `-D warnings`
9+
= help: to override `-D warnings` add `#[allow(clippy::indexing_slicing)]`
10+
11+
error: aborting due to 1 previous error
12+

tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect
66
allow-comparison-to-zero
77
allow-dbg-in-tests
88
allow-expect-in-tests
9+
allow-indexing-slicing-in-tests
910
allow-mixed-uninlined-format-args
1011
allow-one-hash-in-raw-strings
1112
allow-panic-in-tests
@@ -93,6 +94,7 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect
9394
allow-comparison-to-zero
9495
allow-dbg-in-tests
9596
allow-expect-in-tests
97+
allow-indexing-slicing-in-tests
9698
allow-mixed-uninlined-format-args
9799
allow-one-hash-in-raw-strings
98100
allow-panic-in-tests
@@ -180,6 +182,7 @@ error: error reading Clippy's configuration file: unknown field `allow_mixed_uni
180182
allow-comparison-to-zero
181183
allow-dbg-in-tests
182184
allow-expect-in-tests
185+
allow-indexing-slicing-in-tests
183186
allow-mixed-uninlined-format-args
184187
allow-one-hash-in-raw-strings
185188
allow-panic-in-tests

0 commit comments

Comments
 (0)