]> git.lizzy.rs Git - rust.git/blobdiff - src/tools/clippy/clippy_lints/src/utils/hir_utils.rs
Auto merge of #79780 - camelid:use-summary_opts, r=GuillaumeGomez
[rust.git] / src / tools / clippy / clippy_lints / src / utils / hir_utils.rs
index c9e639e8728fd40a69ac4a0dd63985bce6c5b9d1..d942d4e12b10629518cf54d5fac19c97281e1e37 100644 (file)
@@ -81,7 +81,7 @@ pub fn eq_expr(&mut self, left: &Expr<'_>, right: &Expr<'_>) -> bool {
             }
         }
 
-        match (&left.kind, &right.kind) {
+        match (&reduce_exprkind(&left.kind), &reduce_exprkind(&right.kind)) {
             (&ExprKind::AddrOf(lb, l_mut, ref le), &ExprKind::AddrOf(rb, r_mut, ref re)) => {
                 lb == rb && l_mut == r_mut && self.eq_expr(le, re)
             },
@@ -261,14 +261,8 @@ pub fn eq_path_segments(&mut self, left: &[PathSegment<'_>], right: &[PathSegmen
     pub fn eq_path_segment(&mut self, left: &PathSegment<'_>, right: &PathSegment<'_>) -> bool {
         // The == of idents doesn't work with different contexts,
         // we have to be explicit about hygiene
-        if left.ident.as_str() != right.ident.as_str() {
-            return false;
-        }
-        match (&left.args, &right.args) {
-            (&None, &None) => true,
-            (&Some(ref l), &Some(ref r)) => self.eq_path_parameters(l, r),
-            _ => false,
-        }
+        left.ident.as_str() == right.ident.as_str()
+            && both(&left.args, &right.args, |l, r| self.eq_path_parameters(l, r))
     }
 
     pub fn eq_ty(&mut self, left: &Ty<'_>, right: &Ty<'_>) -> bool {
@@ -312,6 +306,32 @@ fn eq_type_binding(&mut self, left: &TypeBinding<'_>, right: &TypeBinding<'_>) -
     }
 }
 
+/// Some simple reductions like `{ return }` => `return`
+fn reduce_exprkind<'hir>(kind: &'hir ExprKind<'hir>) -> &ExprKind<'hir> {
+    if let ExprKind::Block(block, _) = kind {
+        match (block.stmts, block.expr) {
+            // `{}` => `()`
+            ([], None) => &ExprKind::Tup(&[]),
+            ([], Some(expr)) => match expr.kind {
+                // `{ return .. }` => `return ..`
+                ExprKind::Ret(..) => &expr.kind,
+                _ => kind,
+            },
+            ([stmt], None) => match stmt.kind {
+                StmtKind::Expr(expr) | StmtKind::Semi(expr) => match expr.kind {
+                    // `{ return ..; }` => `return ..`
+                    ExprKind::Ret(..) => &expr.kind,
+                    _ => kind,
+                },
+                _ => kind,
+            },
+            _ => kind,
+        }
+    } else {
+        kind
+    }
+}
+
 fn swap_binop<'a>(
     binop: BinOpKind,
     lhs: &'a Expr<'a>,
@@ -497,7 +517,7 @@ pub fn hash_expr(&mut self, e: &Expr<'_>) {
                     }
                 }
                 asm.options.hash(&mut self.s);
-                for op in asm.operands {
+                for (op, _op_sp) in asm.operands {
                     match op {
                         InlineAsmOperand::In { reg, expr } => {
                             reg.hash(&mut self.s);