Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ran cargo clippy --fix on rust implementation to clean up the code a bit #623

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
266 changes: 133 additions & 133 deletions impls/rust/Cargo.lock

Large diffs are not rendered by default.

15 changes: 7 additions & 8 deletions impls/rust/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ fn keys(a: MalArgs) -> MalRet {

fn vals(a: MalArgs) -> MalRet {
match a[0] {
Hash(ref hm, _) => Ok(list!(hm.values().map(|v| { v.clone() }).collect())),
Hash(ref hm, _) => Ok(list!(hm.values().cloned().collect())),
_ => error("keys requires Hash Map"),
}
}
Expand Down Expand Up @@ -212,7 +212,7 @@ fn apply(a: MalArgs) -> MalRet {
List(ref v, _) | Vector(ref v, _) => {
let f = &a[0];
let mut fargs = a[1..a.len() - 1].to_vec();
fargs.extend_from_slice(&v);
fargs.extend_from_slice(v);
f.apply(fargs)
}
_ => error("apply called with non-seq"),
Expand All @@ -237,8 +237,7 @@ fn conj(a: MalArgs) -> MalRet {
List(ref v, _) => {
let sl = a[1..]
.iter()
.rev()
.map(|a| a.clone())
.rev().cloned()
.collect::<Vec<MalVal>>();
Ok(list!([&sl[..], v].concat()))
}
Expand All @@ -251,7 +250,7 @@ fn seq(a: MalArgs) -> MalRet {
match a[0] {
List(ref v, _) | Vector(ref v, _) if v.len() == 0 => Ok(Nil),
List(ref v, _) | Vector(ref v, _) => Ok(list!(v.to_vec())),
Str(ref s) if s.len() == 0 => Ok(Nil),
Str(ref s) if s.is_empty() => Ok(Nil),
Str(ref s) if !a[0].keyword_q() => {
Ok(list!(s.chars().map(|c| { Str(c.to_string()) }).collect()))
}
Expand All @@ -271,12 +270,12 @@ pub fn ns() -> Vec<(&'static str, MalVal)> {
("symbol?", func(fn_is_type!(Sym(_)))),
(
"string?",
func(fn_is_type!(Str(ref s) if !s.starts_with("\u{29e}"))),
func(fn_is_type!(Str(ref s) if !s.starts_with('\u{29e}'))),
),
("keyword", func(|a| a[0].keyword())),
(
"keyword?",
func(fn_is_type!(Str(ref s) if s.starts_with("\u{29e}"))),
func(fn_is_type!(Str(ref s) if s.starts_with('\u{29e}'))),
),
("number?", func(fn_is_type!(Int(_)))),
(
Expand Down Expand Up @@ -320,7 +319,7 @@ pub fn ns() -> Vec<(&'static str, MalVal)> {
("list?", func(fn_is_type!(List(_, _)))),
("vector", func(|a| Ok(vector!(a)))),
("vector?", func(fn_is_type!(Vector(_, _)))),
("hash-map", func(|a| hash_map(a))),
("hash-map", func(hash_map)),
("map?", func(fn_is_type!(Hash(_, _)))),
("assoc", func(assoc)),
("dissoc", func(dissoc)),
Expand Down
2 changes: 1 addition & 1 deletion impls/rust/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub type Env = Rc<EnvStruct>;
pub fn env_new(outer: Option<Env>) -> Env {
Rc::new(EnvStruct {
data: RefCell::new(FnvHashMap::default()),
outer: outer,
outer,
})
}

Expand Down
6 changes: 3 additions & 3 deletions impls/rust/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl MalVal {
Int(i) => format!("{}", i),
//Float(f) => format!("{}", f),
Str(s) => {
if s.starts_with("\u{29e}") {
if s.starts_with('\u{29e}') {
format!(":{}", &s[2..])
} else if print_readably {
format!("\"{}\"", escape_str(s))
Expand All @@ -31,8 +31,8 @@ impl MalVal {
}
}
Sym(s) => s.clone(),
List(l, _) => pr_seq(&**l, print_readably, "(", ")", " "),
Vector(l, _) => pr_seq(&**l, print_readably, "[", "]", " "),
List(l, _) => pr_seq(l, print_readably, "(", ")", " "),
Vector(l, _) => pr_seq(l, print_readably, "[", "]", " "),
Hash(hm, _) => {
let l: Vec<MalVal> = hm
.iter()
Expand Down
16 changes: 8 additions & 8 deletions impls/rust/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct Reader {

impl Reader {
fn next(&mut self) -> Result<String, MalErr> {
self.pos = self.pos + 1;
self.pos += 1;
Ok(self
.tokens
.get(self.pos - 1)
Expand All @@ -39,7 +39,7 @@ fn tokenize(str: &str) -> Vec<String> {

let mut res = vec![];
for cap in RE.captures_iter(str) {
if cap[1].starts_with(";") {
if cap[1].starts_with(';') {
continue;
}
res.push(String::from(&cap[1]));
Expand All @@ -51,8 +51,8 @@ fn unescape_str(s: &str) -> String {
lazy_static! {
static ref RE: Regex = Regex::new(r#"\\(.)"#).unwrap();
}
RE.replace_all(&s, |caps: &Captures| {
format!("{}", if &caps[1] == "n" { "\n" } else { &caps[1] })
RE.replace_all(s, |caps: &Captures| {
(if &caps[1] == "n" { "\n" } else { &caps[1] }).to_string()
})
.to_string()
}
Expand All @@ -72,9 +72,9 @@ fn read_atom(rdr: &mut Reader) -> MalRet {
Ok(Int(token.parse().unwrap()))
} else if STR_RE.is_match(&token) {
Ok(Str(unescape_str(&token[1..token.len() - 1])))
} else if token.starts_with("\"") {
} else if token.starts_with('\"') {
error("expected '\"', got EOF")
} else if token.starts_with(":") {
} else if token.starts_with(':') {
Ok(Str(format!("\u{29e}{}", &token[1..])))
} else {
Ok(Sym(token.to_string()))
Expand Down Expand Up @@ -146,11 +146,11 @@ fn read_form(rdr: &mut Reader) -> MalRet {
pub fn read_str(str: String) -> MalRet {
let tokens = tokenize(&str);
//println!("tokens: {:?}", tokens);
if tokens.len() == 0 {
if tokens.is_empty() {
return error("no input");
}
read_form(&mut Reader {
pos: 0,
tokens: tokens,
tokens,
})
}
2 changes: 1 addition & 1 deletion impls/rust/step0_repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() {
Ok(line) => {
rl.add_history_entry(&line);
rl.save_history(".mal-history").unwrap();
if line.len() > 0 {
if !line.is_empty() {
println!("{}", line);
}
}
Expand Down
2 changes: 1 addition & 1 deletion impls/rust/step1_read_print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn main() {
Ok(line) => {
rl.add_history_entry(&line);
rl.save_history(".mal-history").unwrap();
if line.len() > 0 {
if !line.is_empty() {
match reader::read_str(line) {
Ok(mv) => {
println!("{}", mv.pr_str(true));
Expand Down
4 changes: 2 additions & 2 deletions impls/rust/step2_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn eval(ast: MalVal, env: Env) -> MalRet {
}
match eval_ast(&ast, &env)? {
List(ref el, _) => {
let ref f = el[0].clone();
let f = &el[0].clone();
f.apply(el[1..].to_vec())
}
_ => error("expected a list"),
Expand Down Expand Up @@ -118,7 +118,7 @@ fn main() {
Ok(line) => {
rl.add_history_entry(&line);
rl.save_history(".mal-history").unwrap();
if line.len() > 0 {
if !line.is_empty() {
match rep(&line, &repl_env) {
Ok(out) => println!("{}", out),
Err(e) => println!("Error: {}", format_error(e)),
Expand Down
8 changes: 4 additions & 4 deletions impls/rust/step3_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn read(str: &str) -> MalRet {
// eval
fn eval_ast(ast: &MalVal, env: &Env) -> MalRet {
match ast {
Sym(_) => Ok(env_get(&env, &ast)?),
Sym(_) => Ok(env_get(env, ast)?),
List(v, _) => {
let mut lst: MalArgs = vec![];
for a in v.iter() {
Expand Down Expand Up @@ -69,7 +69,7 @@ fn eval(ast: MalVal, env: Env) -> MalRet {
env_set(&env, l[1].clone(), eval(l[2].clone(), env.clone())?)
}
Sym(ref a0sym) if a0sym == "let*" => {
let let_env = env_new(Some(env.clone()));
let let_env = env_new(Some(env));
let (a1, a2) = (l[1].clone(), l[2].clone());
match a1 {
List(ref binds, _) | Vector(ref binds, _) => {
Expand All @@ -96,7 +96,7 @@ fn eval(ast: MalVal, env: Env) -> MalRet {
}
_ => match eval_ast(&ast, &env)? {
List(ref el, _) => {
let ref f = el[0].clone();
let f = &el[0].clone();
f.apply(el[1..].to_vec())
}
_ => error("expected a list"),
Expand Down Expand Up @@ -144,7 +144,7 @@ fn main() {
Ok(line) => {
rl.add_history_entry(&line);
rl.save_history(".mal-history").unwrap();
if line.len() > 0 {
if !line.is_empty() {
match rep(&line, &repl_env) {
Ok(out) => println!("{}", out),
Err(e) => println!("Error: {}", format_error(e)),
Expand Down
16 changes: 8 additions & 8 deletions impls/rust/step4_if_fn_do.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn read(str: &str) -> MalRet {
// eval
fn eval_ast(ast: &MalVal, env: &Env) -> MalRet {
match ast {
Sym(_) => Ok(env_get(&env, &ast)?),
Sym(_) => Ok(env_get(env, ast)?),
List(v, _) => {
let mut lst: MalArgs = vec![];
for a in v.iter() {
Expand Down Expand Up @@ -70,7 +70,7 @@ fn eval(ast: MalVal, env: Env) -> MalRet {
env_set(&env, l[1].clone(), eval(l[2].clone(), env.clone())?)
}
Sym(ref a0sym) if a0sym == "let*" => {
let let_env = env_new(Some(env.clone()));
let let_env = env_new(Some(env));
let (a1, a2) = (l[1].clone(), l[2].clone());
match a1 {
List(ref binds, _) | Vector(ref binds, _) => {
Expand Down Expand Up @@ -102,26 +102,26 @@ fn eval(ast: MalVal, env: Env) -> MalRet {
Sym(ref a0sym) if a0sym == "if" => {
let cond = eval(l[1].clone(), env.clone())?;
match cond {
Bool(false) | Nil if l.len() >= 4 => eval(l[3].clone(), env.clone()),
Bool(false) | Nil if l.len() >= 4 => eval(l[3].clone(), env),
Bool(false) | Nil => Ok(Nil),
_ if l.len() >= 3 => eval(l[2].clone(), env.clone()),
_ if l.len() >= 3 => eval(l[2].clone(), env),
_ => Ok(Nil),
}
}
Sym(ref a0sym) if a0sym == "fn*" => {
let (a1, a2) = (l[1].clone(), l[2].clone());
Ok(MalFunc {
eval: eval,
eval,
ast: Rc::new(a2),
env: env,
env,
params: Rc::new(a1),
is_macro: false,
meta: Rc::new(Nil),
})
}
_ => match eval_ast(&ast, &env)? {
List(ref el, _) => {
let ref f = el[0].clone();
let f = &el[0].clone();
f.apply(el[1..].to_vec())
}
_ => error("expected a list"),
Expand Down Expand Up @@ -166,7 +166,7 @@ fn main() {
Ok(line) => {
rl.add_history_entry(&line);
rl.save_history(".mal-history").unwrap();
if line.len() > 0 {
if !line.is_empty() {
match rep(&line, &repl_env) {
Ok(out) => println!("{}", out),
Err(e) => println!("Error: {}", format_error(e)),
Expand Down
10 changes: 5 additions & 5 deletions impls/rust/step5_tco.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn read(str: &str) -> MalRet {
// eval
fn eval_ast(ast: &MalVal, env: &Env) -> MalRet {
match ast {
Sym(_) => Ok(env_get(&env, &ast)?),
Sym(_) => Ok(env_get(env, ast)?),
List(v, _) => {
let mut lst: MalArgs = vec![];
for a in v.iter() {
Expand Down Expand Up @@ -126,17 +126,17 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
Sym(ref a0sym) if a0sym == "fn*" => {
let (a1, a2) = (l[1].clone(), l[2].clone());
Ok(MalFunc {
eval: eval,
eval,
ast: Rc::new(a2),
env: env,
env,
params: Rc::new(a1),
is_macro: false,
meta: Rc::new(Nil),
})
}
_ => match eval_ast(&ast, &env)? {
List(ref el, _) => {
let ref f = el[0].clone();
let f = &el[0].clone();
let args = el[1..].to_vec();
match f {
Func(_, _) => f.apply(args),
Expand Down Expand Up @@ -202,7 +202,7 @@ fn main() {
Ok(line) => {
rl.add_history_entry(&line);
rl.save_history(".mal-history").unwrap();
if line.len() > 0 {
if !line.is_empty() {
match rep(&line, &repl_env) {
Ok(out) => println!("{}", out),
Err(e) => println!("Error: {}", format_error(e)),
Expand Down
10 changes: 5 additions & 5 deletions impls/rust/step6_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn read(str: &str) -> MalRet {
// eval
fn eval_ast(ast: &MalVal, env: &Env) -> MalRet {
match ast {
Sym(_) => Ok(env_get(&env, &ast)?),
Sym(_) => Ok(env_get(env, ast)?),
List(v, _) => {
let mut lst: MalArgs = vec![];
for a in v.iter() {
Expand Down Expand Up @@ -126,9 +126,9 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
Sym(ref a0sym) if a0sym == "fn*" => {
let (a1, a2) = (l[1].clone(), l[2].clone());
Ok(MalFunc {
eval: eval,
eval,
ast: Rc::new(a2),
env: env,
env,
params: Rc::new(a1),
is_macro: false,
meta: Rc::new(Nil),
Expand All @@ -143,7 +143,7 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
}
_ => match eval_ast(&ast, &env)? {
List(ref el, _) => {
let ref f = el[0].clone();
let f = &el[0].clone();
let args = el[1..].to_vec();
match f {
Func(_, _) => f.apply(args),
Expand Down Expand Up @@ -228,7 +228,7 @@ fn main() {
Ok(line) => {
rl.add_history_entry(&line);
rl.save_history(".mal-history").unwrap();
if line.len() > 0 {
if !line.is_empty() {
match rep(&line, &repl_env) {
Ok(out) => println!("{}", out),
Err(e) => println!("Error: {}", format_error(e)),
Expand Down