Skip to content

Commit 940c662

Browse files
committed
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
1 parent 63b451e commit 940c662

File tree

3 files changed

+33
-32
lines changed

3 files changed

+33
-32
lines changed

clippy_lints/src/match_on_vec_items.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{is_type_diagnostic_item, snippet_with_applicability, span_lint_and_sugg, walk_ptrs_ty};
1+
use crate::utils::{is_type_diagnostic_item, snippet, span_lint_and_sugg, walk_ptrs_ty};
22
use if_chain::if_chain;
33
use rustc_errors::Applicability;
44
use rustc_hir::{Expr, ExprKind, MatchSource};
@@ -38,7 +38,7 @@ declare_clippy_lint! {
3838
/// }
3939
/// ```
4040
pub MATCH_ON_VEC_ITEMS,
41-
style,
41+
correctness,
4242
"matching on vector elements can panic"
4343
}
4444

@@ -53,19 +53,20 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MatchOnVecItems {
5353
if let ExprKind::Index(vec, idx) = idx_expr.kind;
5454

5555
then {
56-
let mut applicability = Applicability::MaybeIncorrect;
56+
// FIXME: could be improved to suggest surrounding every pattern with Some(_),
57+
// but only when `or_patterns` are stabilized.
5758
span_lint_and_sugg(
5859
cx,
5960
MATCH_ON_VEC_ITEMS,
6061
match_expr.span,
61-
"indexing vector may panic. Consider using `get`",
62+
"indexing into a vector may panic",
6263
"try this",
6364
format!(
6465
"{}.get({})",
65-
snippet_with_applicability(cx, vec.span, "..", &mut applicability),
66-
snippet_with_applicability(cx, idx.span, "..", &mut applicability)
66+
snippet(cx, vec.span, ".."),
67+
snippet(cx, idx.span, "..")
6768
),
68-
applicability
69+
Applicability::MaybeIncorrect
6970
);
7071
}
7172
}

tests/ui/match_on_vec_items.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
#![warn(clippy::match_on_vec_items)]
22

3-
fn main() {
4-
match_with_wildcard();
5-
match_without_wildcard();
6-
match_wildcard_and_action();
7-
match_vec_ref();
8-
match_with_get();
9-
match_with_array();
10-
}
11-
123
fn match_with_wildcard() {
134
let arr = vec![0, 1, 2, 3];
145
let range = 1..3;
@@ -128,3 +119,12 @@ fn match_with_array() {
128119
_ => {},
129120
}
130121
}
122+
123+
fn main() {
124+
match_with_wildcard();
125+
match_without_wildcard();
126+
match_wildcard_and_action();
127+
match_vec_ref();
128+
match_with_get();
129+
match_with_array();
130+
}

tests/ui/match_on_vec_items.stderr

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
1-
error: indexing vector may panic. Consider using `get`
2-
--> $DIR/match_on_vec_items.rs:18:11
1+
error: indexing into a vector may panic
2+
--> $DIR/match_on_vec_items.rs:9:11
33
|
44
LL | match arr[idx] {
55
| ^^^^^^^^ help: try this: `arr.get(idx)`
66
|
77
= note: `-D clippy::match-on-vec-items` implied by `-D warnings`
88

9-
error: indexing vector may panic. Consider using `get`
10-
--> $DIR/match_on_vec_items.rs:25:11
9+
error: indexing into a vector may panic
10+
--> $DIR/match_on_vec_items.rs:16:11
1111
|
1212
LL | match arr[range] {
1313
| ^^^^^^^^^^ help: try this: `arr.get(range)`
1414

15-
error: indexing vector may panic. Consider using `get`
16-
--> $DIR/match_on_vec_items.rs:38:11
15+
error: indexing into a vector may panic
16+
--> $DIR/match_on_vec_items.rs:29:11
1717
|
1818
LL | match arr[idx] {
1919
| ^^^^^^^^ help: try this: `arr.get(idx)`
2020

21-
error: indexing vector may panic. Consider using `get`
22-
--> $DIR/match_on_vec_items.rs:45:11
21+
error: indexing into a vector may panic
22+
--> $DIR/match_on_vec_items.rs:36:11
2323
|
2424
LL | match arr[range] {
2525
| ^^^^^^^^^^ help: try this: `arr.get(range)`
2626

27-
error: indexing vector may panic. Consider using `get`
28-
--> $DIR/match_on_vec_items.rs:58:11
27+
error: indexing into a vector may panic
28+
--> $DIR/match_on_vec_items.rs:49:11
2929
|
3030
LL | match arr[idx] {
3131
| ^^^^^^^^ help: try this: `arr.get(idx)`
3232

33-
error: indexing vector may panic. Consider using `get`
34-
--> $DIR/match_on_vec_items.rs:65:11
33+
error: indexing into a vector may panic
34+
--> $DIR/match_on_vec_items.rs:56:11
3535
|
3636
LL | match arr[range] {
3737
| ^^^^^^^^^^ help: try this: `arr.get(range)`
3838

39-
error: indexing vector may panic. Consider using `get`
40-
--> $DIR/match_on_vec_items.rs:78:11
39+
error: indexing into a vector may panic
40+
--> $DIR/match_on_vec_items.rs:69:11
4141
|
4242
LL | match arr[idx] {
4343
| ^^^^^^^^ help: try this: `arr.get(idx)`
4444

45-
error: indexing vector may panic. Consider using `get`
46-
--> $DIR/match_on_vec_items.rs:85:11
45+
error: indexing into a vector may panic
46+
--> $DIR/match_on_vec_items.rs:76:11
4747
|
4848
LL | match arr[range] {
4949
| ^^^^^^^^^^ help: try this: `arr.get(range)`

0 commit comments

Comments
 (0)