Skip to content

Commit 81f37a8

Browse files
committed
Auto merge of rust-lang#8025 - flip1995:rustup, r=flip1995
Rustup Re-sync, because I didn't get to syncing things back to rustc. r? `@ghost` changelog: none
2 parents d6c707d + c46c8c5 commit 81f37a8

File tree

13 files changed

+123
-284
lines changed

13 files changed

+123
-284
lines changed

clippy_lints/src/loops/explicit_counter_loop.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use super::{
2-
get_span_of_entire_for_loop, make_iterator_snippet, IncrementVisitor, InitializeVisitor, EXPLICIT_COUNTER_LOOP,
3-
};
1+
use super::{make_iterator_snippet, IncrementVisitor, InitializeVisitor, EXPLICIT_COUNTER_LOOP};
42
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
53
use clippy_utils::source::snippet_with_applicability;
64
use clippy_utils::{get_enclosing_block, is_integer_const};
@@ -38,15 +36,13 @@ pub(super) fn check<'tcx>(
3836
then {
3937
let mut applicability = Applicability::MachineApplicable;
4038

41-
let for_span = get_span_of_entire_for_loop(expr);
42-
4339
let int_name = match ty.map(ty::TyS::kind) {
4440
// usize or inferred
4541
Some(ty::Uint(UintTy::Usize)) | None => {
4642
span_lint_and_sugg(
4743
cx,
4844
EXPLICIT_COUNTER_LOOP,
49-
for_span.with_hi(arg.span.hi()),
45+
expr.span.with_hi(arg.span.hi()),
5046
&format!("the variable `{}` is used as a loop counter", name),
5147
"consider using",
5248
format!(
@@ -67,11 +63,11 @@ pub(super) fn check<'tcx>(
6763
span_lint_and_then(
6864
cx,
6965
EXPLICIT_COUNTER_LOOP,
70-
for_span.with_hi(arg.span.hi()),
66+
expr.span.with_hi(arg.span.hi()),
7167
&format!("the variable `{}` is used as a loop counter", name),
7268
|diag| {
7369
diag.span_suggestion(
74-
for_span.with_hi(arg.span.hi()),
70+
expr.span.with_hi(arg.span.hi()),
7571
"consider using",
7672
format!(
7773
"for ({}, {}) in (0_{}..).zip({})",

clippy_lints/src/loops/manual_memcpy.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{get_span_of_entire_for_loop, IncrementVisitor, InitializeVisitor, MANUAL_MEMCPY};
1+
use super::{IncrementVisitor, InitializeVisitor, MANUAL_MEMCPY};
22
use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::source::snippet;
44
use clippy_utils::sugg::Sugg;
@@ -86,7 +86,7 @@ pub(super) fn check<'tcx>(
8686
span_lint_and_sugg(
8787
cx,
8888
MANUAL_MEMCPY,
89-
get_span_of_entire_for_loop(expr),
89+
expr.span,
9090
"it looks like you're manually copying between slices",
9191
"try replacing the loop by",
9292
big_sugg,

clippy_lints/src/loops/mod.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use rustc_hir::{Expr, ExprKind, LoopSource, Pat};
2323
use rustc_lint::{LateContext, LateLintPass};
2424
use rustc_session::{declare_lint_pass, declare_tool_lint};
2525
use rustc_span::source_map::Span;
26-
use utils::{get_span_of_entire_for_loop, make_iterator_snippet, IncrementVisitor, InitializeVisitor};
26+
use utils::{make_iterator_snippet, IncrementVisitor, InitializeVisitor};
2727

2828
declare_clippy_lint! {
2929
/// ### What it does
@@ -584,14 +584,25 @@ declare_lint_pass!(Loops => [
584584
impl<'tcx> LateLintPass<'tcx> for Loops {
585585
#[allow(clippy::too_many_lines)]
586586
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
587-
if let Some(higher::ForLoop { pat, arg, body, span }) = higher::ForLoop::hir(expr) {
587+
let for_loop = higher::ForLoop::hir(expr);
588+
if let Some(higher::ForLoop {
589+
pat,
590+
arg,
591+
body,
592+
loop_id,
593+
span,
594+
}) = for_loop
595+
{
588596
// we don't want to check expanded macros
589597
// this check is not at the top of the function
590598
// since higher::for_loop expressions are marked as expansions
591599
if body.span.from_expansion() {
592600
return;
593601
}
594602
check_for_loop(cx, pat, arg, body, expr, span);
603+
if let ExprKind::Block(block, _) = body.kind {
604+
never_loop::check(cx, block, loop_id, span, for_loop.as_ref());
605+
}
595606
}
596607

597608
// we don't want to check expanded macros
@@ -600,7 +611,9 @@ impl<'tcx> LateLintPass<'tcx> for Loops {
600611
}
601612

602613
// check for never_loop
603-
never_loop::check(cx, expr);
614+
if let ExprKind::Loop(block, ..) = expr.kind {
615+
never_loop::check(cx, block, expr.hir_id, expr.span, None);
616+
}
604617

605618
// check for `loop { if let {} else break }` that could be `while let`
606619
// (also matches an explicit "match" instead of "if let")

clippy_lints/src/loops/never_loop.rs

+32-26
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,41 @@ use clippy_utils::diagnostics::span_lint_and_then;
44
use clippy_utils::higher::ForLoop;
55
use clippy_utils::source::snippet;
66
use rustc_errors::Applicability;
7-
use rustc_hir::{Block, Expr, ExprKind, HirId, InlineAsmOperand, LoopSource, Node, Pat, Stmt, StmtKind};
7+
use rustc_hir::{Block, Expr, ExprKind, HirId, InlineAsmOperand, Pat, Stmt, StmtKind};
88
use rustc_lint::LateContext;
9+
use rustc_span::Span;
910
use std::iter::{once, Iterator};
1011

11-
pub(super) fn check(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
12-
if let ExprKind::Loop(block, _, source, _) = expr.kind {
13-
match never_loop_block(block, expr.hir_id) {
14-
NeverLoopResult::AlwaysBreak => {
15-
span_lint_and_then(cx, NEVER_LOOP, expr.span, "this loop never actually loops", |diag| {
16-
if_chain! {
17-
if source == LoopSource::ForLoop;
18-
if let Some((_, Node::Expr(parent_match))) = cx.tcx.hir().parent_iter(expr.hir_id).nth(1);
19-
if let Some(ForLoop { arg: iterator, pat, span: for_span, .. }) = ForLoop::hir(parent_match);
20-
then {
21-
// Suggests using an `if let` instead. This is `Unspecified` because the
22-
// loop may (probably) contain `break` statements which would be invalid
23-
// in an `if let`.
24-
diag.span_suggestion_verbose(
25-
for_span.with_hi(iterator.span.hi()),
26-
"if you need the first element of the iterator, try writing",
27-
for_to_if_let_sugg(cx, iterator, pat),
28-
Applicability::Unspecified,
29-
);
30-
}
31-
};
32-
});
33-
},
34-
NeverLoopResult::MayContinueMainLoop | NeverLoopResult::Otherwise => (),
35-
}
12+
pub(super) fn check(
13+
cx: &LateContext<'tcx>,
14+
block: &'tcx Block<'_>,
15+
loop_id: HirId,
16+
span: Span,
17+
for_loop: Option<&ForLoop<'_>>,
18+
) {
19+
match never_loop_block(block, loop_id) {
20+
NeverLoopResult::AlwaysBreak => {
21+
span_lint_and_then(cx, NEVER_LOOP, span, "this loop never actually loops", |diag| {
22+
if let Some(ForLoop {
23+
arg: iterator,
24+
pat,
25+
span: for_span,
26+
..
27+
}) = for_loop
28+
{
29+
// Suggests using an `if let` instead. This is `Unspecified` because the
30+
// loop may (probably) contain `break` statements which would be invalid
31+
// in an `if let`.
32+
diag.span_suggestion_verbose(
33+
for_span.with_hi(iterator.span.hi()),
34+
"if you need the first element of the iterator, try writing",
35+
for_to_if_let_sugg(cx, iterator, pat),
36+
Applicability::Unspecified,
37+
);
38+
}
39+
});
40+
},
41+
NeverLoopResult::MayContinueMainLoop | NeverLoopResult::Otherwise => (),
3642
}
3743
}
3844

clippy_lints/src/loops/single_element_loop.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{get_span_of_entire_for_loop, SINGLE_ELEMENT_LOOP};
1+
use super::SINGLE_ELEMENT_LOOP;
22
use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::single_segment_path;
44
use clippy_utils::source::{indent_of, snippet};
@@ -30,7 +30,6 @@ pub(super) fn check<'tcx>(
3030
if !block.stmts.is_empty();
3131

3232
then {
33-
let for_span = get_span_of_entire_for_loop(expr);
3433
let mut block_str = snippet(cx, block.span, "..").into_owned();
3534
block_str.remove(0);
3635
block_str.pop();
@@ -39,7 +38,7 @@ pub(super) fn check<'tcx>(
3938
span_lint_and_sugg(
4039
cx,
4140
SINGLE_ELEMENT_LOOP,
42-
for_span,
41+
expr.span,
4342
"for loop over a single element",
4443
"try",
4544
format!("{{\n{}let {} = &{};{}}}", " ".repeat(indent_of(cx, block.stmts[0].span).unwrap_or(0)), target.name, list_item_name, block_str),

clippy_lints/src/loops/utils.rs

-12
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind, HirId, HirIdMap, Local, M
88
use rustc_lint::LateContext;
99
use rustc_middle::hir::map::Map;
1010
use rustc_middle::ty::Ty;
11-
use rustc_span::source_map::Span;
1211
use rustc_span::source_map::Spanned;
1312
use rustc_span::symbol::{sym, Symbol};
1413
use rustc_typeck::hir_ty_to_ty;
@@ -330,17 +329,6 @@ impl<'tcx> Visitor<'tcx> for LoopNestVisitor {
330329
}
331330
}
332331

333-
// this function assumes the given expression is a `for` loop.
334-
pub(super) fn get_span_of_entire_for_loop(expr: &Expr<'_>) -> Span {
335-
// for some reason this is the only way to get the `Span`
336-
// of the entire `for` loop
337-
if let ExprKind::Match(_, arms, _) = &expr.kind {
338-
arms[0].body.span
339-
} else {
340-
unreachable!()
341-
}
342-
}
343-
344332
/// If `arg` was the argument to a `for` loop, return the "cleanest" way of writing the
345333
/// actual `Iterator` that the loop uses.
346334
pub(super) fn make_iterator_snippet(cx: &LateContext<'_>, arg: &Expr<'_>, applic_ref: &mut Applicability) -> String {

clippy_lints/src/misc.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use rustc_span::symbol::sym;
2020
use clippy_utils::consts::{constant, Constant};
2121
use clippy_utils::sugg::Sugg;
2222
use clippy_utils::{
23-
expr_path_res, get_item_name, get_parent_expr, higher, in_constant, is_diag_trait_item, is_integer_const,
24-
iter_input_pats, last_path_segment, match_any_def_paths, paths, unsext, SpanlessEq,
23+
expr_path_res, get_item_name, get_parent_expr, in_constant, is_diag_trait_item, is_integer_const, iter_input_pats,
24+
last_path_segment, match_any_def_paths, paths, unsext, SpanlessEq,
2525
};
2626

2727
declare_clippy_lint! {
@@ -321,7 +321,6 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
321321
if let StmtKind::Local(local) = stmt.kind;
322322
if let PatKind::Binding(an, .., name, None) = local.pat.kind;
323323
if let Some(init) = local.init;
324-
if !higher::is_from_for_desugar(local);
325324
if an == BindingAnnotation::Ref || an == BindingAnnotation::RefMut;
326325
then {
327326
// use the macro callsite when the init span (but not the whole local span)

0 commit comments

Comments
 (0)