| 1 | // SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 | |
| 3 | use syn::{ |
| 4 | visit_mut::{self, VisitMut}, |
| 5 | Arm, Block, Expr, ExprBlock, ExprBreak, ExprCall, ExprIf, ExprLoop, ExprMacro, ExprMatch, |
| 6 | ExprMethodCall, ExprParen, ExprPath, ExprTry, ExprUnsafe, Item, Label, Lifetime, LocalInit, |
| 7 | Macro, Stmt, StmtMacro, Token, |
| 8 | }; |
| 9 | |
| 10 | use super::{visitor, Context, NAME, NESTED, NEVER}; |
| 11 | use crate::utils::{expr_block, path_eq, replace_block, replace_expr, Attrs as _}; |
| 12 | |
| 13 | /// Visits last expression. |
| 14 | /// |
| 15 | /// Note that do not use this after `cx.visitor()`. |
| 16 | pub(super) fn child_expr(cx: &mut Context, expr: &mut Expr) { |
| 17 | if !cx.visit_last() || is_unreachable(cx, expr) { |
| 18 | return; |
| 19 | } |
| 20 | |
| 21 | match expr { |
| 22 | Expr::Block(ExprBlock { block: &mut Block, .. }) | Expr::Unsafe(ExprUnsafe { block: &mut Block, .. }) => { |
| 23 | if let Some(Stmt::Expr(expr: &mut Expr, None)) = block.stmts.last_mut() { |
| 24 | child_expr(cx, expr); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | Expr::Match(expr: &mut ExprMatch) => visit_last_expr_match(cx, expr), |
| 29 | Expr::If(expr: &mut ExprIf) => visit_last_expr_if(cx, expr), |
| 30 | Expr::Loop(expr: &mut ExprLoop) => visit_last_expr_loop(cx, expr), |
| 31 | |
| 32 | // Search recursively |
| 33 | Expr::MethodCall(ExprMethodCall { receiver: expr: &mut Box, .. }) |
| 34 | | Expr::Paren(ExprParen { expr: &mut Box, .. }) => child_expr(cx, expr), |
| 35 | |
| 36 | _ => {} |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | pub(super) fn is_unreachable(cx: &Context, expr: &Expr) -> bool { |
| 41 | if expr.any_empty_attr(NEVER) || expr.any_attr(NAME) { |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | match expr { |
| 46 | Expr::Block(ExprBlock { block, .. }) | Expr::Unsafe(ExprUnsafe { block, .. }) => { |
| 47 | is_unreachable_stmt(cx, block.stmts.last()) |
| 48 | } |
| 49 | |
| 50 | Expr::Break(_) | Expr::Continue(_) | Expr::Return(_) => true, |
| 51 | |
| 52 | Expr::Macro(ExprMacro { mac, .. }) => is_unreachable_macro(cx, mac), |
| 53 | |
| 54 | Expr::Match(ExprMatch { arms, .. }) => { |
| 55 | arms.iter().all(|arm| arm.any_empty_attr(NEVER) || is_unreachable(cx, &arm.body)) |
| 56 | } |
| 57 | |
| 58 | // `Err(expr)?` or `None?`. |
| 59 | Expr::Try(ExprTry { expr, .. }) => match &**expr { |
| 60 | Expr::Path(ExprPath { path, qself: None, .. }) => { |
| 61 | path_eq(path, &["std" , "core" ], &["option" , "Option" , "None" ]) |
| 62 | } |
| 63 | Expr::Call(ExprCall { args, func, .. }) if args.len() == 1 => match &**func { |
| 64 | Expr::Path(ExprPath { path, qself: None, .. }) => { |
| 65 | path_eq(path, &["std" , "core" ], &["result" , "Result" , "Err" ]) |
| 66 | } |
| 67 | _ => false, |
| 68 | }, |
| 69 | _ => false, |
| 70 | }, |
| 71 | |
| 72 | // Search recursively |
| 73 | Expr::MethodCall(ExprMethodCall { receiver: expr, .. }) |
| 74 | | Expr::Paren(ExprParen { expr, .. }) => is_unreachable(cx, expr), |
| 75 | |
| 76 | _ => false, |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | fn is_unreachable_macro(cx: &Context, mac: &Macro) -> bool { |
| 81 | const UNREACHABLE_MACROS: &[&str] = &["unreachable" , "panic" ]; |
| 82 | |
| 83 | // `unreachable!`, `panic!` or an expression level marker (`marker!` macro). |
| 84 | UNREACHABLE_MACROS.iter().any(|i: &&str| path_eq(&mac.path, &["std" , "core" ], &[i])) |
| 85 | || cx.is_marker_macro(mac) |
| 86 | } |
| 87 | |
| 88 | fn is_unreachable_stmt(cx: &Context, stmt: Option<&Stmt>) -> bool { |
| 89 | match stmt { |
| 90 | Some(Stmt::Expr(expr: &Expr, _)) => is_unreachable(cx, expr), |
| 91 | Some(Stmt::Local(local: &Local)) => { |
| 92 | local.init.as_ref().map_or(default:false, |LocalInit { expr: &Box, .. }| is_unreachable(cx, expr)) |
| 93 | } |
| 94 | Some(Stmt::Item(_)) => true, |
| 95 | Some(Stmt::Macro(StmtMacro { mac: &Macro, .. })) => is_unreachable_macro(cx, mac), |
| 96 | None => false, |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | fn visit_last_expr_match(cx: &mut Context, expr: &mut ExprMatch) { |
| 101 | fn skip(cx: &Context, arm: &mut Arm) -> bool { |
| 102 | arm.any_empty_attr(NEVER) |
| 103 | || arm.any_empty_attr(NESTED) |
| 104 | || is_unreachable(cx, &arm.body) |
| 105 | || visitor::find_nested(node:arm) |
| 106 | } |
| 107 | |
| 108 | for arm: &mut Arm in &mut expr.arms { |
| 109 | if !skip(cx, arm) { |
| 110 | arm.comma = Some(<Token![,]>::default()); |
| 111 | replace_expr(&mut arm.body, |x: Expr| cx.next_expr(x)); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | fn visit_last_expr_if(cx: &mut Context, expr: &mut ExprIf) { |
| 117 | fn skip(cx: &Context, block: &mut Block) -> bool { |
| 118 | match block.stmts.last_mut() { |
| 119 | Some(Stmt::Expr(expr, None)) => { |
| 120 | expr.any_empty_attr(NESTED) |
| 121 | || is_unreachable(cx, expr) |
| 122 | || visitor::find_nested(block) |
| 123 | } |
| 124 | _ => is_unreachable_stmt(cx, block.stmts.last()), |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if !skip(cx, &mut expr.then_branch) { |
| 129 | replace_block(&mut expr.then_branch, |b| cx.next_expr(expr_block(b))); |
| 130 | } |
| 131 | |
| 132 | match expr.else_branch.as_mut().map(|(_, expr)| &mut **expr) { |
| 133 | Some(Expr::Block(expr)) => { |
| 134 | if !skip(cx, &mut expr.block) { |
| 135 | replace_block(&mut expr.block, |b| cx.next_expr(expr_block(b))); |
| 136 | } |
| 137 | } |
| 138 | Some(Expr::If(expr)) => visit_last_expr_if(cx, expr), |
| 139 | |
| 140 | // TODO: https://docs.rs/proc-macro2/1/proc_macro2/struct.Span.html#method.join |
| 141 | // `expr.span().join(expr.then_branch.span()).unwrap_or_else(|| expr.span())`` |
| 142 | None => cx.error(format_err!(expr.if_token, "`if` expression missing an else clause" )), |
| 143 | |
| 144 | Some(_) => unreachable!("wrong_if" ), |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | fn visit_last_expr_loop(cx: &mut Context, expr: &mut ExprLoop) { |
| 149 | struct LoopVisitor<'a> { |
| 150 | cx: &'a mut Context, |
| 151 | label: Option<&'a Label>, |
| 152 | nested: bool, |
| 153 | } |
| 154 | |
| 155 | impl LoopVisitor<'_> { |
| 156 | fn compare_labels(&self, other: Option<&Lifetime>) -> bool { |
| 157 | match (self.label, other) { |
| 158 | (None, None) => true, |
| 159 | (Some(this), Some(other)) => this.name.ident == other.ident, |
| 160 | _ => false, |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | impl VisitMut for LoopVisitor<'_> { |
| 166 | fn visit_expr_mut(&mut self, node: &mut Expr) { |
| 167 | if node.any_empty_attr(NEVER) { |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | let tmp = self.nested; |
| 172 | match node { |
| 173 | // Stop at closure / async block bounds |
| 174 | Expr::Closure(_) | Expr::Async(_) => return, |
| 175 | // Other loop bounds |
| 176 | Expr::Loop(_) | Expr::ForLoop(_) | Expr::While(_) => { |
| 177 | if self.label.is_none() { |
| 178 | return; |
| 179 | } |
| 180 | self.nested = true; |
| 181 | } |
| 182 | // Desugar `break <expr>` into `break Enum::VariantN(<expr>)`. |
| 183 | Expr::Break(ExprBreak { label, expr, .. }) => { |
| 184 | if !self.nested && label.is_none() || self.compare_labels(label.as_ref()) { |
| 185 | self.cx.replace_boxed_expr(expr); |
| 186 | } |
| 187 | } |
| 188 | _ => {} |
| 189 | } |
| 190 | |
| 191 | visit_mut::visit_expr_mut(self, node); |
| 192 | self.nested = tmp; |
| 193 | } |
| 194 | |
| 195 | fn visit_item_mut(&mut self, _: &mut Item) { |
| 196 | // Do not recurse into nested items. |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | LoopVisitor { cx, label: expr.label.as_ref(), nested: false }.visit_block_mut(&mut expr.body); |
| 201 | } |
| 202 | |