Skip to content

Commit

Permalink
Auto merge of #4080 - rust-lang:rustup, r=oli-obk
Browse files Browse the repository at this point in the history
Rustup to rustc 1.36.0-nightly (acc7e65 2019-05-10)

Fixes breakages from rust-lang/rust#59288

Not finished yet, help appreciated.

Todo:

 - [x] Needs to build
 - [x] Util should handle DropTemps, #4080 (comment)
 - [x] Author lint should spit out higher::if_block checks
 - [x] Unsure what to do in consts.rs
 - [x] Needs to pass tests
  • Loading branch information
bors committed May 11, 2019
2 parents c74aac9 + 19cfb84 commit 3710ec5
Show file tree
Hide file tree
Showing 24 changed files with 136 additions and 104 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/block_in_if_condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const COMPLEX_BLOCK_MESSAGE: &str = "in an 'if' condition, avoid complex blocks

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
if let ExprKind::If(check, then, _) = &expr.node {
if let Some((check, then, _)) = higher::if_block(&expr) {
if let ExprKind::Block(block, _) = &check.node {
if block.rules == DefaultBlock {
if block.stmts.is_empty() {
Expand Down
11 changes: 6 additions & 5 deletions clippy_lints/src/consts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(clippy::float_cmp)]

use crate::utils::{clip, sext, unsext};
use crate::utils::{clip, higher, sext, unsext};
use if_chain::if_chain;
use rustc::hir::def::{DefKind, Res};
use rustc::hir::*;
Expand All @@ -15,7 +15,6 @@ use std::convert::TryFrom;
use std::convert::TryInto;
use std::hash::{Hash, Hasher};
use syntax::ast::{FloatTy, LitKind};
use syntax::ptr::P;
use syntax_pos::symbol::{LocalInternedString, Symbol};

/// A `LitKind`-like enum to fold constant `Expr`s into.
Expand Down Expand Up @@ -222,10 +221,12 @@ pub struct ConstEvalLateContext<'a, 'tcx: 'a> {
impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
/// Simple constant folding: Insert an expression, get a constant or none.
pub fn expr(&mut self, e: &Expr) -> Option<Constant> {
if let Some((ref cond, ref then, otherwise)) = higher::if_block(&e) {
return self.ifthenelse(cond, then, otherwise);
}
match e.node {
ExprKind::Path(ref qpath) => self.fetch_path(qpath, e.hir_id),
ExprKind::Block(ref block, _) => self.block(block),
ExprKind::If(ref cond, ref then, ref otherwise) => self.ifthenelse(cond, then, otherwise),
ExprKind::Lit(ref lit) => Some(lit_to_constant(&lit.node, self.tables.expr_ty(e))),
ExprKind::Array(ref vec) => self.multi(vec).map(Constant::Vec),
ExprKind::Tup(ref tup) => self.multi(tup).map(Constant::Tuple),
Expand Down Expand Up @@ -358,10 +359,10 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
}
}

fn ifthenelse(&mut self, cond: &Expr, then: &P<Expr>, otherwise: &Option<P<Expr>>) -> Option<Constant> {
fn ifthenelse(&mut self, cond: &Expr, then: &Expr, otherwise: Option<&Expr>) -> Option<Constant> {
if let Some(Constant::Bool(b)) = self.expr(cond) {
if b {
self.expr(&**then)
self.expr(&*then)
} else {
otherwise.as_ref().and_then(|expr| self.expr(expr))
}
Expand Down
16 changes: 7 additions & 9 deletions clippy_lints/src/copies.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::{get_parent_expr, in_macro, snippet, span_lint_and_then, span_note_and_lint};
use crate::utils::{get_parent_expr, higher, in_macro, snippet, span_lint_and_then, span_note_and_lint};
use crate::utils::{SpanlessEq, SpanlessHash};
use rustc::hir::*;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
Expand Down Expand Up @@ -109,13 +109,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CopyAndPaste {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
if !in_macro(expr.span) {
// skip ifs directly in else, it will be checked in the parent if
if let Some(&Expr {
node: ExprKind::If(_, _, Some(ref else_expr)),
..
}) = get_parent_expr(cx, expr)
{
if else_expr.hir_id == expr.hir_id {
return;
if let Some(expr) = get_parent_expr(cx, expr) {
if let Some((_, _, Some(ref else_expr))) = higher::if_block(&expr) {
if else_expr.hir_id == expr.hir_id {
return;
}
}
}

Expand Down Expand Up @@ -236,7 +234,7 @@ fn if_sequence(mut expr: &Expr) -> (SmallVec<[&Expr; 1]>, SmallVec<[&Block; 1]>)
let mut conds = SmallVec::new();
let mut blocks: SmallVec<[&Block; 1]> = SmallVec::new();

while let ExprKind::If(ref cond, ref then_expr, ref else_expr) = expr.node {
while let Some((ref cond, ref then_expr, ref else_expr)) = higher::if_block(&expr) {
conds.push(&**cond);
if let ExprKind::Block(ref block, _) = then_expr.node {
blocks.push(block);
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/entry.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::utils::SpanlessEq;
use crate::utils::{get_item_name, match_type, paths, snippet, span_lint_and_then, walk_ptrs_ty};
use crate::utils::{get_item_name, higher, match_type, paths, snippet, span_lint_and_then, walk_ptrs_ty};
use if_chain::if_chain;
use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
use rustc::hir::*;
Expand Down Expand Up @@ -41,7 +41,7 @@ declare_lint_pass!(HashMapPass => [MAP_ENTRY]);

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for HashMapPass {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
if let ExprKind::If(ref check, ref then_block, ref else_block) = expr.node {
if let Some((ref check, ref then_block, ref else_block)) = higher::if_block(&expr) {
if let ExprKind::Unary(UnOp::UnNot, ref check) = check.node {
if let Some((ty, map, key)) = check_cond(cx, check) {
// in case of `if !m.contains_key(&k) { m.insert(k, v); }`
Expand Down
7 changes: 0 additions & 7 deletions clippy_lints/src/implicit_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,6 @@ impl ImplicitReturn {
Self::lint(cx, expr.span, break_expr.span, "change `break` to `return` as shown");
}
},
ExprKind::If(.., if_expr, else_expr) => {
Self::expr_match(cx, if_expr);

if let Some(else_expr) = else_expr {
Self::expr_match(cx, else_expr);
}
},
ExprKind::Match(.., arms, source) => {
let check_all_arms = match source {
MatchSource::IfLetDesugar {
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/let_if_seq.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::{snippet, span_lint_and_then};
use crate::utils::{higher, snippet, span_lint_and_then};
use if_chain::if_chain;
use rustc::hir;
use rustc::hir::def::Res;
Expand Down Expand Up @@ -63,7 +63,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetIfSeq {
if let hir::StmtKind::Local(ref local) = stmt.node;
if let hir::PatKind::Binding(mode, canonical_id, ident, None) = local.pat.node;
if let hir::StmtKind::Expr(ref if_) = expr.node;
if let hir::ExprKind::If(ref cond, ref then, ref else_) = if_.node;
if let Some((ref cond, ref then, ref else_)) = higher::if_block(&if_);
if !used_in_expr(cx, canonical_id, cond);
if let hir::ExprKind::Block(ref then, _) = then.node;
if let Some(value) = check_assign(cx, canonical_id, &*then);
Expand Down
10 changes: 1 addition & 9 deletions clippy_lints/src/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,14 +686,6 @@ fn never_loop_expr(expr: &Expr, main_loop_id: HirId) -> NeverLoopResult {
| ExprKind::Assign(ref e1, ref e2)
| ExprKind::AssignOp(_, ref e1, ref e2)
| ExprKind::Index(ref e1, ref e2) => never_loop_expr_all(&mut [&**e1, &**e2].iter().cloned(), main_loop_id),
ExprKind::If(ref e, ref e2, ref e3) => {
let e1 = never_loop_expr(e, main_loop_id);
let e2 = never_loop_expr(e2, main_loop_id);
let e3 = e3
.as_ref()
.map_or(NeverLoopResult::Otherwise, |e| never_loop_expr(e, main_loop_id));
combine_seq(e1, combine_branches(e2, e3))
},
ExprKind::Loop(ref b, _, _) => {
// Break can come from the inner loop so remove them.
absorb_break(&never_loop_block(b, main_loop_id))
Expand Down Expand Up @@ -2204,7 +2196,7 @@ fn is_loop(expr: &Expr) -> bool {

fn is_conditional(expr: &Expr) -> bool {
match expr.node {
ExprKind::If(..) | ExprKind::Match(..) => true,
ExprKind::Match(..) => true,
_ => false,
}
}
Expand Down
1 change: 0 additions & 1 deletion clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1266,7 +1266,6 @@ fn lint_expect_fun_call(cx: &LateContext<'_, '_>, expr: &hir::Expr, method_span:
hir::ExprKind::Call(..)
| hir::ExprKind::MethodCall(..)
// These variants are debatable or require further examination
| hir::ExprKind::If(..)
| hir::ExprKind::Match(..)
| hir::ExprKind::Block{ .. } => true,
_ => false,
Expand Down
6 changes: 0 additions & 6 deletions clippy_lints/src/methods/unnecessary_filter_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,6 @@ fn check_expression<'a, 'tcx: 'a>(
(false, false)
}
},
// There must be an else_arm or there will be a type error
hir::ExprKind::If(_, ref if_arm, Some(ref else_arm)) => {
let if_check = check_expression(cx, arg_id, if_arm);
let else_check = check_expression(cx, arg_id, else_arm);
(if_check.0 | else_check.0, if_check.1 | else_check.1)
},
hir::ExprKind::Match(_, ref arms, _) => {
let mut found_mapping = false;
let mut found_filtering = false;
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/needless_bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! This lint is **warn** by default

use crate::utils::sugg::Sugg;
use crate::utils::{in_macro, span_lint, span_lint_and_sugg};
use crate::utils::{higher, in_macro, span_lint, span_lint_and_sugg};
use rustc::hir::*;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use rustc::{declare_lint_pass, declare_tool_lint};
Expand Down Expand Up @@ -59,7 +59,7 @@ declare_lint_pass!(NeedlessBool => [NEEDLESS_BOOL]);
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBool {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
use self::Expression::*;
if let ExprKind::If(ref pred, ref then_block, Some(ref else_expr)) = e.node {
if let Some((ref pred, ref then_block, Some(ref else_expr))) = higher::if_block(&e) {
let reduce = |ret, not| {
let mut applicability = Applicability::MachineApplicable;
let snip = Sugg::hir_with_applicability(cx, pred, "<predicate>", &mut applicability);
Expand Down Expand Up @@ -119,7 +119,7 @@ fn parent_node_is_if_expr<'a, 'b>(expr: &Expr, cx: &LateContext<'a, 'b>) -> bool
let parent_node = cx.tcx.hir().get_by_hir_id(parent_id);

if let rustc::hir::Node::Expr(e) = parent_node {
if let ExprKind::If(_, _, _) = e.node {
if higher::if_block(&e).is_some() {
return true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/question_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use syntax::ptr::P;

use crate::utils::paths::*;
use crate::utils::sugg::Sugg;
use crate::utils::{match_type, span_lint_and_then, SpanlessEq};
use crate::utils::{higher, match_type, span_lint_and_then, SpanlessEq};

declare_clippy_lint! {
/// **What it does:** Checks for expressions that could be replaced by the question mark operator.
Expand Down Expand Up @@ -48,7 +48,7 @@ impl QuestionMark {
/// If it matches, it will suggest to use the question mark operator instead
fn check_is_none_and_early_return_none(cx: &LateContext<'_, '_>, expr: &Expr) {
if_chain! {
if let ExprKind::If(if_expr, body, else_) = &expr.node;
if let Some((if_expr, body, else_)) = higher::if_block(&expr);
if let ExprKind::MethodCall(segment, _, args) = &if_expr.node;
if segment.ident.name == "is_none";
if Self::expression_returns_none(cx, body);
Expand Down
7 changes: 0 additions & 7 deletions clippy_lints/src/shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,6 @@ fn check_expr<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, bindings:
check_expr(cx, e, bindings)
}
},
ExprKind::If(ref cond, ref then, ref otherwise) => {
check_expr(cx, cond, bindings);
check_expr(cx, &**then, bindings);
if let Some(ref o) = *otherwise {
check_expr(cx, o, bindings);
}
},
ExprKind::While(ref cond, ref block, _) => {
check_expr(cx, cond, bindings);
check_block(cx, block, bindings);
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/unwrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use if_chain::if_chain;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use rustc::{declare_lint_pass, declare_tool_lint};

use crate::utils::{in_macro, match_type, paths, span_lint_and_then, usage::is_potentially_mutated};
use crate::utils::{higher::if_block, in_macro, match_type, paths, span_lint_and_then, usage::is_potentially_mutated};
use rustc::hir::intravisit::*;
use rustc::hir::*;
use syntax::source_map::Span;
Expand Down Expand Up @@ -130,7 +130,7 @@ impl<'a, 'tcx: 'a> UnwrappableVariablesVisitor<'a, 'tcx> {

impl<'a, 'tcx: 'a> Visitor<'tcx> for UnwrappableVariablesVisitor<'a, 'tcx> {
fn visit_expr(&mut self, expr: &'tcx Expr) {
if let ExprKind::If(cond, then, els) = &expr.node {
if let Some((cond, then, els)) = if_block(&expr) {
walk_expr(self, cond);
self.visit_branch(cond, then, false);
if let Some(els) = els {
Expand Down
51 changes: 31 additions & 20 deletions clippy_lints/src/utils/author.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! A group of attributes that can be attached to Rust code in order
//! to generate a clippy lint detecting said code automatically.

use crate::utils::get_attr;
use crate::utils::{get_attr, higher};
use rustc::hir;
use rustc::hir::intravisit::{NestedVisitorMap, Visitor};
use rustc::hir::{BindingAnnotation, Expr, ExprKind, Pat, PatKind, QPath, Stmt, StmtKind, TyKind};
Expand Down Expand Up @@ -187,6 +187,32 @@ struct PrintVisitor {
impl<'tcx> Visitor<'tcx> for PrintVisitor {
#[allow(clippy::too_many_lines)]
fn visit_expr(&mut self, expr: &Expr) {
// handle if desugarings
// TODO add more desugarings here
if let Some((cond, then, opt_else)) = higher::if_block(&expr) {
let cond_pat = self.next("cond");
let then_pat = self.next("then");
if let Some(else_) = opt_else {
let else_pat = self.next("else_");
println!(
" if let Some((ref {}, ref {}, Some({}))) = higher::if_block(&{});",
cond_pat, then_pat, else_pat, self.current
);
self.current = else_pat;
self.visit_expr(else_);
} else {
println!(
" if let Some((ref {}, ref {}, None)) = higher::if_block(&{});",
cond_pat, then_pat, self.current
);
}
self.current = cond_pat;
self.visit_expr(cond);
self.current = then_pat;
self.visit_expr(then);
return;
}

print!(" if let ExprKind::");
let current = format!("{}.node", self.current);
match expr.node {
Expand Down Expand Up @@ -296,25 +322,6 @@ impl<'tcx> Visitor<'tcx> for PrintVisitor {
self.current = cast_pat;
self.visit_expr(expr);
},
ExprKind::If(ref cond, ref then, ref opt_else) => {
let cond_pat = self.next("cond");
let then_pat = self.next("then");
if let Some(ref else_) = *opt_else {
let else_pat = self.next("else_");
println!(
"If(ref {}, ref {}, Some(ref {})) = {};",
cond_pat, then_pat, else_pat, current
);
self.current = else_pat;
self.visit_expr(else_);
} else {
println!("If(ref {}, ref {}, None) = {};", cond_pat, then_pat, current);
}
self.current = cond_pat;
self.visit_expr(cond);
self.current = then_pat;
self.visit_expr(then);
},
ExprKind::While(ref cond, ref body, _) => {
let cond_pat = self.next("cond");
let body_pat = self.next("body");
Expand Down Expand Up @@ -684,6 +691,10 @@ fn desugaring_name(des: hir::MatchSource) -> String {
"MatchSource::IfLetDesugar {{ contains_else_clause: {} }}",
contains_else_clause
),
hir::MatchSource::IfDesugar { contains_else_clause } => format!(
"MatchSource::IfDesugar {{ contains_else_clause: {} }}",
contains_else_clause
),
hir::MatchSource::AwaitDesugar => "MatchSource::AwaitDesugar".to_string(),
}
}
Expand Down
21 changes: 21 additions & 0 deletions clippy_lints/src/utils/higher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,27 @@ pub fn for_loop(expr: &hir::Expr) -> Option<(&hir::Pat, &hir::Expr, &hir::Expr)>
None
}

/// Recover the essential nodes of a desugared if block
/// `if cond { then } else { els }` becomes `(cond, then, Some(els))`
pub fn if_block(expr: &hir::Expr) -> Option<(&hir::Expr, &hir::Expr, Option<&hir::Expr>)> {
if let hir::ExprKind::Match(ref cond, ref arms, hir::MatchSource::IfDesugar { contains_else_clause }) = expr.node {
let cond = if let hir::ExprKind::DropTemps(ref cond) = cond.node {
cond
} else {
panic!("If block desugar must contain DropTemps");
};
let then = &arms[0].body;
let els = if contains_else_clause {
Some(&*arms[1].body)
} else {
None
};
Some((cond, then, els))
} else {
None
}
}

/// Represent the pre-expansion arguments of a `vec!` invocation.
pub enum VecArgs<'a> {
/// `vec![elem; len]`
Expand Down
12 changes: 0 additions & 12 deletions clippy_lints/src/utils/hir_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,6 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
(&ExprKind::Index(ref la, ref li), &ExprKind::Index(ref ra, ref ri)) => {
self.eq_expr(la, ra) && self.eq_expr(li, ri)
},
(&ExprKind::If(ref lc, ref lt, ref le), &ExprKind::If(ref rc, ref rt, ref re)) => {
self.eq_expr(lc, rc) && self.eq_expr(&**lt, &**rt) && both(le, re, |l, r| self.eq_expr(l, r))
},
(&ExprKind::Lit(ref l), &ExprKind::Lit(ref r)) => l.node == r.node,
(&ExprKind::Loop(ref lb, ref ll, ref lls), &ExprKind::Loop(ref rb, ref rl, ref rls)) => {
lls == rls && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.ident.as_str() == r.ident.as_str())
Expand Down Expand Up @@ -493,15 +490,6 @@ impl<'a, 'tcx: 'a> SpanlessHash<'a, 'tcx> {
let c: fn(_, _, _) -> _ = ExprKind::InlineAsm;
c.hash(&mut self.s);
},
ExprKind::If(ref cond, ref t, ref e) => {
let c: fn(_, _, _) -> _ = ExprKind::If;
c.hash(&mut self.s);
self.hash_expr(cond);
self.hash_expr(&**t);
if let Some(ref e) = *e {
self.hash_expr(e);
}
},
ExprKind::Lit(ref l) => {
let c: fn(_) -> _ = ExprKind::Lit;
c.hash(&mut self.s);
Expand Down
Loading

0 comments on commit 3710ec5

Please sign in to comment.