Skip to content

Commit

Permalink
Small lint update
Browse files Browse the repository at this point in the history
- Changed lint category to `correctness`
- Moved main function to bottom in test file
- Added `FIXME` comment to `span_lint_and_sugg` to improve later
  • Loading branch information
CrazyRoka committed Apr 26, 2020
1 parent 63b451e commit 940c662
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
15 changes: 8 additions & 7 deletions 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};
Expand Down Expand Up @@ -38,7 +38,7 @@ declare_clippy_lint! {
/// }
/// ```
pub MATCH_ON_VEC_ITEMS,
style,
correctness,
"matching on vector elements can panic"
}

Expand All @@ -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
);
}
}
Expand Down
18 changes: 9 additions & 9 deletions 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;
Expand Down Expand Up @@ -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();
}
32 changes: 16 additions & 16 deletions 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)`
Expand Down

0 comments on commit 940c662

Please sign in to comment.