From 1f0849c5b655f3c33e8613754267f9d1a2e2d1ce Mon Sep 17 00:00:00 2001 From: Benjamin Rogge Date: Mon, 7 Oct 2024 12:21:44 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8F=20feat:=20add=20clippy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit also fixed clippy suggestions --- .github/workflows/rust.yml | 2 ++ src/code/java/tokenizer/mod.rs | 37 ++++++++--------------- src/collections/push_back_iterator/mod.rs | 2 +- 3 files changed, 16 insertions(+), 25 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 9fd45e0..71c1c70 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -20,3 +20,5 @@ jobs: run: cargo build --verbose - name: Run tests run: cargo test --verbose + - name: Run clippy + run: cargo clippy --verbose diff --git a/src/code/java/tokenizer/mod.rs b/src/code/java/tokenizer/mod.rs index 5882c7c..49020c4 100644 --- a/src/code/java/tokenizer/mod.rs +++ b/src/code/java/tokenizer/mod.rs @@ -7,6 +7,8 @@ use crate::collections::push_back_iterator::PushBackIterator; #[cfg(test)] mod tests; +type TokenHandler = fn(&mut PushBackIterator>) -> Option; + const OPERATORS: [&str; 27] = [ "+", "/", "-", "=", "*", ">", "<", "|", "&", "++", "--", "==", "!=", "<=", ">=", "&&", "||", "+=", "-=", "*=", "/=", "%=", "&=", "|=", "^=", "!", "!=", @@ -17,7 +19,7 @@ pub fn tokenize(compilation_unit: &str) -> Vec { let mut tokens: Vec = Vec::new(); let mut iter = PushBackIterator::new(compilation_unit.chars().peekable()); - let handlers: Vec>) -> Option> = vec![ + let handlers: Vec = vec![ handle_whitespace, handle_alpha, handle_number, @@ -33,15 +35,15 @@ pub fn tokenize(compilation_unit: &str) -> Vec { while iter.peek().is_some() { for handler in &handlers { let token = handler(&mut iter); - if token.is_some() { - if token.as_ref().unwrap().essential { - tokens.push(token.unwrap()); + if let Some(value) = &token { + if value.essential { + tokens.push(value.clone()); } break; } } } - return tokens; + tokens } fn get_token_type(token: &str) -> TokenType { @@ -67,11 +69,7 @@ fn handle_whitespace(iter: &mut PushBackIterator>) -> Option>) -> Option { @@ -148,13 +146,10 @@ fn push_back_token(token: String, iter: &mut PushBackIterator fn handle_bracket(iter: &mut PushBackIterator>) -> Option { let mut token = String::new(); - while let Some(&ch) = iter.peek() { + if let Some(&ch) = iter.peek() { if ch == '{' || ch == '}' || ch == ')' || ch == '(' { token.push(ch); iter.next(); - break; - } else { - break; } } if !token.is_empty() { @@ -166,13 +161,10 @@ fn handle_bracket(iter: &mut PushBackIterator>) -> Option fn handle_comma(iter: &mut PushBackIterator>) -> Option { let mut token = String::new(); - while let Some(&ch) = iter.peek() { + if let Some(&ch) = iter.peek() { if ch == ',' { token.push(ch); iter.next(); - break; - } else { - break; } } if !token.is_empty() { @@ -184,13 +176,10 @@ fn handle_comma(iter: &mut PushBackIterator>) -> Option { fn handle_asterisks(iter: &mut PushBackIterator>) -> Option { let mut token = String::new(); - while let Some(&ch) = iter.peek() { + if let Some(&ch) = iter.peek() { if ch == '*' { token.push(ch); iter.next(); - break; - } else { - break; } } if !token.is_empty() { @@ -250,7 +239,7 @@ fn is_identifier(token: &str) -> bool { } /// Represents a token in a compilation unit -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Clone)] pub struct Token { pub token_type: TokenType, pub value: String, @@ -281,7 +270,7 @@ impl fmt::Display for Token { } /// Represents the type of a token -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Clone)] pub enum TokenType { Identifier, Number, diff --git a/src/collections/push_back_iterator/mod.rs b/src/collections/push_back_iterator/mod.rs index bc2830e..e3f05bf 100644 --- a/src/collections/push_back_iterator/mod.rs +++ b/src/collections/push_back_iterator/mod.rs @@ -31,7 +31,7 @@ where if self.pushed_back.is_empty() { self.iter.peek() } else { - Some(&self.pushed_back.last().unwrap()) + Some(self.pushed_back.last().unwrap()) } } }