Skip to content

Commit 316da7e

Browse files
committed
Auto merge of #4258 - mikerite:fix-breakage-20190706, r=Manishearth
Fix breakage due to rust-lang/rust#61988 changelog: none
2 parents 5d7f6a1 + c72be0f commit 316da7e

File tree

8 files changed

+31
-60
lines changed

8 files changed

+31
-60
lines changed

clippy_lints/src/loops.rs

+11-24
Original file line numberDiff line numberDiff line change
@@ -481,16 +481,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Loops {
481481
}
482482

483483
// check for never_loop
484-
match expr.node {
485-
ExprKind::While(_, ref block, _) | ExprKind::Loop(ref block, _, _) => {
486-
match never_loop_block(block, expr.hir_id) {
487-
NeverLoopResult::AlwaysBreak => {
488-
span_lint(cx, NEVER_LOOP, expr.span, "this loop never actually loops")
489-
},
490-
NeverLoopResult::MayContinueMainLoop | NeverLoopResult::Otherwise => (),
491-
}
492-
},
493-
_ => (),
484+
if let ExprKind::Loop(ref block, _, _) = expr.node {
485+
match never_loop_block(block, expr.hir_id) {
486+
NeverLoopResult::AlwaysBreak => span_lint(cx, NEVER_LOOP, expr.span, "this loop never actually loops"),
487+
NeverLoopResult::MayContinueMainLoop | NeverLoopResult::Otherwise => (),
488+
}
494489
}
495490

496491
// check for `loop { if let {} else break }` that could be `while let`
@@ -590,9 +585,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Loops {
590585
}
591586
}
592587

