Move check_for_loop_arg back to mod; split into 4 lint files
This commit is contained in:
parent
7cfdef6de1
commit
ecebfe0c9c
20
clippy_lints/src/loops/explicit_into_iter_loop.rs
Normal file
20
clippy_lints/src/loops/explicit_into_iter_loop.rs
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
use super::EXPLICIT_INTO_ITER_LOOP;
|
||||||
|
use crate::utils::{snippet_with_applicability, span_lint_and_sugg};
|
||||||
|
use rustc_errors::Applicability;
|
||||||
|
use rustc_hir::Expr;
|
||||||
|
use rustc_lint::LateContext;
|
||||||
|
|
||||||
|
pub(super) fn check_explicit_into_iter_loop(cx: &LateContext<'_>, method_args: &'hir [Expr<'hir>], arg: &Expr<'_>) {
|
||||||
|
let mut applicability = Applicability::MachineApplicable;
|
||||||
|
let object = snippet_with_applicability(cx, method_args[0].span, "_", &mut applicability);
|
||||||
|
span_lint_and_sugg(
|
||||||
|
cx,
|
||||||
|
EXPLICIT_INTO_ITER_LOOP,
|
||||||
|
arg.span,
|
||||||
|
"it is more concise to loop over containers instead of using explicit \
|
||||||
|
iteration methods",
|
||||||
|
"to write this more concisely, try",
|
||||||
|
object.to_string(),
|
||||||
|
applicability,
|
||||||
|
);
|
||||||
|
}
|
21
clippy_lints/src/loops/explicit_iter_loop.rs
Normal file
21
clippy_lints/src/loops/explicit_iter_loop.rs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
use super::EXPLICIT_ITER_LOOP;
|
||||||
|
use crate::utils::{snippet_with_applicability, span_lint_and_sugg};
|
||||||
|
use rustc_errors::Applicability;
|
||||||
|
use rustc_hir::Expr;
|
||||||
|
use rustc_lint::LateContext;
|
||||||
|
|
||||||
|
pub(super) fn lint_iter_method(cx: &LateContext<'_>, args: &[Expr<'_>], arg: &Expr<'_>, method_name: &str) {
|
||||||
|
let mut applicability = Applicability::MachineApplicable;
|
||||||
|
let object = snippet_with_applicability(cx, args[0].span, "_", &mut applicability);
|
||||||
|
let muta = if method_name == "iter_mut" { "mut " } else { "" };
|
||||||
|
span_lint_and_sugg(
|
||||||
|
cx,
|
||||||
|
EXPLICIT_ITER_LOOP,
|
||||||
|
arg.span,
|
||||||
|
"it is more concise to loop over references to containers instead of using explicit \
|
||||||
|
iteration methods",
|
||||||
|
"to write this more concisely, try",
|
||||||
|
format!("&{}{}", muta, object),
|
||||||
|
applicability,
|
||||||
|
)
|
||||||
|
}
|
45
clippy_lints/src/loops/for_loops_over_fallibles.rs
Normal file
45
clippy_lints/src/loops/for_loops_over_fallibles.rs
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
use super::FOR_LOOPS_OVER_FALLIBLES;
|
||||||
|
use crate::utils::{is_type_diagnostic_item, snippet, span_lint_and_help};
|
||||||
|
use rustc_hir::{Expr, Pat};
|
||||||
|
use rustc_lint::LateContext;
|
||||||
|
use rustc_span::symbol::sym;
|
||||||
|
|
||||||
|
/// Checks for `for` loops over `Option`s and `Result`s.
|
||||||
|
pub(super) fn check_arg_type(cx: &LateContext<'_>, pat: &Pat<'_>, arg: &Expr<'_>) {
|
||||||
|
let ty = cx.typeck_results().expr_ty(arg);
|
||||||
|
if is_type_diagnostic_item(cx, ty, sym::option_type) {
|
||||||
|
span_lint_and_help(
|
||||||
|
cx,
|
||||||
|
FOR_LOOPS_OVER_FALLIBLES,
|
||||||
|
arg.span,
|
||||||
|
&format!(
|
||||||
|
"for loop over `{0}`, which is an `Option`. This is more readably written as an \
|
||||||
|
`if let` statement",
|
||||||
|
snippet(cx, arg.span, "_")
|
||||||
|
),
|
||||||
|
None,
|
||||||
|
&format!(
|
||||||
|
"consider replacing `for {0} in {1}` with `if let Some({0}) = {1}`",
|
||||||
|
snippet(cx, pat.span, "_"),
|
||||||
|
snippet(cx, arg.span, "_")
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} else if is_type_diagnostic_item(cx, ty, sym::result_type) {
|
||||||
|
span_lint_and_help(
|
||||||
|
cx,
|
||||||
|
FOR_LOOPS_OVER_FALLIBLES,
|
||||||
|
arg.span,
|
||||||
|
&format!(
|
||||||
|
"for loop over `{0}`, which is a `Result`. This is more readably written as an \
|
||||||
|
`if let` statement",
|
||||||
|
snippet(cx, arg.span, "_")
|
||||||
|
),
|
||||||
|
None,
|
||||||
|
&format!(
|
||||||
|
"consider replacing `for {0} in {1}` with `if let Ok({0}) = {1}`",
|
||||||
|
snippet(cx, pat.span, "_"),
|
||||||
|
snippet(cx, arg.span, "_")
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
14
clippy_lints/src/loops/iter_next_loop.rs
Normal file
14
clippy_lints/src/loops/iter_next_loop.rs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
use super::ITER_NEXT_LOOP;
|
||||||
|
use crate::utils::span_lint;
|
||||||
|
use rustc_hir::Expr;
|
||||||
|
use rustc_lint::LateContext;
|
||||||
|
|
||||||
|
pub(super) fn lint(cx: &LateContext<'_>, expr: &Expr<'_>) {
|
||||||
|
span_lint(
|
||||||
|
cx,
|
||||||
|
ITER_NEXT_LOOP,
|
||||||
|
expr.span,
|
||||||
|
"you are iterating over `Iterator::next()` which is an Option; this will compile but is \
|
||||||
|
probably not what you want",
|
||||||
|
);
|
||||||
|
}
|
@ -1,11 +1,14 @@
|
|||||||
mod empty_loop;
|
mod empty_loop;
|
||||||
mod explicit_counter_loop;
|
mod explicit_counter_loop;
|
||||||
mod for_loop_arg;
|
mod explicit_into_iter_loop;
|
||||||
|
mod explicit_iter_loop;
|
||||||
mod for_loop_over_map_kv;
|
mod for_loop_over_map_kv;
|
||||||
mod for_loop_range;
|
mod for_loop_range;
|
||||||
|
mod for_loops_over_fallibles;
|
||||||
mod for_mut_range_bound;
|
mod for_mut_range_bound;
|
||||||
mod for_single_element_loop;
|
mod for_single_element_loop;
|
||||||
mod infinite_loop;
|
mod infinite_loop;
|
||||||
|
mod iter_next_loop;
|
||||||
mod manual_flatten;
|
mod manual_flatten;
|
||||||
mod manual_memcpy;
|
mod manual_memcpy;
|
||||||
mod needless_collect;
|
mod needless_collect;
|
||||||
@ -15,11 +18,13 @@ mod utils;
|
|||||||
mod while_let_loop;
|
mod while_let_loop;
|
||||||
mod while_let_on_iterator;
|
mod while_let_on_iterator;
|
||||||
|
|
||||||
use crate::utils::higher;
|
use crate::utils::{higher, is_type_diagnostic_item, match_trait_method, match_type, paths};
|
||||||
use rustc_hir::{Expr, ExprKind, LoopSource, Pat};
|
use rustc_hir::{Expr, ExprKind, LoopSource, Mutability, Pat};
|
||||||
use rustc_lint::{LateContext, LateLintPass};
|
use rustc_lint::{LateContext, LateLintPass};
|
||||||
|
use rustc_middle::ty::{self, Ty, TyS};
|
||||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||||
use rustc_span::source_map::Span;
|
use rustc_span::source_map::Span;
|
||||||
|
use rustc_span::symbol::sym;
|
||||||
use utils::{get_span_of_entire_for_loop, make_iterator_snippet, IncrementVisitor, InitializeVisitor};
|
use utils::{get_span_of_entire_for_loop, make_iterator_snippet, IncrementVisitor, InitializeVisitor};
|
||||||
|
|
||||||
declare_clippy_lint! {
|
declare_clippy_lint! {
|
||||||
@ -588,10 +593,77 @@ fn check_for_loop<'tcx>(
|
|||||||
for_loop_range::check_for_loop_range(cx, pat, arg, body, expr);
|
for_loop_range::check_for_loop_range(cx, pat, arg, body, expr);
|
||||||
explicit_counter_loop::check_for_loop_explicit_counter(cx, pat, arg, body, expr);
|
explicit_counter_loop::check_for_loop_explicit_counter(cx, pat, arg, body, expr);
|
||||||
}
|
}
|
||||||
for_loop_arg::check_for_loop_arg(cx, pat, arg, expr);
|
check_for_loop_arg(cx, pat, arg, expr);
|
||||||
for_loop_over_map_kv::check_for_loop_over_map_kv(cx, pat, arg, body, expr);
|
for_loop_over_map_kv::check_for_loop_over_map_kv(cx, pat, arg, body, expr);
|
||||||
for_mut_range_bound::check_for_mut_range_bound(cx, arg, body);
|
for_mut_range_bound::check_for_mut_range_bound(cx, arg, body);
|
||||||
for_single_element_loop::check_for_single_element_loop(cx, pat, arg, body, expr);
|
for_single_element_loop::check_for_single_element_loop(cx, pat, arg, body, expr);
|
||||||
same_item_push::detect_same_item_push(cx, pat, arg, body, expr);
|
same_item_push::detect_same_item_push(cx, pat, arg, body, expr);
|
||||||
manual_flatten::check_manual_flatten(cx, pat, arg, body, span);
|
manual_flatten::check_manual_flatten(cx, pat, arg, body, span);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn check_for_loop_arg(cx: &LateContext<'_>, pat: &Pat<'_>, arg: &Expr<'_>, expr: &Expr<'_>) {
|
||||||
|
let mut next_loop_linted = false; // whether or not ITER_NEXT_LOOP lint was used
|
||||||
|
if let ExprKind::MethodCall(ref method, _, ref args, _) = arg.kind {
|
||||||
|
// just the receiver, no arguments
|
||||||
|
if args.len() == 1 {
|
||||||
|
let method_name = &*method.ident.as_str();
|
||||||
|
// check for looping over x.iter() or x.iter_mut(), could use &x or &mut x
|
||||||
|
if method_name == "iter" || method_name == "iter_mut" {
|
||||||
|
if is_ref_iterable_type(cx, &args[0]) {
|
||||||
|
explicit_iter_loop::lint_iter_method(cx, args, arg, method_name);
|
||||||
|
}
|
||||||
|
} else if method_name == "into_iter" && match_trait_method(cx, arg, &paths::INTO_ITERATOR) {
|
||||||
|
let receiver_ty = cx.typeck_results().expr_ty(&args[0]);
|
||||||
|
let receiver_ty_adjusted = cx.typeck_results().expr_ty_adjusted(&args[0]);
|
||||||
|
if TyS::same_type(receiver_ty, receiver_ty_adjusted) {
|
||||||
|
explicit_into_iter_loop::check_explicit_into_iter_loop(cx, args, arg);
|
||||||
|
} else {
|
||||||
|
let ref_receiver_ty = cx.tcx.mk_ref(
|
||||||
|
cx.tcx.lifetimes.re_erased,
|
||||||
|
ty::TypeAndMut {
|
||||||
|
ty: receiver_ty,
|
||||||
|
mutbl: Mutability::Not,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
if TyS::same_type(receiver_ty_adjusted, ref_receiver_ty) {
|
||||||
|
explicit_iter_loop::lint_iter_method(cx, args, arg, method_name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if method_name == "next" && match_trait_method(cx, arg, &paths::ITERATOR) {
|
||||||
|
iter_next_loop::lint(cx, expr);
|
||||||
|
next_loop_linted = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !next_loop_linted {
|
||||||
|
for_loops_over_fallibles::check_arg_type(cx, pat, arg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns `true` if the type of expr is one that provides `IntoIterator` impls
|
||||||
|
/// for `&T` and `&mut T`, such as `Vec`.
|
||||||
|
#[rustfmt::skip]
|
||||||
|
fn is_ref_iterable_type(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
|
||||||
|
// no walk_ptrs_ty: calling iter() on a reference can make sense because it
|
||||||
|
// will allow further borrows afterwards
|
||||||
|
let ty = cx.typeck_results().expr_ty(e);
|
||||||
|
is_iterable_array(ty, cx) ||
|
||||||
|
is_type_diagnostic_item(cx, ty, sym::vec_type) ||
|
||||||
|
match_type(cx, ty, &paths::LINKED_LIST) ||
|
||||||
|
is_type_diagnostic_item(cx, ty, sym!(hashmap_type)) ||
|
||||||
|
is_type_diagnostic_item(cx, ty, sym!(hashset_type)) ||
|
||||||
|
is_type_diagnostic_item(cx, ty, sym!(vecdeque_type)) ||
|
||||||
|
match_type(cx, ty, &paths::BINARY_HEAP) ||
|
||||||
|
match_type(cx, ty, &paths::BTREEMAP) ||
|
||||||
|
match_type(cx, ty, &paths::BTREESET)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_iterable_array<'tcx>(ty: Ty<'tcx>, cx: &LateContext<'tcx>) -> bool {
|
||||||
|
// IntoIterator is currently only implemented for array sizes <= 32 in rustc
|
||||||
|
match ty.kind() {
|
||||||
|
ty::Array(_, n) => n
|
||||||
|
.try_eval_usize(cx.tcx, cx.param_env)
|
||||||
|
.map_or(false, |val| (0..=32).contains(&val)),
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user