diff options
| author | CrazyRoka <RokaRostuk@gmail.com> | 2020-04-26 17:57:19 +0300 |
|---|---|---|
| committer | CrazyRoka <RokaRostuk@gmail.com> | 2020-04-26 18:00:51 +0300 |
| commit | 940c6626541664597c41577a3d54ab7e5bbe10ee (patch) | |
| tree | 45237b0ebb144e63d92c2fafef37f3be4563233e | |
| parent | 63b451ea25c7797530c84b82781e0800c9bda68d (diff) | |
| download | rust-940c6626541664597c41577a3d54ab7e5bbe10ee.tar.gz rust-940c6626541664597c41577a3d54ab7e5bbe10ee.zip | |
Small lint update
- Changed lint category to `correctness` - Moved main function to bottom in test file - Added `FIXME` comment to `span_lint_and_sugg` to improve later
| -rw-r--r-- | clippy_lints/src/match_on_vec_items.rs | 15 | ||||
| -rw-r--r-- | tests/ui/match_on_vec_items.rs | 18 | ||||
| -rw-r--r-- | tests/ui/match_on_vec_items.stderr | 32 |
3 files changed, 33 insertions, 32 deletions
diff --git a/clippy_lints/src/match_on_vec_items.rs b/clippy_lints/src/match_on_vec_items.rs index 167b4abd93d..4071406cc84 100644 --- a/clippy_lints/src/match_on_vec_items.rs +++ b/clippy_lints/src/match_on_vec_items.rs @@ -1,4 +1,4 @@ -use crate::utils::{is_type_diagnostic_item, snippet_with_applicability, span_lint_and_sugg, walk_ptrs_ty}; +use crate::utils::{is_type_diagnostic_item, snippet, span_lint_and_sugg, walk_ptrs_ty}; use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind, MatchSource}; @@ -38,7 +38,7 @@ declare_clippy_lint! { /// } /// ``` pub MATCH_ON_VEC_ITEMS, - style, + correctness, "matching on vector elements can panic" } @@ -53,19 +53,20 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MatchOnVecItems { if let ExprKind::Index(vec, idx) = idx_expr.kind; then { - let mut applicability = Applicability::MaybeIncorrect; + // FIXME: could be improved to suggest surrounding every pattern with Some(_), + // but only when `or_patterns` are stabilized. span_lint_and_sugg( cx, MATCH_ON_VEC_ITEMS, match_expr.span, - "indexing vector may panic. Consider using `get`", + "indexing into a vector may panic", "try this", format!( "{}.get({})", - snippet_with_applicability(cx, vec.span, "..", &mut applicability), - snippet_with_applicability(cx, idx.span, "..", &mut applicability) + snippet(cx, vec.span, ".."), + snippet(cx, idx.span, "..") ), - applicability + Applicability::MaybeIncorrect ); } } diff --git a/tests/ui/match_on_vec_items.rs b/tests/ui/match_on_vec_items.rs index 02b6da03439..0bb39d77e46 100644 --- a/tests/ui/match_on_vec_items.rs +++ b/tests/ui/match_on_vec_items.rs @@ -1,14 +1,5 @@ #![warn(clippy::match_on_vec_items)] -fn main() { - match_with_wildcard(); - match_without_wildcard(); - match_wildcard_and_action(); - match_vec_ref(); - match_with_get(); - match_with_array(); -} - fn match_with_wildcard() { let arr = vec![0, 1, 2, 3]; let range = 1..3; @@ -128,3 +119,12 @@ fn match_with_array() { _ => {}, } } + +fn main() { + match_with_wildcard(); + match_without_wildcard(); + match_wildcard_and_action(); + match_vec_ref(); + match_with_get(); + match_with_array(); +} diff --git a/tests/ui/match_on_vec_items.stderr b/tests/ui/match_on_vec_items.stderr index 0e53a58f141..49446d715ab 100644 --- a/tests/ui/match_on_vec_items.stderr +++ b/tests/ui/match_on_vec_items.stderr @@ -1,49 +1,49 @@ -error: indexing vector may panic. Consider using `get` - --> $DIR/match_on_vec_items.rs:18:11 +error: indexing into a vector may panic + --> $DIR/match_on_vec_items.rs:9:11 | LL | match arr[idx] { | ^^^^^^^^ help: try this: `arr.get(idx)` | = note: `-D clippy::match-on-vec-items` implied by `-D warnings` -error: indexing vector may panic. Consider using `get` - --> $DIR/match_on_vec_items.rs:25:11 +error: indexing into a vector may panic + --> $DIR/match_on_vec_items.rs:16:11 | LL | match arr[range] { | ^^^^^^^^^^ help: try this: `arr.get(range)` -error: indexing vector may panic. Consider using `get` - --> $DIR/match_on_vec_items.rs:38:11 +error: indexing into a vector may panic + --> $DIR/match_on_vec_items.rs:29:11 | LL | match arr[idx] { | ^^^^^^^^ help: try this: `arr.get(idx)` -error: indexing vector may panic. Consider using `get` - --> $DIR/match_on_vec_items.rs:45:11 +error: indexing into a vector may panic + --> $DIR/match_on_vec_items.rs:36:11 | LL | match arr[range] { | ^^^^^^^^^^ help: try this: `arr.get(range)` -error: indexing vector may panic. Consider using `get` - --> $DIR/match_on_vec_items.rs:58:11 +error: indexing into a vector may panic + --> $DIR/match_on_vec_items.rs:49:11 | LL | match arr[idx] { | ^^^^^^^^ help: try this: `arr.get(idx)` -error: indexing vector may panic. Consider using `get` - --> $DIR/match_on_vec_items.rs:65:11 +error: indexing into a vector may panic + --> $DIR/match_on_vec_items.rs:56:11 | LL | match arr[range] { | ^^^^^^^^^^ help: try this: `arr.get(range)` -error: indexing vector may panic. Consider using `get` - --> $DIR/match_on_vec_items.rs:78:11 +error: indexing into a vector may panic + --> $DIR/match_on_vec_items.rs:69:11 | LL | match arr[idx] { | ^^^^^^^^ help: try this: `arr.get(idx)` -error: indexing vector may panic. Consider using `get` - --> $DIR/match_on_vec_items.rs:85:11 +error: indexing into a vector may panic + --> $DIR/match_on_vec_items.rs:76:11 | LL | match arr[range] { | ^^^^^^^^^^ help: try this: `arr.get(range)` |