593-
// check for while loops which conditions never change
594-
if let ExprKind::While(ref cond, _, _) = expr.node {
595-
check_infinite_loop(cx, cond, expr);
588+
if let Some((cond, body)) = higher::while_loop(&expr) {
589+
check_infinite_loop(cx, cond, body);
596590
}
597591

598592
check_needless_collect(expr, cx);
@@ -701,12 +695,6 @@ fn never_loop_expr(expr: &Expr, main_loop_id: HirId) -> NeverLoopResult {
701695
// Break can come from the inner loop so remove them.
702696
absorb_break(&never_loop_block(b, main_loop_id))
703697
},
704-
ExprKind::While(ref e, ref b, _) => {
705-
let e = never_loop_expr(e, main_loop_id);
706-
let result = never_loop_block(b, main_loop_id);
707-
// Break can come from the inner loop so remove them.
708-
combine_seq(e, absorb_break(&result))
709-
},
710698
ExprKind::Match(ref e, ref arms, _) => {
711699
let e = never_loop_expr(e, main_loop_id);
712700
if arms.is_empty() {
@@ -2202,7 +2190,7 @@ fn var_def_id(cx: &LateContext<'_, '_>, expr: &Expr) -> Option<HirId> {
22022190

22032191
fn is_loop(expr: &Expr) -> bool {
22042192
match expr.node {
2205-
ExprKind::Loop(..) | ExprKind::While(..) => true,
2193+
ExprKind::Loop(..) => true,
22062194
_ => false,
22072195
}
22082196
}
@@ -2239,11 +2227,10 @@ fn is_loop_nested(cx: &LateContext<'_, '_>, loop_expr: &Expr, iter_expr: &Expr)
22392227
return false;
22402228
}
22412229
match cx.tcx.hir().find(parent) {
2242-
Some(Node::Expr(expr)) => match expr.node {
2243-
ExprKind::Loop(..) | ExprKind::While(..) => {
2230+
Some(Node::Expr(expr)) => {
2231+
if let ExprKind::Loop(..) = expr.node {
22442232
return true;
2245-
},
2246-
_ => (),
2233+
};
22472234
},
22482235
Some(Node::Block(block)) => {
22492236
let mut block_visitor = LoopNestVisitor {

clippy_lints/src/shadow.rs

-4
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,6 @@ fn check_expr<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, bindings:
319319
check_expr(cx, e, bindings)
320320
}
321321
},
322-
ExprKind::While(ref cond, ref block, _) => {
323-
check_expr(cx, cond, bindings);
324-
check_block(cx, block, bindings);
325-
},
326322
ExprKind::Match(ref init, ref arms, _) => {
327323
check_expr(cx, init, bindings);
328324
let len = bindings.len();

clippy_lints/src/unused_label.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UnusedLabelVisitor<'a, 'tcx> {
6868
self.labels.remove(&label.ident.as_str());
6969
}
7070
},
71-
hir::ExprKind::Loop(_, Some(label), _) | hir::ExprKind::While(_, _, Some(label)) => {
71+
hir::ExprKind::Loop(_, Some(label), _) => {
7272
self.labels.insert(label.ident.as_str(), expr.span);
7373
},
7474
_ => (),

clippy_lints/src/utils/author.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -322,19 +322,6 @@ impl<'tcx> Visitor<'tcx> for PrintVisitor {
322322
self.current = cast_pat;
323323
self.visit_expr(expr);
324324
},
325-
ExprKind::While(ref cond, ref body, _) => {
326-
let cond_pat = self.next("cond");
327-
let body_pat = self.next("body");
328-
let label_pat = self.next("label");
329-
println!(
330-
"While(ref {}, ref {}, ref {}) = {};",
331-
cond_pat, body_pat, label_pat, current
332-
);
333-
self.current = cond_pat;
334-
self.visit_expr(cond);
335-
self.current = body_pat;
336-
self.visit_block(body);
337-
},
338325
ExprKind::Loop(ref body, _, desugaring) => {
339326
let body_pat = self.next("body");
340327
let des = loop_desugaring_name(desugaring);
@@ -696,6 +683,7 @@ fn desugaring_name(des: hir::MatchSource) -> String {
696683
match des {
697684
hir::MatchSource::ForLoopDesugar => "MatchSource::ForLoopDesugar".to_string(),
698685
hir::MatchSource::TryDesugar => "MatchSource::TryDesugar".to_string(),
686+
hir::MatchSource::WhileDesugar => "MatchSource::WhileDesugar".to_string(),
699687
hir::MatchSource::WhileLetDesugar => "MatchSource::WhileLetDesugar".to_string(),
700688
hir::MatchSource::Normal => "MatchSource::Normal".to_string(),
701689
hir::MatchSource::IfLetDesugar { contains_else_clause } => format!(
@@ -714,6 +702,7 @@ fn loop_desugaring_name(des: hir::LoopSource) -> &'static str {
714702
match des {
715703
hir::LoopSource::ForLoop => "LoopSource::ForLoop",
716704
hir::LoopSource::Loop => "LoopSource::Loop",
705+
hir::LoopSource::While => "LoopSource::While",
717706
hir::LoopSource::WhileLet => "LoopSource::WhileLet",
718707
}
719708
}

clippy_lints/src/utils/higher.rs

+17
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,23 @@ pub fn for_loop(expr: &hir::Expr) -> Option<(&hir::Pat, &hir::Expr, &hir::Expr)>
199199
None
200200
}
201201

202+
/// Recover the essential nodes of a desugared while loop:
203+
/// `while cond { body }` becomes `(cond, body)`.
204+
pub fn while_loop(expr: &hir::Expr) -> Option<(&hir::Expr, &hir::Expr)> {
205+
if_chain! {
206+
if let hir::ExprKind::Loop(block, _, hir::LoopSource::While) = &expr.node;
207+
if let hir::Block { expr: Some(expr), .. } = &**block;
208+
if let hir::ExprKind::Match(cond, arms, hir::MatchSource::WhileDesugar) = &expr.node;
209+
if let hir::ExprKind::DropTemps(cond) = &cond.node;
210+
if let [arm, ..] = &arms[..];
211+
if let hir::Arm { body, .. } = arm;
212+
then {
213+
return Some((cond, body));
214+
}
215+
}
216+
None
217+
}
218+
202219
/// Recover the essential nodes of a desugared if block
203220
/// `if cond { then } else { els }` becomes `(cond, then, Some(els))`
204221
pub fn if_block(expr: &hir::Expr) -> Option<(&hir::Expr, &hir::Expr, Option<&hir::Expr>)> {

clippy_lints/src/utils/hir_utils.rs

-12
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,6 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
148148
(&ExprKind::Tup(ref l_tup), &ExprKind::Tup(ref r_tup)) => self.eq_exprs(l_tup, r_tup),
149149
(&ExprKind::Unary(l_op, ref le), &ExprKind::Unary(r_op, ref re)) => l_op == r_op && self.eq_expr(le, re),
150150
(&ExprKind::Array(ref l), &ExprKind::Array(ref r)) => self.eq_exprs(l, r),
151-
(&ExprKind::While(ref lc, ref lb, ref ll), &ExprKind::While(ref rc, ref rb, ref rl)) => {
152-
self.eq_expr(lc, rc)
153-
&& self.eq_block(lb, rb)
154-
&& both(ll, rl, |l, r| l.ident.as_str() == r.ident.as_str())
155-
},
156151
(&ExprKind::DropTemps(ref le), &ExprKind::DropTemps(ref re)) => self.eq_expr(le, re),
157152
_ => false,
158153
}
@@ -524,13 +519,6 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
524519
lop.hash(&mut self.s);
525520
self.hash_expr(le);
526521
},
527-
ExprKind::While(ref cond, ref b, l) => {
528-
self.hash_expr(cond);
529-
self.hash_block(b);
530-
if let Some(l) = l {
531-
self.hash_name(l.ident.name);
532-
}
533-
},
534522
}
535523
}
536524

clippy_lints/src/utils/inspector.rs

-5
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,6 @@ fn print_expr(cx: &LateContext<'_, '_>, expr: &hir::Expr, indent: usize) {
209209
print_expr(cx, e, indent + 1);
210210
println!("{}target type: {:?}", ind, target);
211211
},
212-
hir::ExprKind::While(ref cond, _, _) => {
213-
println!("{}While", ind);
214-
println!("{}condition:", ind);
215-
print_expr(cx, cond, indent + 1);
216-
},
217212
hir::ExprKind::Loop(..) => {
218213
println!("{}Loop", ind);
219214
},

clippy_lints/src/utils/sugg.rs

-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ impl<'a> Sugg<'a> {
113113
| hir::ExprKind::Ret(..)
114114
| hir::ExprKind::Struct(..)
115115
| hir::ExprKind::Tup(..)
116-
| hir::ExprKind::While(..)
117116
| hir::ExprKind::DropTemps(_)
118117
| hir::ExprKind::Err => Sugg::NonParen(snippet),
119118
hir::ExprKind::Assign(..) => Sugg::BinOp(AssocOp::Assign, snippet),

0 commit comments

Comments
 (0)