Unify names of lint entry functions in loops to 'check'
This commit is contained in:
parent
845a3a061c
commit
eaf63d6df7
@ -4,7 +4,7 @@ use crate::utils::{is_in_panic_handler, is_no_std_crate, span_lint_and_help};
|
||||
use rustc_hir::{Block, Expr};
|
||||
use rustc_lint::LateContext;
|
||||
|
||||
pub(super) fn check_empty_loop(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, loop_block: &'tcx Block<'_>) {
|
||||
pub(super) fn check(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, loop_block: &'tcx Block<'_>) {
|
||||
if loop_block.stmts.is_empty() && loop_block.expr.is_none() && !is_in_panic_handler(cx, expr) {
|
||||
let msg = "empty `loop {}` wastes CPU cycles";
|
||||
let help = if is_no_std_crate(cx.tcx.hir().krate()) {
|
||||
|
@ -11,7 +11,7 @@ use rustc_lint::LateContext;
|
||||
// To trigger the EXPLICIT_COUNTER_LOOP lint, a variable must be
|
||||
// incremented exactly once in the loop body, and initialized to zero
|
||||
// at the start of the loop.
|
||||
pub(super) fn check_for_loop_explicit_counter<'tcx>(
|
||||
pub(super) fn check<'tcx>(
|
||||
cx: &LateContext<'tcx>,
|
||||
pat: &'tcx Pat<'_>,
|
||||
arg: &'tcx Expr<'_>,
|
||||
|
@ -4,7 +4,7 @@ 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<'_>) {
|
||||
pub(super) fn check(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(
|
||||
|
@ -4,7 +4,7 @@ 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) {
|
||||
pub(super) fn check(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 { "" };
|
||||
|
@ -6,7 +6,7 @@ use rustc_lint::LateContext;
|
||||
use rustc_middle::ty;
|
||||
|
||||
/// Checks for the `FOR_KV_MAP` lint.
|
||||
pub(super) fn check_for_loop_over_map_kv<'tcx>(
|
||||
pub(super) fn check<'tcx>(
|
||||
cx: &LateContext<'tcx>,
|
||||
pat: &'tcx Pat<'_>,
|
||||
arg: &'tcx Expr<'_>,
|
||||
|
@ -5,7 +5,7 @@ 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<'_>) {
|
||||
pub(super) fn check(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(
|
||||
|
@ -3,7 +3,7 @@ use crate::utils::span_lint;
|
||||
use rustc_hir::Expr;
|
||||
use rustc_lint::LateContext;
|
||||
|
||||
pub(super) fn lint(cx: &LateContext<'_>, expr: &Expr<'_>) {
|
||||
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
|
||||
span_lint(
|
||||
cx,
|
||||
ITER_NEXT_LOOP,
|
||||
|
@ -9,7 +9,7 @@ use rustc_span::source_map::Span;
|
||||
|
||||
/// Check for unnecessary `if let` usage in a for loop where only the `Some` or `Ok` variant of the
|
||||
/// iterator element is used.
|
||||
pub(super) fn check_manual_flatten<'tcx>(
|
||||
pub(super) fn check<'tcx>(
|
||||
cx: &LateContext<'tcx>,
|
||||
pat: &'tcx Pat<'_>,
|
||||
arg: &'tcx Expr<'_>,
|
||||
|
@ -15,7 +15,7 @@ use std::iter::Iterator;
|
||||
|
||||
/// Checks for for loops that sequentially copy items from one slice-like
|
||||
/// object to another.
|
||||
pub(super) fn detect_manual_memcpy<'tcx>(
|
||||
pub(super) fn check<'tcx>(
|
||||
cx: &LateContext<'tcx>,
|
||||
pat: &'tcx Pat<'_>,
|
||||
arg: &'tcx Expr<'_>,
|
||||
|
@ -559,24 +559,24 @@ impl<'tcx> LateLintPass<'tcx> for Loops {
|
||||
}
|
||||
|
||||
// check for never_loop
|
||||
never_loop::check_never_loop(cx, expr);
|
||||
never_loop::check(cx, expr);
|
||||
|
||||
// check for `loop { if let {} else break }` that could be `while let`
|
||||
// (also matches an explicit "match" instead of "if let")
|
||||
// (even if the "match" or "if let" is used for declaration)
|
||||
if let ExprKind::Loop(ref block, _, LoopSource::Loop, _) = expr.kind {
|
||||
// also check for empty `loop {}` statements, skipping those in #[panic_handler]
|
||||
empty_loop::check_empty_loop(cx, expr, block);
|
||||
while_let_loop::check_while_let_loop(cx, expr, block);
|
||||
empty_loop::check(cx, expr, block);
|
||||
while_let_loop::check(cx, expr, block);
|
||||
}
|
||||
|
||||
while_let_on_iterator::check_while_let_on_iterator(cx, expr);
|
||||
while_let_on_iterator::check(cx, expr);
|
||||
|
||||
if let Some((cond, body)) = higher::while_loop(&expr) {
|
||||
while_immutable_condition::check_infinite_loop(cx, cond, body);
|
||||
while_immutable_condition::check(cx, cond, body);
|
||||
}
|
||||
|
||||
needless_collect::check_needless_collect(expr, cx);
|
||||
needless_collect::check(expr, cx);
|
||||
}
|
||||
}
|
||||
|
||||
@ -588,17 +588,17 @@ fn check_for_loop<'tcx>(
|
||||
expr: &'tcx Expr<'_>,
|
||||
span: Span,
|
||||
) {
|
||||
let is_manual_memcpy_triggered = manual_memcpy::detect_manual_memcpy(cx, pat, arg, body, expr);
|
||||
let is_manual_memcpy_triggered = manual_memcpy::check(cx, pat, arg, body, expr);
|
||||
if !is_manual_memcpy_triggered {
|
||||
needless_range_loop::check_for_loop_range(cx, pat, arg, body, expr);
|
||||
explicit_counter_loop::check_for_loop_explicit_counter(cx, pat, arg, body, expr);
|
||||
needless_range_loop::check(cx, pat, arg, body, expr);
|
||||
explicit_counter_loop::check(cx, pat, arg, body, expr);
|
||||
}
|
||||
check_for_loop_arg(cx, pat, arg, expr);
|
||||
for_kv_map::check_for_loop_over_map_kv(cx, pat, arg, body, expr);
|
||||
mut_range_bound::check_for_mut_range_bound(cx, arg, body);
|
||||
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);
|
||||
manual_flatten::check_manual_flatten(cx, pat, arg, body, span);
|
||||
for_kv_map::check(cx, pat, arg, body, expr);
|
||||
mut_range_bound::check(cx, arg, body);
|
||||
single_element_loop::check(cx, pat, arg, body, expr);
|
||||
same_item_push::check(cx, pat, arg, body, expr);
|
||||
manual_flatten::check(cx, pat, arg, body, span);
|
||||
}
|
||||
|
||||
fn check_for_loop_arg(cx: &LateContext<'_>, pat: &Pat<'_>, arg: &Expr<'_>, expr: &Expr<'_>) {
|
||||
@ -610,13 +610,13 @@ fn check_for_loop_arg(cx: &LateContext<'_>, pat: &Pat<'_>, arg: &Expr<'_>, expr:
|
||||
// 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);
|
||||
explicit_iter_loop::check(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);
|
||||
explicit_into_iter_loop::check(cx, args, arg);
|
||||
} else {
|
||||
let ref_receiver_ty = cx.tcx.mk_ref(
|
||||
cx.tcx.lifetimes.re_erased,
|
||||
@ -626,17 +626,17 @@ fn check_for_loop_arg(cx: &LateContext<'_>, pat: &Pat<'_>, arg: &Expr<'_>, expr:
|
||||
},
|
||||
);
|
||||
if TyS::same_type(receiver_ty_adjusted, ref_receiver_ty) {
|
||||
explicit_iter_loop::lint_iter_method(cx, args, arg, method_name)
|
||||
explicit_iter_loop::check(cx, args, arg, method_name)
|
||||
}
|
||||
}
|
||||
} else if method_name == "next" && match_trait_method(cx, arg, &paths::ITERATOR) {
|
||||
iter_next_loop::lint(cx, expr);
|
||||
iter_next_loop::check(cx, expr);
|
||||
next_loop_linted = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if !next_loop_linted {
|
||||
for_loops_over_fallibles::check_arg_type(cx, pat, arg);
|
||||
for_loops_over_fallibles::check(cx, pat, arg);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,7 +8,7 @@ use rustc_middle::ty;
|
||||
use rustc_span::source_map::Span;
|
||||
use rustc_typeck::expr_use_visitor::{ConsumeMode, Delegate, ExprUseVisitor, PlaceBase, PlaceWithHirId};
|
||||
|
||||
pub(super) fn check_for_mut_range_bound(cx: &LateContext<'_>, arg: &Expr<'_>, body: &Expr<'_>) {
|
||||
pub(super) fn check(cx: &LateContext<'_>, arg: &Expr<'_>, body: &Expr<'_>) {
|
||||
if let Some(higher::Range {
|
||||
start: Some(start),
|
||||
end: Some(end),
|
||||
|
@ -15,7 +15,7 @@ use rustc_span::symbol::{sym, Ident};
|
||||
|
||||
const NEEDLESS_COLLECT_MSG: &str = "avoid using `collect()` when not needed";
|
||||
|
||||
pub(super) fn check_needless_collect<'tcx>(expr: &'tcx Expr<'_>, cx: &LateContext<'tcx>) {
|
||||
pub(super) fn check<'tcx>(expr: &'tcx Expr<'_>, cx: &LateContext<'tcx>) {
|
||||
check_needless_collect_direct_usage(expr, cx);
|
||||
check_needless_collect_indirect_usage(expr, cx);
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ use std::mem;
|
||||
/// Checks for looping over a range and then indexing a sequence with it.
|
||||
/// The iteratee must be a range literal.
|
||||
#[allow(clippy::too_many_lines)]
|
||||
pub(super) fn check_for_loop_range<'tcx>(
|
||||
pub(super) fn check<'tcx>(
|
||||
cx: &LateContext<'tcx>,
|
||||
pat: &'tcx Pat<'_>,
|
||||
arg: &'tcx Expr<'_>,
|
||||
|
@ -4,7 +4,7 @@ use rustc_hir::{Block, Expr, ExprKind, HirId, InlineAsmOperand, Stmt, StmtKind};
|
||||
use rustc_lint::LateContext;
|
||||
use std::iter::{once, Iterator};
|
||||
|
||||
pub(super) fn check_never_loop(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
pub(super) fn check(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
if let ExprKind::Loop(ref block, _, _, _) = expr.kind {
|
||||
match never_loop_block(block, expr.hir_id) {
|
||||
NeverLoopResult::AlwaysBreak => span_lint(cx, NEVER_LOOP, expr.span, "this loop never actually loops"),
|
||||
|
@ -10,7 +10,7 @@ use rustc_span::symbol::sym;
|
||||
use std::iter::Iterator;
|
||||
|
||||
/// Detects for loop pushing the same item into a Vec
|
||||
pub(super) fn detect_same_item_push<'tcx>(
|
||||
pub(super) fn check<'tcx>(
|
||||
cx: &LateContext<'tcx>,
|
||||
pat: &'tcx Pat<'_>,
|
||||
_: &'tcx Expr<'_>,
|
||||
|
@ -5,7 +5,7 @@ use rustc_errors::Applicability;
|
||||
use rustc_hir::{BorrowKind, Expr, ExprKind, Pat, PatKind};
|
||||
use rustc_lint::LateContext;
|
||||
|
||||
pub(super) fn check_for_single_element_loop<'tcx>(
|
||||
pub(super) fn check<'tcx>(
|
||||
cx: &LateContext<'tcx>,
|
||||
pat: &'tcx Pat<'_>,
|
||||
arg: &'tcx Expr<'_>,
|
||||
|
@ -11,7 +11,7 @@ use rustc_lint::LateContext;
|
||||
use rustc_middle::hir::map::Map;
|
||||
use std::iter::Iterator;
|
||||
|
||||
pub(super) fn check_infinite_loop<'tcx>(cx: &LateContext<'tcx>, cond: &'tcx Expr<'_>, expr: &'tcx Expr<'_>) {
|
||||
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, cond: &'tcx Expr<'_>, expr: &'tcx Expr<'_>) {
|
||||
if constant(cx, cx.typeck_results(), cond).is_some() {
|
||||
// A pure constant condition (e.g., `while false`) is not linted.
|
||||
return;
|
||||
|
@ -5,7 +5,7 @@ use rustc_hir::{Block, Expr, ExprKind, MatchSource, StmtKind};
|
||||
use rustc_lint::{LateContext, LintContext};
|
||||
use rustc_middle::lint::in_external_macro;
|
||||
|
||||
pub(super) fn check_while_let_loop(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, loop_block: &'tcx Block<'_>) {
|
||||
pub(super) fn check(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, loop_block: &'tcx Block<'_>) {
|
||||
// extract the expression from the first statement (if any) in a block
|
||||
let inner_stmt_expr = extract_expr_from_first_stmt(loop_block);
|
||||
// or extract the first expression (if any) from the block
|
||||
|
@ -14,7 +14,7 @@ use rustc_middle::hir::map::Map;
|
||||
|
||||
use rustc_span::symbol::sym;
|
||||
|
||||
pub(super) fn check_while_let_on_iterator(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
pub(super) fn check(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
if let ExprKind::Match(ref match_expr, ref arms, MatchSource::WhileLetDesugar) = expr.kind {
|
||||
let pat = &arms[0].pat.kind;
|
||||
if let (
|
||||
|
Loading…
x
Reference in New Issue
Block a user