Skip to content

Commit 76ddac5

Browse files
committed
Auto merge of #5560 - CrazyRoka:fix-match-on-vector-full-range, r=phansch,flip1995
Fix match on vec items: match on vec[..] - Added new tests - Fixed false positive when matching on full range, which will never panic Closes #5551 changelog: fix match_on_vec_items when matching full range
2 parents 75a7171 + de58c56 commit 76ddac5

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

clippy_lints/src/match_on_vec_items.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{is_type_diagnostic_item, snippet, span_lint_and_sugg, walk_ptrs_ty};
1+
use crate::utils::{self, is_type_diagnostic_item, match_type, 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};
@@ -75,10 +75,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MatchOnVecItems {
7575

7676
fn is_vec_indexing<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'tcx>) -> Option<&'tcx Expr<'tcx>> {
7777
if_chain! {
78-
if let ExprKind::Index(ref array, _) = expr.kind;
79-
let ty = cx.tables.expr_ty(array);
80-
let ty = walk_ptrs_ty(ty);
81-
if is_type_diagnostic_item(cx, ty, sym!(vec_type));
78+
if let ExprKind::Index(ref array, ref index) = expr.kind;
79+
if is_vector(cx, array);
80+
if !is_full_range(cx, index);
8281

8382
then {
8483
return Some(expr);
@@ -87,3 +86,15 @@ fn is_vec_indexing<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'tcx>)
8786

8887
None
8988
}
89+
90+
fn is_vector(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> bool {
91+
let ty = cx.tables.expr_ty(expr);
92+
let ty = walk_ptrs_ty(ty);
93+
is_type_diagnostic_item(cx, ty, sym!(vec_type))
94+
}
95+
96+
fn is_full_range(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> bool {
97+
let ty = cx.tables.expr_ty(expr);
98+
let ty = walk_ptrs_ty(ty);
99+
match_type(cx, ty, &utils::paths::RANGE_FULL)
100+
}

clippy_lints/src/utils/paths.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub const RANGE: [&str; 3] = ["core", "ops", "Range"];
8585
pub const RANGE_ARGUMENT_TRAIT: [&str; 3] = ["core", "ops", "RangeBounds"];
8686
pub const RANGE_FROM: [&str; 3] = ["core", "ops", "RangeFrom"];
8787
pub const RANGE_FROM_STD: [&str; 3] = ["std", "ops", "RangeFrom"];
88-
pub const RANGE_FULL: [&str; 3] = ["core", "ops", "RangeFull"];
88+
pub const RANGE_FULL: [&str; 4] = ["core", "ops", "range", "RangeFull"];
8989
pub const RANGE_FULL_STD: [&str; 3] = ["std", "ops", "RangeFull"];
9090
pub const RANGE_INCLUSIVE_NEW: [&str; 4] = ["core", "ops", "RangeInclusive", "new"];
9191
pub const RANGE_INCLUSIVE_STD_NEW: [&str; 4] = ["std", "ops", "RangeInclusive", "new"];

tests/ui/match_on_vec_items.rs

+22
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,33 @@ fn match_with_array() {
120120
}
121121
}
122122

123+
fn match_with_endless_range() {
124+
let arr = vec![0, 1, 2, 3];
125+
let range = ..;
126+
127+
// Ok
128+
match arr[range] {
129+
[0, 1] => println!("0 1"),
130+
[1, 2] => println!("1 2"),
131+
[0, 1, 2, 3] => println!("0, 1, 2, 3"),
132+
_ => {},
133+
}
134+
135+
// Ok
136+
match arr[..] {
137+
[0, 1] => println!("0 1"),
138+
[1, 2] => println!("1 2"),
139+
[0, 1, 2, 3] => println!("0, 1, 2, 3"),
140+
_ => {},
141+
}
142+
}
143+
123144
fn main() {
124145
match_with_wildcard();
125146
match_without_wildcard();
126147
match_wildcard_and_action();
127148
match_vec_ref();
128149
match_with_get();
129150
match_with_array();
151+
match_with_endless_range();
130152
}

0 commit comments

Comments
 (0)