"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));
"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 }
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}
match
statements with only one useful arm can be reduced to an if let
statement.
This helps improve readability, an if let
statement nests lesser than a match
.
Use the pattern of the single match arm as the pattern within an if let
statement.
The useless match arm can be omitted.
let v = Some(3);
match v {
Some(n) => {
println!("The value of v is: {}", n)
},
_ => {},
}
let v = Some(3);
if let Some(n) = v {
println!("The value of v is: {}", n)
}