Skip to content

Commit 46de6a0

Browse files
committed
Fixed issues reported by clippy
1 parent e3658e0 commit 46de6a0

File tree

5 files changed

+64
-80
lines changed

5 files changed

+64
-80
lines changed

src/interpreter.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::io;
2-
use std::io::Error as IoError;
32

43
#[derive(Debug, Copy, Clone, PartialEq)]
54
pub enum Instruction {
@@ -16,14 +15,14 @@ pub enum Instruction {
1615

1716
#[derive(Debug)]
1817
pub enum Error {
19-
Io(IoError),
18+
Io(io::Error),
2019
PointerOverflow,
2120
PointerUnderflow,
2221
}
2322

24-
impl From<IoError> for Error {
25-
fn from(io_error: IoError) -> Self {
26-
Error::Io(io_error)
23+
impl From<io::Error> for Error {
24+
fn from(error: io::Error) -> Self {
25+
Error::Io(error)
2726
}
2827
}
2928

@@ -33,6 +32,7 @@ pub struct Interpreter {
3332
}
3433

3534
impl Interpreter {
35+
#[must_use]
3636
pub fn new(memory_size: usize) -> Self {
3737
Interpreter {
3838
memory: vec![0; memory_size],
@@ -45,12 +45,12 @@ impl Interpreter {
4545
}
4646

4747
fn write_current_cell(&mut self, value: u8) {
48-
self.memory[self.pointer] = value
48+
self.memory[self.pointer] = value;
4949
}
5050

5151
pub fn run(
5252
&mut self,
53-
program: Vec<Instruction>,
53+
program: &[Instruction],
5454
input: &mut dyn io::Read,
5555
output: &mut dyn io::Write,
5656
) -> Result<(), Error> {

src/main.rs

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
1-
use std::env;
2-
use std::fs;
3-
use std::io;
4-
use std::time::Instant;
5-
6-
use brainrust::interpreter;
7-
use brainrust::interpreter::Interpreter;
8-
use brainrust::lexer;
9-
use brainrust::optimizer;
10-
use brainrust::parser;
11-
use clap::Arg;
12-
use clap::ArgAction;
13-
use clap::ArgMatches;
14-
use clap::Command;
15-
use clap::crate_name;
16-
use clap::crate_version;
17-
use clap::value_parser;
1+
use brainrust::{
2+
interpreter::{self, Interpreter},
3+
lexer, optimizer, parser,
4+
};
5+
use clap::{Arg, ArgAction, ArgMatches, Command, crate_name, crate_version, value_parser};
6+
use std::{env, fs, io, time::Instant};
187

198
const DEFAULT_MEMORY_SIZE: usize = 32768;
209
const CLI_SUB_CMD_RUN: &str = "run";
@@ -66,16 +55,15 @@ fn main() -> Result<(), CliError> {
6655
)
6756
.arg(
6857
Arg::new(CLI_SUB_CMD_RUN_MEMORY)
69-
.help(&format!(
70-
"Sets the number of memory cells, defaults to {:?}",
71-
DEFAULT_MEMORY_SIZE
58+
.help(format!(
59+
"Sets the number of memory cells, defaults to {DEFAULT_MEMORY_SIZE:?}"
7260
))
7361
.long(CLI_SUB_CMD_RUN_MEMORY)
7462
.value_parser(value_parser!(u32)),
7563
)
7664
.arg(
7765
Arg::new(CLI_SUB_CMD_RUN_TIME)
78-
.help(&"Prints time of various metrics".to_string())
66+
.help("Prints time of various metrics")
7967
.long(CLI_SUB_CMD_RUN_TIME)
8068
.value_parser(CLI_SUB_CMD_RUN_TIME_OPTIONS)
8169
.action(ArgAction::Append),
@@ -107,14 +95,14 @@ fn handle_run(matches: &ArgMatches) -> Result<(), CliError> {
10795
let total_start = Instant::now();
10896
let contents = fs::read_to_string(input)?;
10997
let tokens = lexer::lex(&contents);
110-
let parsed = parser::parse(tokens)?;
98+
let parsed = parser::parse(&tokens)?;
11199
let optimized = optimizer::optimize(parsed);
112100
let parse_elapsed = total_start.elapsed();
113101

114102
let mut interpreter = Interpreter::new(memory);
115103

116104
let exec_start = Instant::now();
117-
interpreter.run(optimized, &mut io::stdin(), &mut io::stdout())?;
105+
interpreter.run(&optimized, &mut io::stdin(), &mut io::stdout())?;
118106
let exec_elapsed = exec_start.elapsed();
119107
let total_elapsed = total_start.elapsed();
120108

src/optimizer.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
use std::ops::RangeInclusive;
2-
1+
use crate::{
2+
interpreter::Instruction::{
3+
self, Add, Clear, JumpIfNotZero, JumpIfZero, MoveLeft, MoveRight, Sub,
4+
},
5+
parser,
6+
};
37
use itertools::Itertools;
8+
use std::ops::RangeInclusive;
49

5-
use crate::interpreter::Instruction;
6-
use crate::interpreter::Instruction::*;
7-
use crate::parser;
8-
10+
#[must_use]
911
pub fn optimize(instructions: Vec<Instruction>) -> Vec<Instruction> {
1012
let iter = instructions.into_iter();
1113
let iter = combine_instructions(iter);
1214
let iter = optimize_clear_loop(iter);
13-
let mut optimized = iter.collect();
15+
let mut optimized: Vec<_> = iter.collect();
1416
parser::link_loops(&mut optimized).unwrap()
1517
}
1618

src/parser.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use crate::interpreter::Instruction;
2-
use crate::lexer::Command;
1+
use crate::{interpreter::Instruction, lexer::Command};
32

4-
pub fn parse(commands: Vec<Command>) -> Result<Vec<Instruction>, Error> {
3+
pub fn parse(commands: &[Command]) -> Result<Vec<Instruction>, Error> {
54
let mut instructions: Vec<Instruction> = commands
65
.iter()
76
.map(|cmd| match cmd {
@@ -19,7 +18,7 @@ pub fn parse(commands: Vec<Command>) -> Result<Vec<Instruction>, Error> {
1918
link_loops(&mut instructions)
2019
}
2120

22-
pub fn link_loops(program: &mut Vec<Instruction>) -> Result<Vec<Instruction>, Error> {
21+
pub fn link_loops(program: &mut [Instruction]) -> Result<Vec<Instruction>, Error> {
2322
let mut jump_stack = Vec::new();
2423

2524
for i in 0..program.len() {
@@ -78,7 +77,7 @@ mod tests {
7877
Instruction::Print,
7978
Instruction::Read,
8079
];
81-
assert_eq!(parse(input).unwrap(), expected);
80+
assert_eq!(parse(&input).unwrap(), expected);
8281
}
8382

8483
#[test]
@@ -103,7 +102,7 @@ mod tests {
103102
Instruction::Sub(1),
104103
Instruction::Read,
105104
];
106-
assert_eq!(parse(input).unwrap(), expected);
105+
assert_eq!(parse(&input).unwrap(), expected);
107106
}
108107

109108
#[test]
@@ -130,18 +129,18 @@ mod tests {
130129
Instruction::JumpIfNotZero(1),
131130
Instruction::JumpIfNotZero(0),
132131
];
133-
assert_eq!(parse(input).unwrap(), expected);
132+
assert_eq!(parse(&input).unwrap(), expected);
134133
}
135134

136135
#[test]
137136
fn test_missing_closing_bracket() {
138137
let input = vec![Command::JumpIfZero, Command::Add];
139-
assert!(parse(input).is_err());
138+
assert!(parse(&input).is_err());
140139
}
141140

142141
#[test]
143142
fn test_missing_opening_bracket() {
144143
let input = vec![Command::Add, Command::JumpIfNotZero];
145-
assert!(parse(input).is_err());
144+
assert!(parse(&input).is_err());
146145
}
147146
}

tests/integration_test.rs

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
use brainrust::interpreter;
2-
use brainrust::interpreter::Interpreter;
3-
use brainrust::lexer;
4-
use brainrust::optimizer;
5-
use brainrust::parser;
6-
7-
use std::fs;
8-
use std::str;
1+
use brainrust::{
2+
interpreter::{self, Interpreter},
3+
lexer, optimizer, parser,
4+
};
5+
use std::{fs, io, str};
96

107
const PROGRAM_PREFIX: &str = "tests/programs/";
118
const PROGRAM_EXTENSION: &str = ".b";
@@ -19,18 +16,16 @@ macro_rules! test_programs {
1916
$(
2017
#[test]
2118
fn $test_name() -> Result<(), TestError> {
22-
let program = format!("{}{}{}", PROGRAM_PREFIX, $program_name, PROGRAM_EXTENSION);
23-
let input = format!("{}{}{}", PROGRAM_PREFIX, $program_name, INPUT_EXTENSION);
24-
let output = format!("{}{}{}", PROGRAM_PREFIX, $program_name, OUTPUT_EXTENSION);
19+
let program_name = $program_name;
20+
let program = format!("{PROGRAM_PREFIX}{program_name}{PROGRAM_EXTENSION}");
21+
let input = format!("{PROGRAM_PREFIX}{program_name}{INPUT_EXTENSION}");
22+
let output = format!("{PROGRAM_PREFIX}{program_name}{OUTPUT_EXTENSION}");
2523

26-
let program = fs::read(program)?;
27-
let input = fs::read(input)?;
24+
let program = fs::read_to_string(program)?;
25+
let input = fs::read_to_string(input)?;
2826
let output = fs::read(output)?;
2927

30-
let program = str::from_utf8(&program)?;
31-
let input = str::from_utf8(&input)?;
32-
33-
let result = run_program(program, input)?;
28+
let result = run_program(&program, &input)?;
3429

3530
assert_eq!(result, output);
3631
Ok(())
@@ -44,45 +39,45 @@ test_programs! {
4439
}
4540

4641
fn run_program(file: &str, input: &str) -> Result<Vec<u8>, TestError> {
47-
let tokens = lexer::lex(&file);
48-
let parsed = parser::parse(tokens)?;
42+
let tokens = lexer::lex(file);
43+
let parsed = parser::parse(&tokens)?;
4944
let optimized = optimizer::optimize(parsed);
5045
let mut interpreter = Interpreter::new(MEMORY_SIZE);
5146

5247
let mut output: Vec<u8> = vec![];
5348

54-
interpreter.run(optimized, &mut input.as_bytes(), &mut output)?;
49+
interpreter.run(&optimized, &mut input.as_bytes(), &mut output)?;
5550
Ok(output)
5651
}
5752

5853
#[derive(Debug)]
5954
enum TestError {
60-
Io(std::io::Error),
61-
Parsing(parser::Error),
62-
Interpreter(interpreter::Error),
63-
ConversationError(std::str::Utf8Error),
55+
Io,
56+
Parsing,
57+
Interpreter,
58+
ConversationError,
6459
}
6560

66-
impl From<std::io::Error> for TestError {
67-
fn from(io_error: std::io::Error) -> Self {
68-
TestError::Io(io_error)
61+
impl From<io::Error> for TestError {
62+
fn from(_error: io::Error) -> Self {
63+
TestError::Io
6964
}
7065
}
7166

7267
impl From<parser::Error> for TestError {
73-
fn from(parser_error: parser::Error) -> Self {
74-
TestError::Parsing(parser_error)
68+
fn from(_error: parser::Error) -> Self {
69+
TestError::Parsing
7570
}
7671
}
7772

7873
impl From<interpreter::Error> for TestError {
79-
fn from(interpreter_error: interpreter::Error) -> Self {
80-
TestError::Interpreter(interpreter_error)
74+
fn from(_error: interpreter::Error) -> Self {
75+
TestError::Interpreter
8176
}
8277
}
8378

84-
impl From<std::str::Utf8Error> for TestError {
85-
fn from(utf8_error: std::str::Utf8Error) -> Self {
86-
TestError::ConversationError(utf8_error)
79+
impl From<str::Utf8Error> for TestError {
80+
fn from(_error: str::Utf8Error) -> Self {
81+
TestError::ConversationError
8782
}
8883
}

0 commit comments

Comments
 (0)