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
This commit is contained in:
CrazyRoka 2020-04-26 17:57:19 +03:00
parent 63b451ea25
commit 940c662654
3 changed files with 33 additions and 32 deletions

View File

@ -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
);
}
}

View File

@ -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();
}

View File

@ -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)`