-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New lint match_vec_item
#5522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
New lint match_vec_item
#5522
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
96e2bc8
Added lint `match_vec_item`
CrazyRoka b0115fb
Removed unnecessary code, added support for vector references
CrazyRoka 63b451e
Renamed lint to `match_on_vec_items`
CrazyRoka 940c662
Small lint update
CrazyRoka b574941
Updated lint info in lib.rs
CrazyRoka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,111 @@ | ||
use crate::utils::{is_wild, span_lint_and_help}; | ||
use if_chain::if_chain; | ||
use rustc_hir::{Arm, Expr, ExprKind, MatchSource}; | ||
use rustc_lint::{LateContext, LateLintPass, LintContext}; | ||
use rustc_middle::lint::in_external_macro; | ||
use rustc_middle::ty::{self, AdtDef}; | ||
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_VEC_ITEM, | ||
style, | ||
"match vector by indexing can panic" | ||
CrazyRoka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
declare_lint_pass!(MatchVecItem => [MATCH_VEC_ITEM]); | ||
|
||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MatchVecItem { | ||
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 ex, ref arms, MatchSource::Normal) = expr.kind; | ||
if contains_wild_arm(arms); | ||
CrazyRoka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if is_vec_indexing(cx, ex); | ||
|
||
then { | ||
span_lint_and_help( | ||
cx, | ||
MATCH_VEC_ITEM, | ||
expr.span, | ||
CrazyRoka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"indexing vector may panic", | ||
None, | ||
"consider using `get(..)` instead.", | ||
CrazyRoka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn contains_wild_arm(arms: &[Arm<'_>]) -> bool { | ||
arms.iter().any(|arm| is_wild(&arm.pat) && is_unit_expr(arm.body)) | ||
} | ||
|
||
fn is_vec_indexing<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'tcx>) -> bool { | ||
if_chain! { | ||
if let ExprKind::Index(ref array, _) = expr.kind; | ||
let ty = cx.tables.expr_ty(array); | ||
CrazyRoka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if let ty::Adt(def, _) = ty.kind; | ||
if is_vec(cx, def); | ||
CrazyRoka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
then { | ||
return true; | ||
} | ||
} | ||
|
||
false | ||
} | ||
|
||
fn is_vec<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, def: &'a AdtDef) -> bool { | ||
if_chain! { | ||
let def_path = cx.tcx.def_path(def.did); | ||
if def_path.data.len() == 2; | ||
if let Some(module) = def_path.data.get(0); | ||
if module.data.as_symbol() == sym!(vec); | ||
if let Some(name) = def_path.data.get(1); | ||
if name.data.as_symbol() == sym!(Vec); | ||
|
||
then { | ||
return true; | ||
} | ||
} | ||
|
||
false | ||
} | ||
|
||
fn is_unit_expr(expr: &Expr<'_>) -> bool { | ||
match expr.kind { | ||
ExprKind::Tup(ref v) if v.is_empty() => true, | ||
ExprKind::Block(ref b, _) if b.stmts.is_empty() && b.expr.is_none() => true, | ||
_ => false, | ||
} | ||
} |
This file contains hidden or 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 hidden or 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,109 @@ | ||
#![warn(clippy::match_vec_item)] | ||
|
||
fn main() { | ||
match_with_wildcard(); | ||
match_without_wildcard(); | ||
match_wildcard_and_action(); | ||
match_with_get(); | ||
match_with_array(); | ||
} | ||
|
||
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; | ||
|
||
// Ok | ||
match arr[idx] { | ||
0 => println!("0"), | ||
1 => println!("1"), | ||
num => {}, | ||
} | ||
|
||
// Ok | ||
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; | ||
|
||
// Ok | ||
match arr[idx] { | ||
0 => println!("0"), | ||
1 => println!("1"), | ||
_ => println!("Hello, World!"), | ||
} | ||
|
||
// Ok | ||
match arr[range] { | ||
[0, 1] => println!("0 1"), | ||
[1, 2] => println!("1 2"), | ||
_ => println!("Hello, World!"), | ||
} | ||
} | ||
|
||
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"), | ||
_ => {}, | ||
} | ||
} |
This file contains hidden or 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,27 @@ | ||
error: indexing vector may panic | ||
--> $DIR/match_vec_item.rs:17:5 | ||
| | ||
LL | / match arr[idx] { | ||
LL | | 0 => println!("0"), | ||
LL | | 1 => println!("1"), | ||
LL | | _ => {}, | ||
LL | | } | ||
| |_____^ | ||
| | ||
= note: `-D clippy::match-vec-item` implied by `-D warnings` | ||
= help: consider using `get(..)` instead. | ||
|
||
error: indexing vector may panic | ||
--> $DIR/match_vec_item.rs:24:5 | ||
| | ||
LL | / match arr[range] { | ||
LL | | [0, 1] => println!("0 1"), | ||
LL | | [1, 2] => println!("1 2"), | ||
LL | | _ => {}, | ||
LL | | } | ||
| |_____^ | ||
| | ||
= help: consider using `get(..)` instead. | ||
|
||
error: aborting due to 2 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.