]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/utils/higher.rs
Auto merge of #3680 - g-bartoszek:needless-bool-else-if-brackets, r=oli-obk
[rust.git] / clippy_lints / src / utils / higher.rs
index 29c7260f4912a5c03320127c83333ba4fd04c7c4..537cdf55eb146d5fff6cd0933aaa8877bab3db8f 100644 (file)
@@ -1,23 +1,13 @@
-// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-
 //! This module contains functions for retrieve the original AST from lowered
 //! `hir`.
 
 #![deny(clippy::missing_docs_in_private_items)]
 
-use if_chain::if_chain;
-use crate::rustc::{hir, ty};
-use crate::rustc::lint::LateContext;
-use crate::syntax::ast;
 use crate::utils::{is_expn_of, match_def_path, match_qpath, opt_def_id, paths, resolve_node};
+use if_chain::if_chain;
+use rustc::lint::LateContext;
+use rustc::{hir, ty};
+use syntax::ast;
 
 /// Convert a hir binary operator to the corresponding `ast` type.
 pub fn binop(op: hir::BinOpKind) -> ast::BinOpKind {
@@ -64,7 +54,6 @@ fn get_field<'a>(name: &str, fields: &'a [hir::Field]) -> Option<&'a hir::Expr>
         Some(expr)
     }
 
-
     let def_path = match cx.tables.expr_ty(expr).sty {
         ty::Adt(def, _) => cx.tcx.def_path(def.did),
         _ => return None,
@@ -109,64 +98,67 @@ fn get_field<'a>(name: &str, fields: &'a [hir::Field]) -> Option<&'a hir::Expr>
                 None
             }
         },
-        hir::ExprKind::Call(ref path, ref args) => if let hir::ExprKind::Path(ref path) = path.node {
-            if match_qpath(path, &paths::RANGE_INCLUSIVE_STD_NEW) || match_qpath(path, &paths::RANGE_INCLUSIVE_NEW) {
+        hir::ExprKind::Call(ref path, ref args) => {
+            if let hir::ExprKind::Path(ref path) = path.node {
+                if match_qpath(path, &paths::RANGE_INCLUSIVE_STD_NEW) || match_qpath(path, &paths::RANGE_INCLUSIVE_NEW)
+                {
+                    Some(Range {
+                        start: Some(&args[0]),
+                        end: Some(&args[1]),
+                        limits: ast::RangeLimits::Closed,
+                    })
+                } else {
+                    None
+                }
+            } else {
+                None
+            }
+        },
+        hir::ExprKind::Struct(ref path, ref fields, None) => {
+            if match_qpath(path, &paths::RANGE_FROM_STD) || match_qpath(path, &paths::RANGE_FROM) {
+                Some(Range {
+                    start: Some(get_field("start", fields)?),
+                    end: None,
+                    limits: ast::RangeLimits::HalfOpen,
+                })
+            } else if match_qpath(path, &paths::RANGE_STD) || match_qpath(path, &paths::RANGE) {
                 Some(Range {
-                    start: Some(&args[0]),
-                    end: Some(&args[1]),
+                    start: Some(get_field("start", fields)?),
+                    end: Some(get_field("end", fields)?),
+                    limits: ast::RangeLimits::HalfOpen,
+                })
+            } else if match_qpath(path, &paths::RANGE_TO_INCLUSIVE_STD) || match_qpath(path, &paths::RANGE_TO_INCLUSIVE)
+            {
+                Some(Range {
+                    start: None,
+                    end: Some(get_field("end", fields)?),
                     limits: ast::RangeLimits::Closed,
                 })
+            } else if match_qpath(path, &paths::RANGE_TO_STD) || match_qpath(path, &paths::RANGE_TO) {
+                Some(Range {
+                    start: None,
+                    end: Some(get_field("end", fields)?),
+                    limits: ast::RangeLimits::HalfOpen,
+                })
             } else {
                 None
             }
-        } else {
-            None
-        },
-        hir::ExprKind::Struct(ref path, ref fields, None) => if match_qpath(path, &paths::RANGE_FROM_STD)
-            || match_qpath(path, &paths::RANGE_FROM)
-        {
-            Some(Range {
-                start: Some(get_field("start", fields)?),
-                end: None,
-                limits: ast::RangeLimits::HalfOpen,
-            })
-        } else if match_qpath(path, &paths::RANGE_STD) || match_qpath(path, &paths::RANGE) {
-            Some(Range {
-                start: Some(get_field("start", fields)?),
-                end: Some(get_field("end", fields)?),
-                limits: ast::RangeLimits::HalfOpen,
-            })
-        } else if match_qpath(path, &paths::RANGE_TO_INCLUSIVE_STD) || match_qpath(path, &paths::RANGE_TO_INCLUSIVE) {
-            Some(Range {
-                start: None,
-                end: Some(get_field("end", fields)?),
-                limits: ast::RangeLimits::Closed,
-            })
-        } else if match_qpath(path, &paths::RANGE_TO_STD) || match_qpath(path, &paths::RANGE_TO) {
-            Some(Range {
-                start: None,
-                end: Some(get_field("end", fields)?),
-                limits: ast::RangeLimits::HalfOpen,
-            })
-        } else {
-            None
         },
         _ => None,
     }
 }
 
-/// Checks if a `let` decl is from a `for` loop desugaring.
-pub fn is_from_for_desugar(decl: &hir::Decl) -> bool {
+/// Checks if a `let` statement is from a `for` loop desugaring.
+pub fn is_from_for_desugar(local: &hir::Local) -> bool {
     // This will detect plain for-loops without an actual variable binding:
     //
     // ```
     // for x in some_vec {
-    //   // do stuff
+    //     // do stuff
     // }
     // ```
     if_chain! {
-        if let hir::DeclKind::Local(ref loc) = decl.node;
-        if let Some(ref expr) = loc.init;
+        if let Some(ref expr) = local.init;
         if let hir::ExprKind::Match(_, _, hir::MatchSource::ForLoopDesugar) = expr.node;
         then {
             return true;
@@ -178,15 +170,11 @@ pub fn is_from_for_desugar(decl: &hir::Decl) -> bool {
     //
     // ```
     // for _ in vec![()] {
-    //   // anything
+    //     // anything
     // }
     // ```
-    if_chain! {
-        if let hir::DeclKind::Local(ref loc) = decl.node;
-        if let hir::LocalSource::ForLoopDesugar = loc.source;
-        then {
-            return true;
-        }
+    if let hir::LocalSource::ForLoopDesugar = local.source {
+        return true;
     }
 
     false
@@ -202,11 +190,10 @@ pub fn for_loop(expr: &hir::Expr) -> Option<(&hir::Pat, &hir::Expr, &hir::Expr)>
         if let hir::ExprKind::Loop(ref block, _, _) = arms[0].body.node;
         if block.expr.is_none();
         if let [ _, _, ref let_stmt, ref body ] = *block.stmts;
-        if let hir::StmtKind::Decl(ref decl, _) = let_stmt.node;
-        if let hir::DeclKind::Local(ref decl) = decl.node;
-        if let hir::StmtKind::Expr(ref expr, _) = body.node;
+        if let hir::StmtKind::Local(ref local) = let_stmt.node;
+        if let hir::StmtKind::Expr(ref expr) = body.node;
         then {
-            return Some((&*decl.pat, &iterargs[0], expr));
+            return Some((&*local.pat, &iterargs[0], expr));
         }
     }
     None