applied clippy

This commit is contained in:
pca006132 2021-01-04 12:07:10 +08:00
parent fa2fc1db54
commit 2b2e220723
7 changed files with 55 additions and 52 deletions

View File

@ -70,7 +70,7 @@ impl<'a> InferenceContext<'a> {
self.stack.level += 1; self.stack.level += 1;
let result = f(self); let result = f(self);
self.stack.level -= 1; self.stack.level -= 1;
while self.stack.var_defs.len() > 0 { while !self.stack.var_defs.is_empty() {
let (_, _, level) = self.stack.var_defs.last().unwrap(); let (_, _, level) = self.stack.var_defs.last().unwrap();
if *level > self.stack.level { if *level > self.stack.level {
let (id, def, _) = self.stack.var_defs.pop().unwrap(); let (id, def, _) = self.stack.var_defs.pop().unwrap();
@ -80,7 +80,7 @@ impl<'a> InferenceContext<'a> {
} }
} }
let mut poped_names = Vec::new(); let mut poped_names = Vec::new();
while self.stack.sym_def.len() > 0 { while !self.stack.sym_def.is_empty() {
let (_, level) = self.stack.sym_def.last().unwrap(); let (_, level) = self.stack.sym_def.last().unwrap();
if *level > self.stack.level { if *level > self.stack.level {
let (name, _) = self.stack.sym_def.pop().unwrap(); let (name, _) = self.stack.sym_def.pop().unwrap();
@ -196,7 +196,7 @@ impl TypeEnum {
*id, *id,
params params
.iter() .iter()
.map(|v| v.as_ref().inv_subst(map).into()) .map(|v| v.as_ref().inv_subst(map))
.collect(), .collect(),
), ),
_ => self.clone(), _ => self.clone(),

View File

@ -31,7 +31,7 @@ impl<'a> TopLevelContext<'a> {
primitives.push(TypeEnum::PrimitiveType(PrimitiveId(i)).into()); primitives.push(TypeEnum::PrimitiveType(PrimitiveId(i)).into());
sym_table.insert(t.name, TypeEnum::PrimitiveType(PrimitiveId(i)).into()); sym_table.insert(t.name, TypeEnum::PrimitiveType(PrimitiveId(i)).into());
} }
return TopLevelContext { TopLevelContext {
primitive_defs, primitive_defs,
class_defs: Vec::new(), class_defs: Vec::new(),
parametric_defs: Vec::new(), parametric_defs: Vec::new(),
@ -40,7 +40,7 @@ impl<'a> TopLevelContext<'a> {
sym_table, sym_table,
primitives, primitives,
variables: Vec::new(), variables: Vec::new(),
}; }
} }
pub fn add_class(&mut self, def: ClassDef<'a>) -> ClassId { pub fn add_class(&mut self, def: ClassDef<'a>) -> ClassId {
@ -131,6 +131,6 @@ impl<'a> TopLevelContext<'a> {
pub fn get_type(&self, name: &str) -> Option<Type> { pub fn get_type(&self, name: &str) -> Option<Type> {
// TODO: handle parametric types // TODO: handle parametric types
self.sym_table.get(name).map(|v| v.clone()) self.sym_table.get(name).cloned()
} }
} }

View File

@ -27,7 +27,7 @@ pub fn infer_expr<'b: 'a, 'a>(ctx: &mut InferenceContext<'a>, expr: &'b Expressi
function, function,
keywords, keywords,
} => { } => {
if keywords.len() > 0 { if keywords.is_empty() {
Err("keyword is not supported".into()) Err("keyword is not supported".into())
} else { } else {
infer_call(ctx, &args, &function) infer_call(ctx, &args, &function)
@ -81,7 +81,7 @@ fn infer_identifier(ctx: &mut InferenceContext, name: &str) -> ParserResult {
} }
fn infer_list<'b: 'a, 'a>(ctx: &mut InferenceContext<'a>, elements: &'b [Expression]) -> ParserResult { fn infer_list<'b: 'a, 'a>(ctx: &mut InferenceContext<'a>, elements: &'b [Expression]) -> ParserResult {
if elements.len() == 0 { if elements.is_empty() {
return Ok(Some(ParametricType(LIST_TYPE, vec![BotType.into()]).into())); return Ok(Some(ParametricType(LIST_TYPE, vec![BotType.into()]).into()));
} }
@ -119,7 +119,7 @@ fn infer_attribute<'b: 'a, 'a>(
let value = infer_expr(ctx, value)?.ok_or("no value".to_string())?; let value = infer_expr(ctx, value)?.ok_or("no value".to_string())?;
if let TypeVariable(id) = value.as_ref() { if let TypeVariable(id) = value.as_ref() {
let v = ctx.get_variable_def(*id); let v = ctx.get_variable_def(*id);
if v.bound.len() == 0 { if v.bound.is_empty() {
return Err("no fields on unbounded type variable".into()); return Err("no fields on unbounded type variable".into());
} }
let ty = v.bound[0] let ty = v.bound[0]
@ -156,7 +156,7 @@ fn infer_bool_ops<'b: 'a, 'a>(
let b = ctx.get_primitive(BOOL_TYPE); let b = ctx.get_primitive(BOOL_TYPE);
if left == b && right == b { if left == b && right == b {
Ok(Some(b.into())) Ok(Some(b))
} else { } else {
Err("bool operands must be bool".into()) Err("bool operands must be bool".into())
} }
@ -214,7 +214,7 @@ fn infer_compare<'b: 'a, 'a>(
return Err("comparison result must be boolean".into()); return Err("comparison result must be boolean".into());
} }
} }
Ok(Some(boolean.into())) Ok(Some(boolean))
} }
fn infer_call<'b: 'a, 'a>( fn infer_call<'b: 'a, 'a>(

View File

@ -38,8 +38,8 @@ fn find_subst(
} }
let v_a = ctx.get_variable_def(*id_a); let v_a = ctx.get_variable_def(*id_a);
let v_b = ctx.get_variable_def(*id_b); let v_b = ctx.get_variable_def(*id_b);
if v_b.bound.len() > 0 { if !v_b.bound.is_empty() {
if v_a.bound.len() == 0 { if v_a.bound.is_empty() {
return Err("unbounded a".to_string()); return Err("unbounded a".to_string());
} else { } else {
let diff: Vec<_> = v_a let diff: Vec<_> = v_a
@ -47,12 +47,12 @@ fn find_subst(
.iter() .iter()
.filter(|x| !v_b.bound.contains(x)) .filter(|x| !v_b.bound.contains(x))
.collect(); .collect();
if diff.len() > 0 { if diff.is_empty() {
return Err("different domain".to_string()); return Err("different domain".to_string());
} }
} }
} }
sub.insert(*id_b, a.clone().into()); sub.insert(*id_b, a.clone());
Ok(()) Ok(())
} }
(TypeVariable(id_a), _) => { (TypeVariable(id_a), _) => {
@ -65,8 +65,8 @@ fn find_subst(
} }
(_, TypeVariable(id_b)) => { (_, TypeVariable(id_b)) => {
let v_b = ctx.get_variable_def(*id_b); let v_b = ctx.get_variable_def(*id_b);
if v_b.bound.len() == 0 || v_b.bound.contains(&a) { if v_b.bound.is_empty() || v_b.bound.contains(&a) {
sub.insert(*id_b, a.clone().into()); sub.insert(*id_b, a.clone());
Ok(()) Ok(())
} else { } else {
Err("different domain".to_string()) Err("different domain".to_string())
@ -124,14 +124,14 @@ fn resolve_call_rec(
let mut subst = obj let mut subst = obj
.as_ref() .as_ref()
.map(|v| v.get_subst(ctx)) .map(|v| v.get_subst(ctx))
.unwrap_or(HashMap::new()); .unwrap_or_else(HashMap::new);
let fun = match &obj { let fun = match &obj {
Some(obj) => { Some(obj) => {
let base = match obj.as_ref() { let base = match obj.as_ref() {
TypeVariable(id) => { TypeVariable(id) => {
let v = ctx.get_variable_def(*id); let v = ctx.get_variable_def(*id);
if v.bound.len() == 0 { if v.bound.is_empty() {
return Err("unbounded type var".to_string()); return Err("unbounded type var".to_string());
} }
let results: Result<Vec<_>, String> = v let results: Result<Vec<_>, String> = v
@ -153,7 +153,7 @@ fn resolve_call_rec(
} }
let mut results = results.iter().zip(v.bound.iter()).map(|(r, ins)| { let mut results = results.iter().zip(v.bound.iter()).map(|(r, ins)| {
r.as_ref() r.as_ref()
.map(|v| v.inv_subst(&[(ins.clone(), obj.clone().into())])) .map(|v| v.inv_subst(&[(ins.clone(), obj.clone())]))
}); });
let first = results.next().unwrap(); let first = results.next().unwrap();
if results.all(|v| v == first) { if results.all(|v| v == first) {
@ -171,7 +171,7 @@ fn resolve_call_rec(
} }
None => ctx.get_fn_def(func), None => ctx.get_fn_def(func),
} }
.ok_or("no such function".to_string())?; .ok_or_else(|| "no such function".to_string())?;
if args.len() != fun.args.len() { if args.len() != fun.args.len() {
return Err("incorrect parameter number".to_string()); return Err("incorrect parameter number".to_string());
@ -233,17 +233,17 @@ mod tests {
assert_eq!( assert_eq!(
resolve_call(&ctx, None, "int32", &[ctx.get_primitive(FLOAT_TYPE)]), resolve_call(&ctx, None, "int32", &[ctx.get_primitive(FLOAT_TYPE)]),
Ok(Some(ctx.get_primitive(INT32_TYPE).into())) Ok(Some(ctx.get_primitive(INT32_TYPE)))
); );
assert_eq!( assert_eq!(
resolve_call(&ctx, None, "int32", &[ctx.get_primitive(INT32_TYPE)],), resolve_call(&ctx, None, "int32", &[ctx.get_primitive(INT32_TYPE)],),
Ok(Some(ctx.get_primitive(INT32_TYPE).into())) Ok(Some(ctx.get_primitive(INT32_TYPE)))
); );
assert_eq!( assert_eq!(
resolve_call(&ctx, None, "float", &[ctx.get_primitive(INT32_TYPE)]), resolve_call(&ctx, None, "float", &[ctx.get_primitive(INT32_TYPE)]),
Ok(Some(ctx.get_primitive(FLOAT_TYPE).into())) Ok(Some(ctx.get_primitive(FLOAT_TYPE)))
); );
assert_eq!( assert_eq!(
@ -281,25 +281,25 @@ mod tests {
let v1 = ctx.add_variable(VarDef { let v1 = ctx.add_variable(VarDef {
name: "V1", name: "V1",
bound: vec![ bound: vec![
ctx.get_primitive(INT32_TYPE).into(), ctx.get_primitive(INT32_TYPE),
ctx.get_primitive(FLOAT_TYPE).into(), ctx.get_primitive(FLOAT_TYPE),
], ],
}); });
let v1 = ctx.get_variable(v1); let v1 = ctx.get_variable(v1);
let v2 = ctx.add_variable(VarDef { let v2 = ctx.add_variable(VarDef {
name: "V2", name: "V2",
bound: vec![ bound: vec![
ctx.get_primitive(INT32_TYPE).into(), ctx.get_primitive(INT32_TYPE),
ctx.get_primitive(FLOAT_TYPE).into(), ctx.get_primitive(FLOAT_TYPE),
], ],
}); });
let v2 = ctx.get_variable(v2); let v2 = ctx.get_variable(v2);
let v3 = ctx.add_variable(VarDef { let v3 = ctx.add_variable(VarDef {
name: "V3", name: "V3",
bound: vec![ bound: vec![
ctx.get_primitive(BOOL_TYPE).into(), ctx.get_primitive(BOOL_TYPE),
ctx.get_primitive(INT32_TYPE).into(), ctx.get_primitive(INT32_TYPE),
ctx.get_primitive(FLOAT_TYPE).into(), ctx.get_primitive(FLOAT_TYPE),
], ],
}); });
let v3 = ctx.get_variable(v3); let v3 = ctx.get_variable(v3);
@ -320,7 +320,7 @@ mod tests {
); );
assert_eq!( assert_eq!(
resolve_call(&ctx, Some(int32.clone()), "__add__", &[int64.clone()]), resolve_call(&ctx, Some(int32), "__add__", &[int64]),
Err("not equal".to_string()) Err("not equal".to_string())
); );
@ -334,11 +334,11 @@ mod tests {
Err("unbounded type var".to_string()) Err("unbounded type var".to_string())
); );
assert_eq!( assert_eq!(
resolve_call(&ctx, Some(v1.clone()), "__add__", &[v0.clone()]), resolve_call(&ctx, Some(v1.clone()), "__add__", &[v0]),
Err("different domain".to_string()) Err("different domain".to_string())
); );
assert_eq!( assert_eq!(
resolve_call(&ctx, Some(v1.clone()), "__add__", &[v2.clone()]), resolve_call(&ctx, Some(v1.clone()), "__add__", &[v2]),
Err("different domain".to_string()) Err("different domain".to_string())
); );
assert_eq!( assert_eq!(
@ -346,11 +346,11 @@ mod tests {
Err("different domain".to_string()) Err("different domain".to_string())
); );
assert_eq!( assert_eq!(
resolve_call(&ctx, Some(v3.clone()), "__add__", &[v1.clone()]), resolve_call(&ctx, Some(v3.clone()), "__add__", &[v1]),
Err("no such function".to_string()) Err("no such function".to_string())
); );
assert_eq!( assert_eq!(
resolve_call(&ctx, Some(v3.clone()), "__add__", &[v3.clone()]), resolve_call(&ctx, Some(v3.clone()), "__add__", &[v3]),
Err("no such function".to_string()) Err("no such function".to_string())
); );
} }
@ -391,9 +391,9 @@ mod tests {
"foo1", "foo1",
FnDef { FnDef {
args: vec![ args: vec![
ParametricType(TUPLE_TYPE, vec![v0.clone(), v0.clone(), v1.clone()]).into(), ParametricType(TUPLE_TYPE, vec![v0.clone(), v0.clone(), v1]).into(),
], ],
result: Some(v0.clone()), result: Some(v0),
}, },
); );
let ctx = get_inference_context(ctx); let ctx = get_inference_context(ctx);
@ -434,7 +434,7 @@ mod tests {
&ctx, &ctx,
None, None,
"foo1", "foo1",
&[ParametricType(TUPLE_TYPE, vec![v2.clone(), v3.clone(), v3.clone()]).into()] &[ParametricType(TUPLE_TYPE, vec![v2, v3.clone(), v3]).into()]
), ),
Err("different variables".to_string()) Err("different variables".to_string())
); );
@ -456,7 +456,7 @@ mod tests {
list.base.methods.insert( list.base.methods.insert(
"append", "append",
FnDef { FnDef {
args: vec![t.clone()], args: vec![t],
result: None, result: None,
}, },
); );
@ -494,9 +494,9 @@ mod tests {
assert_eq!( assert_eq!(
resolve_call( resolve_call(
&ctx, &ctx,
Some(ParametricType(LIST_TYPE, vec![v0.clone()]).into()), Some(ParametricType(LIST_TYPE, vec![v0]).into()),
"append", "append",
&[v1.clone()] &[v1]
), ),
Err("different variables".to_string()) Err("different variables".to_string())
); );

View File

@ -1,3 +1,6 @@
#![warn(clippy::all)]
#![allow(clippy::clone_double_ref)]
extern crate num_bigint; extern crate num_bigint;
extern crate inkwell; extern crate inkwell;
extern crate rustpython_parser; extern crate rustpython_parser;
@ -231,7 +234,7 @@ impl<'ctx> CodeGen<'ctx> {
}, },
ast::ExpressionType::Identifier { name } => { ast::ExpressionType::Identifier { name } => {
match self.namespace.get(name) { match self.namespace.get(name) {
Some(value) => Ok(self.builder.build_load(*value, name).into()), Some(value) => Ok(self.builder.build_load(*value, name)),
None => Err(self.compile_error(CompileErrorKind::UnboundIdentifier)) None => Err(self.compile_error(CompileErrorKind::UnboundIdentifier))
} }
}, },
@ -413,10 +416,10 @@ impl<'ctx> CodeGen<'ctx> {
_ => Err(self.compile_error(CompileErrorKind::Unsupported("unrecognized call"))) _ => Err(self.compile_error(CompileErrorKind::Unsupported("unrecognized call")))
} }
} else { } else {
return Err(self.compile_error(CompileErrorKind::Unsupported("function must be an identifier"))) Err(self.compile_error(CompileErrorKind::Unsupported("function must be an identifier")))
} }
}, },
_ => return Err(self.compile_error(CompileErrorKind::Unsupported("unimplemented expression"))), _ => Err(self.compile_error(CompileErrorKind::Unsupported("unimplemented expression"))),
} }
} }
@ -522,7 +525,7 @@ impl<'ctx> CodeGen<'ctx> {
} }
}, },
Return { value: None } => { Return { value: None } => {
if !return_type.is_none() { if return_type.is_some() {
return Err(self.compile_error(CompileErrorKind::IncompatibleTypes)); return Err(self.compile_error(CompileErrorKind::IncompatibleTypes));
} }
self.builder.build_return(None); self.builder.build_return(None);

View File

@ -32,7 +32,7 @@ fn impl_math(def: &mut TypeDef, ty: &Type) {
); );
def.methods.insert("__floordiv__", fun.clone()); def.methods.insert("__floordiv__", fun.clone());
def.methods.insert("__mod__", fun.clone()); def.methods.insert("__mod__", fun.clone());
def.methods.insert("__pow__", fun.clone()); def.methods.insert("__pow__", fun);
} }
fn impl_bits(def: &mut TypeDef, ty: &Type) { fn impl_bits(def: &mut TypeDef, ty: &Type) {
@ -43,7 +43,7 @@ fn impl_bits(def: &mut TypeDef, ty: &Type) {
}; };
def.methods.insert("__lshift__", fun.clone()); def.methods.insert("__lshift__", fun.clone());
def.methods.insert("__rshift__", fun.clone()); def.methods.insert("__rshift__", fun);
def.methods.insert( def.methods.insert(
"__xor__", "__xor__",
FnDef { FnDef {
@ -60,7 +60,7 @@ fn impl_eq(def: &mut TypeDef, ty: &Type) {
}; };
def.methods.insert("__eq__", fun.clone()); def.methods.insert("__eq__", fun.clone());
def.methods.insert("__ne__", fun.clone()); def.methods.insert("__ne__", fun);
} }
fn impl_order(def: &mut TypeDef, ty: &Type) { fn impl_order(def: &mut TypeDef, ty: &Type) {
@ -72,7 +72,7 @@ fn impl_order(def: &mut TypeDef, ty: &Type) {
def.methods.insert("__lt__", fun.clone()); def.methods.insert("__lt__", fun.clone());
def.methods.insert("__gt__", fun.clone()); def.methods.insert("__gt__", fun.clone());
def.methods.insert("__le__", fun.clone()); def.methods.insert("__le__", fun.clone());
def.methods.insert("__ge__", fun.clone()); def.methods.insert("__ge__", fun);
} }
pub fn basic_ctx() -> TopLevelContext<'static> { pub fn basic_ctx() -> TopLevelContext<'static> {
@ -172,7 +172,7 @@ pub fn basic_ctx() -> TopLevelContext<'static> {
ctx.add_fn( ctx.add_fn(
"float", "float",
FnDef { FnDef {
args: args.clone(), args,
result: Some(PrimitiveType(FLOAT_TYPE).into()), result: Some(PrimitiveType(FLOAT_TYPE).into()),
}, },
); );

View File

@ -4,6 +4,6 @@ in
pkgs.stdenv.mkDerivation { pkgs.stdenv.mkDerivation {
name = "nac3-env"; name = "nac3-env";
buildInputs = with pkgs; [ buildInputs = with pkgs; [
llvm_10 clang_10 cargo rustc libffi libxml2 llvm_10 clang_10 cargo rustc libffi libxml2 clippy
]; ];
} }