willothy / worth

This match statement can be collapsed into an if-let statement RS-W1006
Anti-pattern
Minor
a year agoa year old
Using match to destructure a single pattern: "macro", prefer an if-let
203            }
204            InstructionKind::Keyword(Keyword::End { .. }) => {
205                let (kind, start_ip) = macro_stack.pop().unwrap();
206                match kind {207                    "macro" => {208                        if in_macro {209                            in_macro = false;210211                            program.macros.insert(212                                macro_name.clone(),213                                Macro {214                                    name: macro_name.clone(),215                                    body: macro_body.clone(),216                                    loc: (start_ip, ip),217                                    uses: vec![],218                                },219                            );220                            macro_name.clear();221                            macro_body.clear();222                            continue;223                        } else {224                            err!(225                                program,226                                PreprocessorError(UnexpectedMacroEnd),227                                "Unexpected macro end",228                                ip229                            );230                        }231                    }232                    _ => {}233                }234            }
235            InstructionKind::Keyword(Keyword::If { .. }) => {
236                macro_stack.push(("if", ip));
Using match to destructure a single pattern: "macro", prefer an if-let
302            InstructionKind::Keyword(Keyword::End { .. }) => {
303                let kind = macro_stack.pop().unwrap();
304
305                match kind {306                    "macro" => {307                        if in_macro {308                            in_macro = false;309                            continue;310                        }311                    }312                    _ => {}313                }314            }
315            _ => {}
316        }
Using match to destructure a single pattern: InstructionKind::Intrinsic(Intrinsic::Here), prefer an if-let
 52
 53fn here(program: &mut Program) -> Result<()> {
 54    for instruction in &mut program.instructions {
 55        match instruction.kind { 56            InstructionKind::Intrinsic(Intrinsic::Here) => { 57                let loc = instruction.loc.clone(); 58                *instruction = Instruction { 59                    kind: InstructionKind::Push(Value::Str( 60                        loc.0.clone() + ":" + &loc.1.to_string() + ":" + &loc.2.to_string(), 61                    )), 62                    loc: loc, 63                    ip: instruction.ip, 64                }; 65            } 66            _ => {} 67        } 68    }
 69    Ok(())
 70}