-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c2d23ad
commit 02f8048
Showing
2 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
diff --git a/CHANGELOG.md b/CHANGELOG.md | ||
index cc9669729..f1983ad1d 100644 | ||
--- a/CHANGELOG.md | ||
+++ b/CHANGELOG.md | ||
@@ -6215,6 +6215,7 @@ Released 2018-09-13 | ||
[`allow-expect-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-expect-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-indexing-slicing-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-indexing-slicing-in-tests | ||
[`allow-panic-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-panic-in-tests | ||
[`allow-print-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-print-in-tests | ||
[`allow-private-module-inception`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-private-module-inception | ||
diff --git a/book/src/lint_configuration.md b/book/src/lint_configuration.md | ||
index 275d12509..1ec33025e 100644 | ||
--- a/book/src/lint_configuration.md | ||
+++ b/book/src/lint_configuration.md | ||
@@ -101,6 +101,16 @@ Whether to allow `r#""#` when `r""` can be used | ||
* [`unnecessary_raw_string_hashes`](https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_raw_string_hashes) | ||
|
||
|
||
+## `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-panic-in-tests` | ||
Whether `panic` should be allowed in test functions or `#[cfg(test)]` | ||
|
||
diff --git a/clippy_config/src/conf.rs b/clippy_config/src/conf.rs | ||
index 41b56b45d..bffa04f6f 100644 | ||
--- a/clippy_config/src/conf.rs | ||
+++ b/clippy_config/src/conf.rs | ||
@@ -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, | ||
diff --git a/clippy_lints/src/indexing_slicing.rs b/clippy_lints/src/indexing_slicing.rs | ||
index 15203b35b..c8cdc19f7 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}; | ||
@@ -89,12 +89,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, | ||
} | ||
} | ||
@@ -164,6 +166,10 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing { | ||
(None, None) => return, // [..] is ok. | ||
}; | ||
|
||
+ if self.allow_indexing_slicing_in_tests && is_in_test(cx.tcx, expr.hir_id) { | ||
+ return; | ||
+ } | ||
+ | ||
span_lint_and_then(cx, INDEXING_SLICING, expr.span, "slicing may panic", |diag| { | ||
diag.help(help_msg); | ||
|
||
@@ -202,6 +208,10 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing { | ||
} | ||
} | ||
|
||
+ if self.allow_indexing_slicing_in_tests && is_in_test(cx.tcx, expr.hir_id) { | ||
+ 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"); | ||
|
||
diff --git a/tests/ui-toml/indexing_slicing/clippy.toml b/tests/ui-toml/indexing_slicing/clippy.toml | ||
new file mode 100644 | ||
index 000000000..7e8386833 | ||
--- /dev/null | ||
+++ b/tests/ui-toml/indexing_slicing/clippy.toml | ||
@@ -0,0 +1 @@ | ||
+allow-indexing-slicing-in-tests = true | ||
diff --git a/tests/ui-toml/indexing_slicing/indexing_slicing.rs b/tests/ui-toml/indexing_slicing/indexing_slicing.rs | ||
new file mode 100644 | ||
index 000000000..0a0da88ea | ||
--- /dev/null | ||
+++ b/tests/ui-toml/indexing_slicing/indexing_slicing.rs | ||
@@ -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..]; | ||
+ } | ||
+} | ||
diff --git a/tests/ui-toml/indexing_slicing/indexing_slicing.stderr b/tests/ui-toml/indexing_slicing/indexing_slicing.stderr | ||
new file mode 100644 | ||
index 000000000..5a4de8337 | ||
--- /dev/null | ||
+++ b/tests/ui-toml/indexing_slicing/indexing_slicing.stderr | ||
@@ -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 | ||
+ | ||
diff --git a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr | ||
index 6fa583fc0..200129da2 100644 | ||
--- a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr | ||
+++ b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr | ||
@@ -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 | ||
@@ -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 | ||
@@ -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 |