-
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.
new lint to use contains() instead of iter().any() for u8 and i8 slices
- Loading branch information
1 parent
c2d23ad
commit acb27cb
Showing
6 changed files
with
118 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use crate::methods::method_call; | ||
use clippy_utils::diagnostics::span_lint; | ||
use rustc_hir::Expr; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_middle::ty::{self}; | ||
use rustc_session::declare_lint_pass; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for usage of `iter().any()` on slices of `u8` or `i8` and suggests using `contains()` instead. | ||
/// | ||
/// ### Why is this bad? | ||
/// `iter().any()` on slices of `u8` or `i8` is optimized to use `memchr`. | ||
/// | ||
/// ### Example | ||
/// ```no_run | ||
/// fn foo(values: &[u8]) -> bool { | ||
/// values.iter().any(|&v| v == 10) | ||
/// } | ||
/// ``` | ||
/// Use instead: | ||
/// ```no_run | ||
/// fn foo(values: &[u8]) -> bool { | ||
/// values.contains(&10) | ||
/// } | ||
/// ``` | ||
#[clippy::version = "1.85.0"] | ||
pub CONTAINS_FOR_SLICE, | ||
perf, | ||
"using `contains()` instead of `iter().any()` on u8/i8 slices is more efficient" | ||
} | ||
|
||
declare_lint_pass!(ContainsForSlice => [CONTAINS_FOR_SLICE]); | ||
|
||
impl LateLintPass<'_> for ContainsForSlice { | ||
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { | ||
if !expr.span.from_expansion() | ||
// any() | ||
&& let Some((name, recv, _, _, _)) = method_call(expr) | ||
&& name == "any" | ||
// iter() | ||
&& let Some((name, recv, _, _, _)) = method_call(recv) | ||
&& name == "iter" | ||
{ | ||
// check if the receiver is a u8/i8 slice | ||
let ref_type = cx.typeck_results().expr_ty(recv); | ||
|
||
match ref_type.kind() { | ||
ty::Ref(_, inner_type, _) if inner_type.is_slice() => { | ||
if let ty::Slice(slice_type) = inner_type.kind() | ||
&& (slice_type.to_string() == "u8" || slice_type.to_string() == "i8") | ||
{ | ||
span_lint( | ||
cx, | ||
CONTAINS_FOR_SLICE, | ||
expr.span, | ||
"using `contains()` instead of `iter().any()` on u8/i8 slices is more efficient", | ||
); | ||
} | ||
}, | ||
_ => {}, | ||
} | ||
} | ||
} | ||
} |
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,32 @@ | ||
#![warn(clippy::contains_for_slice)] | ||
|
||
fn main() { | ||
let vec: Vec<u8> = vec![1, 2, 3, 4, 5, 6]; | ||
let values = &vec[..]; | ||
let _ = values.iter().any(|&v| v == 4); | ||
//~^ ERROR: using `contains()` instead of `iter().any()` on u8/i8 slices is more efficient | ||
|
||
let vec: Vec<u8> = vec![1, 2, 3, 4, 5, 6]; | ||
let values = &vec[..]; | ||
let _ = values.contains(&4); | ||
// no error, because it uses `contains()` | ||
|
||
let vec: Vec<u32> = vec![1, 2, 3, 4, 5, 6]; | ||
let values = &vec[..]; | ||
let _ = values.iter().any(|&v| v == 4); | ||
// no error, because it's not a slice of u8/i8 | ||
|
||
let values: [u8; 6] = [3, 14, 15, 92, 6, 5]; | ||
let _ = values.iter().any(|&v| v == 10); | ||
// no error, because it's an array | ||
} | ||
|
||
fn foo(values: &[u8]) -> bool { | ||
values.iter().any(|&v| v == 10) | ||
//~^ ERROR: using `contains()` instead of `iter().any()` on u8/i8 slices is more efficient | ||
} | ||
|
||
fn bar(values: [u8; 3]) -> bool { | ||
values.iter().any(|&v| v == 10) | ||
// no error, because it's an 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,17 @@ | ||
error: using `contains()` instead of `iter().any()` on u8/i8 slices is more efficient | ||
--> tests/ui/contains_for_slice.rs:6:13 | ||
| | ||
LL | let _ = values.iter().any(|&v| v == 4); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::contains-for-slice` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::contains_for_slice)]` | ||
|
||
error: using `contains()` instead of `iter().any()` on u8/i8 slices is more efficient | ||
--> tests/ui/contains_for_slice.rs:25:5 | ||
| | ||
LL | values.iter().any(|&v| v == 10) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 2 previous errors | ||
|