From 0624b939459728f0c36ebc695df2b4a857bd32dd Mon Sep 17 00:00:00 2001 From: "Hikaru Terazono (3c1u)" <3c1u@vulpesgames.tokyo> Date: Sun, 15 Dec 2019 01:03:42 +0900 Subject: [PATCH] clippy --- src/codegen.rs | 15 +++++++++------ src/parser.rs | 5 ++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/codegen.rs b/src/codegen.rs index 8962eae..eb5ffb2 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -31,13 +31,17 @@ pub struct Codegen<'c> { extern "C" fn bfrs_get_char() -> u8 { let mut input = std::io::stdin(); let mut buf = [0u8]; - input.read(&mut buf).unwrap(); + + if input.read(&mut buf).unwrap() == 0 { + return 0xFF; // EOF + } + buf[0] } extern "C" fn bfrs_print_char(c: u8) { let mut out = std::io::stdout(); - out.write(&[c]).unwrap(); + out.write_all(&[c]).unwrap(); out.flush().unwrap(); } @@ -167,7 +171,7 @@ impl<'c> Codegen<'c> { match operation { BfAST::LoopBlock(v) => { // 特殊パターンの高速化 - if v.len() == 0 { + if v.is_empty() { return Ok(()); } else if v.len() == 1 { if let BfAST::SubOp(k) = v[0] { @@ -189,7 +193,7 @@ impl<'c> Codegen<'c> { return Ok(()); } } else if v.len() == 3 { - if let &[BfAST::AddPtr(j), BfAST::AddOp(k), BfAST::SubPtr(l)] = &v[0..3] { + if let [BfAST::AddPtr(j), BfAST::AddOp(k), BfAST::SubPtr(l)] = v[0..3] { if j == l { let rhs = self.get_current(value_table, counter); @@ -221,8 +225,7 @@ impl<'c> Codegen<'c> { return Ok(()); } - } else if let &[BfAST::SubPtr(j), BfAST::AddOp(k), BfAST::AddPtr(l)] = &v[0..3] - { + } else if let [BfAST::SubPtr(j), BfAST::AddOp(k), BfAST::AddPtr(l)] = v[0..3] { if j == l { let rhs = self.get_current(value_table, counter); diff --git a/src/parser.rs b/src/parser.rs index 5d5617b..c360d9f 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -138,11 +138,10 @@ fn visit_program(p: Pair<'_, Rule>, v: &mut Vec) -> Result<()> { } pub fn parse>(program: P) -> Result> { - let pairs = BfParser::parse(Rule::program, program.as_ref())?; + let mut pairs = BfParser::parse(Rule::program, program.as_ref())?; let program = pairs - .into_iter() .next() - .ok_or(Error::ice("no matching program"))?; + .ok_or_else(|| Error::ice("no matching program"))?; let mut program_out = vec![]; visit_program(program, &mut program_out)?;