-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #5522 - CrazyRoka:match_vec_item, r=phansch
New lint `match_vec_item` Added new lint to warn a match on index item which can panic. It's always better to use `get(..)` instead. Closes #5500 changelog: New lint `match_on_vec_items`
- Loading branch information
Showing
7 changed files
with
287 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
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}; | ||
use rustc_lint::{LateContext, LateLintPass, LintContext}; | ||
use rustc_middle::lint::in_external_macro; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** Checks for `match vec[idx]` or `match vec[n..m]`. | ||
/// | ||
/// **Why is this bad?** This can panic at runtime. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// ```rust, no_run | ||
/// let arr = vec![0, 1, 2, 3]; | ||
/// let idx = 1; | ||
/// | ||
/// // Bad | ||
/// match arr[idx] { | ||
/// 0 => println!("{}", 0), | ||
/// 1 => println!("{}", 3), | ||
/// _ => {}, | ||
/// } | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust, no_run | ||
/// let arr = vec![0, 1, 2, 3]; | ||
/// let idx = 1; | ||
/// | ||
/// // Good | ||
/// match arr.get(idx) { | ||
/// Some(0) => println!("{}", 0), | ||
/// Some(1) => println!("{}", 3), | ||
/// _ => {}, | ||
/// } | ||
/// ``` | ||
pub MATCH_ON_VEC_ITEMS, | ||
correctness, | ||
"matching on vector elements can panic" | ||
} | ||
|
||
declare_lint_pass!(MatchOnVecItems => [MATCH_ON_VEC_ITEMS]); | ||
|
||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MatchOnVecItems { | ||
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'tcx>) { | ||
if_chain! { | ||
if !in_external_macro(cx.sess(), expr.span); | ||
if let ExprKind::Match(ref match_expr, _, MatchSource::Normal) = expr.kind; | ||
if let Some(idx_expr) = is_vec_indexing(cx, match_expr); | ||
if let ExprKind::Index(vec, idx) = idx_expr.kind; | ||
|
||
then { | ||
// 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 into a vector may panic", | ||
"try this", | ||
format!( | ||
"{}.get({})", | ||
snippet(cx, vec.span, ".."), | ||
snippet(cx, idx.span, "..") | ||
), | ||
Applicability::MaybeIncorrect | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn is_vec_indexing<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'tcx>) -> Option<&'tcx Expr<'tcx>> { | ||
if_chain! { | ||
if let ExprKind::Index(ref array, _) = expr.kind; | ||
let ty = cx.tables.expr_ty(array); | ||
let ty = walk_ptrs_ty(ty); | ||
if is_type_diagnostic_item(cx, ty, sym!(vec_type)); | ||
|
||
then { | ||
return Some(expr); | ||
} | ||
} | ||
|
||
None | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
#![warn(clippy::match_on_vec_items)] | ||
|
||
fn match_with_wildcard() { | ||
let arr = vec![0, 1, 2, 3]; | ||
let range = 1..3; | ||
let idx = 1; | ||
|
||
// Lint, may panic | ||
match arr[idx] { | ||
0 => println!("0"), | ||
1 => println!("1"), | ||
_ => {}, | ||
} | ||
|
||
// Lint, may panic | ||
match arr[range] { | ||
[0, 1] => println!("0 1"), | ||
[1, 2] => println!("1 2"), | ||
_ => {}, | ||
} | ||
} | ||
|
||
fn match_without_wildcard() { | ||
let arr = vec![0, 1, 2, 3]; | ||
let range = 1..3; | ||
let idx = 2; | ||
|
||
// Lint, may panic | ||
match arr[idx] { | ||
0 => println!("0"), | ||
1 => println!("1"), | ||
num => {}, | ||
} | ||
|
||
// Lint, may panic | ||
match arr[range] { | ||
[0, 1] => println!("0 1"), | ||
[1, 2] => println!("1 2"), | ||
[ref sub @ ..] => {}, | ||
} | ||
} | ||
|
||
fn match_wildcard_and_action() { | ||
let arr = vec![0, 1, 2, 3]; | ||
let range = 1..3; | ||
let idx = 3; | ||
|
||
// Lint, may panic | ||
match arr[idx] { | ||
0 => println!("0"), | ||
1 => println!("1"), | ||
_ => println!("Hello, World!"), | ||
} | ||
|
||
// Lint, may panic | ||
match arr[range] { | ||
[0, 1] => println!("0 1"), | ||
[1, 2] => println!("1 2"), | ||
_ => println!("Hello, World!"), | ||
} | ||
} | ||
|
||
fn match_vec_ref() { | ||
let arr = &vec![0, 1, 2, 3]; | ||
let range = 1..3; | ||
let idx = 3; | ||
|
||
// Lint, may panic | ||
match arr[idx] { | ||
0 => println!("0"), | ||
1 => println!("1"), | ||
_ => {}, | ||
} | ||
|
||
// Lint, may panic | ||
match arr[range] { | ||
[0, 1] => println!("0 1"), | ||
[1, 2] => println!("1 2"), | ||
_ => {}, | ||
} | ||
} | ||
|
||
fn match_with_get() { | ||
let arr = vec![0, 1, 2, 3]; | ||
let range = 1..3; | ||
let idx = 3; | ||
|
||
// Ok | ||
match arr.get(idx) { | ||
Some(0) => println!("0"), | ||
Some(1) => println!("1"), | ||
_ => {}, | ||
} | ||
|
||
// Ok | ||
match arr.get(range) { | ||
Some(&[0, 1]) => println!("0 1"), | ||
Some(&[1, 2]) => println!("1 2"), | ||
_ => {}, | ||
} | ||
} | ||
|
||
fn match_with_array() { | ||
let arr = [0, 1, 2, 3]; | ||
let range = 1..3; | ||
let idx = 3; | ||
|
||
// Ok | ||
match arr[idx] { | ||
0 => println!("0"), | ||
1 => println!("1"), | ||
_ => {}, | ||
} | ||
|
||
// Ok | ||
match arr[range] { | ||
[0, 1] => println!("0 1"), | ||
[1, 2] => println!("1 2"), | ||
_ => {}, | ||
} | ||
} | ||
|
||
fn main() { | ||
match_with_wildcard(); | ||
match_without_wildcard(); | ||
match_wildcard_and_action(); | ||
match_vec_ref(); | ||
match_with_get(); | ||
match_with_array(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
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 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 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 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 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 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 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 into a vector may panic | ||
--> $DIR/match_on_vec_items.rs:76:11 | ||
| | ||
LL | match arr[range] { | ||
| ^^^^^^^^^^ help: try this: `arr.get(range)` | ||
|
||
error: aborting due to 8 previous errors | ||
|