Skip to content

Commit

Permalink
Add allow-indexing-slicing-in-tests option (#13854)
Browse files Browse the repository at this point in the history
Close #13842

changelog: [`indexing_slicing`]: add allow-indexing-slicing-in-tests
option to be able ignore at test
  • Loading branch information
llogiq authored Dec 19, 2024
2 parents a775a1b + 6f6ddd2 commit b3fadd5
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6214,6 +6214,7 @@ Released 2018-09-13
[`allow-comparison-to-zero`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-comparison-to-zero
[`allow-dbg-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-dbg-in-tests
[`allow-expect-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-expect-in-tests
[`allow-indexing-slicing-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-indexing-slicing-in-tests
[`allow-mixed-uninlined-format-args`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-mixed-uninlined-format-args
[`allow-one-hash-in-raw-strings`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-one-hash-in-raw-strings
[`allow-panic-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-panic-in-tests
Expand Down
10 changes: 10 additions & 0 deletions book/src/lint_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ Whether `expect` should be allowed in test functions or `#[cfg(test)]`
* [`expect_used`](https://rust-lang.github.io/rust-clippy/master/index.html#expect_used)


## `allow-indexing-slicing-in-tests`
Whether `indexing_slicing` should be allowed in test functions or `#[cfg(test)]`

**Default Value:** `false`

---
**Affected lints:**
* [`indexing_slicing`](https://rust-lang.github.io/rust-clippy/master/index.html#indexing_slicing)


## `allow-mixed-uninlined-format-args`
Whether to allow mixed uninlined format args, e.g. `format!("{} {}", a, foo.bar)`

Expand Down
3 changes: 3 additions & 0 deletions clippy_config/src/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ define_Conf! {
/// Whether `expect` should be allowed in test functions or `#[cfg(test)]`
#[lints(expect_used)]
allow_expect_in_tests: bool = false,
/// Whether `indexing_slicing` should be allowed in test functions or `#[cfg(test)]`
#[lints(indexing_slicing)]
allow_indexing_slicing_in_tests: bool = false,
/// Whether to allow mixed uninlined format args, e.g. `format!("{} {}", a, foo.bar)`
#[lints(uninlined_format_args)]
allow_mixed_uninlined_format_args: bool = true,
Expand Down
13 changes: 12 additions & 1 deletion clippy_lints/src/indexing_slicing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
}
}
Expand All @@ -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() {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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");

Expand Down
1 change: 1 addition & 0 deletions tests/ui-toml/indexing_slicing/clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
allow-indexing-slicing-in-tests = true
19 changes: 19 additions & 0 deletions tests/ui-toml/indexing_slicing/indexing_slicing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//@compile-flags: --test
#![warn(clippy::indexing_slicing)]
#![allow(clippy::no_effect)]

fn main() {
let x = [1, 2, 3, 4];
let index: usize = 1;
&x[index..];
}

#[cfg(test)]
mod tests {
#[test]
fn test_fn() {
let x = [1, 2, 3, 4];
let index: usize = 1;
&x[index..];
}
}
12 changes: 12 additions & 0 deletions tests/ui-toml/indexing_slicing/indexing_slicing.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error: slicing may panic
--> tests/ui-toml/indexing_slicing/indexing_slicing.rs:8:6
|
LL | &x[index..];
| ^^^^^^^^^^
|
= help: consider using `.get(n..)` or .get_mut(n..)` instead
= note: `-D clippy::indexing-slicing` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::indexing_slicing)]`

error: aborting due to 1 previous error

3 changes: 3 additions & 0 deletions tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect
allow-comparison-to-zero
allow-dbg-in-tests
allow-expect-in-tests
allow-indexing-slicing-in-tests
allow-mixed-uninlined-format-args
allow-one-hash-in-raw-strings
allow-panic-in-tests
Expand Down Expand Up @@ -93,6 +94,7 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect
allow-comparison-to-zero
allow-dbg-in-tests
allow-expect-in-tests
allow-indexing-slicing-in-tests
allow-mixed-uninlined-format-args
allow-one-hash-in-raw-strings
allow-panic-in-tests
Expand Down Expand Up @@ -180,6 +182,7 @@ error: error reading Clippy's configuration file: unknown field `allow_mixed_uni
allow-comparison-to-zero
allow-dbg-in-tests
allow-expect-in-tests
allow-indexing-slicing-in-tests
allow-mixed-uninlined-format-args
allow-one-hash-in-raw-strings
allow-panic-in-tests
Expand Down

0 comments on commit b3fadd5

Please sign in to comment.