-
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 #12417 - Alexendoo:iter-nth, r=flip1995
Move `iter_nth` to `style`, add machine applicable suggestion There's no `O(n)` involved with `.iter().nth()` on the linted types since the iterator implementations provide `nth` and/or `advance_by` that operate in `O(1)` For slice iterators the codegen is equivalent, `VecDeque`'s iterator seems to codegen differently but that doesn't seem significant enough to keep it as a perf lint changelog: [`iter_nth`] Move to `style` r? `@flip1995`
- Loading branch information
Showing
5 changed files
with
139 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,44 @@ | ||
use super::utils::derefs_to_slice; | ||
use crate::methods::iter_nth_zero; | ||
use clippy_utils::diagnostics::span_lint_and_help; | ||
use clippy_utils::ty::is_type_diagnostic_item; | ||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use clippy_utils::ty::get_type_diagnostic_name; | ||
use rustc_errors::Applicability; | ||
use rustc_hir as hir; | ||
use rustc_lint::LateContext; | ||
use rustc_span::symbol::sym; | ||
use rustc_span::Span; | ||
|
||
use super::ITER_NTH; | ||
|
||
pub(super) fn check<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &hir::Expr<'_>, | ||
iter_recv: &'tcx hir::Expr<'tcx>, | ||
nth_recv: &hir::Expr<'_>, | ||
nth_arg: &hir::Expr<'_>, | ||
is_mut: bool, | ||
) { | ||
let mut_str = if is_mut { "_mut" } else { "" }; | ||
let caller_type = if derefs_to_slice(cx, iter_recv, cx.typeck_results().expr_ty(iter_recv)).is_some() { | ||
"slice" | ||
} else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(iter_recv), sym::Vec) { | ||
"`Vec`" | ||
} else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(iter_recv), sym::VecDeque) { | ||
"`VecDeque`" | ||
} else { | ||
iter_nth_zero::check(cx, expr, nth_recv, nth_arg); | ||
return; // caller is not a type that we want to lint | ||
iter_method: &str, | ||
iter_span: Span, | ||
nth_span: Span, | ||
) -> bool { | ||
let caller_type = match get_type_diagnostic_name(cx, cx.typeck_results().expr_ty(iter_recv).peel_refs()) { | ||
Some(sym::Vec) => "`Vec`", | ||
Some(sym::VecDeque) => "`VecDeque`", | ||
_ if cx.typeck_results().expr_ty_adjusted(iter_recv).peel_refs().is_slice() => "slice", | ||
// caller is not a type that we want to lint | ||
_ => return false, | ||
}; | ||
|
||
span_lint_and_help( | ||
span_lint_and_then( | ||
cx, | ||
ITER_NTH, | ||
expr.span, | ||
&format!("called `.iter{mut_str}().nth()` on a {caller_type}"), | ||
None, | ||
&format!("calling `.get{mut_str}()` is both faster and more readable"), | ||
&format!("called `.{iter_method}().nth()` on a {caller_type}"), | ||
|diag| { | ||
let get_method = if iter_method == "iter_mut" { "get_mut" } else { "get" }; | ||
diag.span_suggestion_verbose( | ||
iter_span.to(nth_span), | ||
format!("`{get_method}` is equivalent but more concise"), | ||
get_method, | ||
Applicability::MachineApplicable, | ||
); | ||
}, | ||
); | ||
|
||
true | ||
} |
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,60 @@ | ||
//@aux-build:option_helpers.rs | ||
|
||
#![warn(clippy::iter_nth)] | ||
#![allow(clippy::useless_vec)] | ||
|
||
#[macro_use] | ||
extern crate option_helpers; | ||
|
||
use option_helpers::IteratorFalsePositives; | ||
use std::collections::VecDeque; | ||
|
||
/// Struct to generate false positives for things with `.iter()`. | ||
#[derive(Copy, Clone)] | ||
struct HasIter; | ||
|
||
impl HasIter { | ||
fn iter(self) -> IteratorFalsePositives { | ||
IteratorFalsePositives { foo: 0 } | ||
} | ||
|
||
fn iter_mut(self) -> IteratorFalsePositives { | ||
IteratorFalsePositives { foo: 0 } | ||
} | ||
} | ||
|
||
/// Checks implementation of `ITER_NTH` lint. | ||
fn iter_nth() { | ||
let mut some_vec = vec![0, 1, 2, 3]; | ||
let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]); | ||
let mut some_vec_deque: VecDeque<_> = some_vec.iter().cloned().collect(); | ||
|
||
{ | ||
// Make sure we lint `.iter()` for relevant types. | ||
let bad_vec = some_vec.get(3); | ||
let bad_slice = &some_vec[..].get(3); | ||
let bad_boxed_slice = boxed_slice.get(3); | ||
let bad_vec_deque = some_vec_deque.get(3); | ||
} | ||
|
||
{ | ||
// Make sure we lint `.iter_mut()` for relevant types. | ||
let bad_vec = some_vec.get_mut(3); | ||
} | ||
{ | ||
let bad_slice = &some_vec[..].get_mut(3); | ||
} | ||
{ | ||
let bad_vec_deque = some_vec_deque.get_mut(3); | ||
} | ||
|
||
let vec_ref = &Vec::<String>::new(); | ||
vec_ref.get(3); | ||
|
||
// Make sure we don't lint for non-relevant types. | ||
let false_positive = HasIter; | ||
let ok = false_positive.iter().nth(3); | ||
let ok_mut = false_positive.iter_mut().nth(3); | ||
} | ||
|
||
fn main() {} |
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