From b8726f03dfb04eddcac2ec0e02dd7b0d6e07e9b6 Mon Sep 17 00:00:00 2001 From: coolreader18 <33094578+coolreader18@users.noreply.github.com> Date: Sat, 14 Sep 2019 15:16:02 -0500 Subject: [PATCH] Don't emit None, ReturnValue if the last statement in a function is a return --- src/compile.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/compile.rs b/src/compile.rs index 890ff8e..b1b5f62 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -842,15 +842,21 @@ impl Compiler { let mut flags = self.enter_function(name, args)?; - let (new_body, doc_str) = get_doc(body); + let (body, doc_str) = get_doc(body); - self.compile_statements(new_body)?; + self.compile_statements(body)?; // Emit None at end: - self.emit(Instruction::LoadConst { - value: bytecode::Constant::None, - }); - self.emit(Instruction::ReturnValue); + match body.last().map(|s| &s.node) { + Some(ast::StatementType::Return { .. }) => {} + _ => { + self.emit(Instruction::LoadConst { + value: bytecode::Constant::None, + }); + self.emit(Instruction::ReturnValue); + } + } + let code = self.pop_code_object(); self.leave_scope();