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;
let result = f(self);
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();
if *level > self.stack.level {
let (id, def, _) = self.stack.var_defs.pop().unwrap();
@ -80,7 +80,7 @@ impl<'a> InferenceContext<'a> {
}
}
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();
if *level > self.stack.level {
let (name, _) = self.stack.sym_def.pop().unwrap();
@ -196,7 +196,7 @@ impl TypeEnum {
*id,
params
.iter()
.map(|v| v.as_ref().inv_subst(map).into())
.map(|v| v.as_ref().inv_subst(map))
.collect(),
),
_ => self.clone(),

View File

@ -31,7 +31,7 @@ impl<'a> TopLevelContext<'a> {
primitives.push(TypeEnum::PrimitiveType(PrimitiveId(i)).into());
sym_table.insert(t.name, TypeEnum::PrimitiveType(PrimitiveId(i)).into());
}
return TopLevelContext {
TopLevelContext {
primitive_defs,
class_defs: Vec::new(),
parametric_defs: Vec::new(),
@ -40,7 +40,7 @@ impl<'a> TopLevelContext<'a> {
sym_table,
primitives,
variables: Vec::new(),
};
}
}
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> {
// 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,
keywords,
} => {
if keywords.len() > 0 {
if keywords.is_empty() {
Err("keyword is not supported".into())
} else {
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 {
if elements.len() == 0 {
if elements.is_empty() {
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())?;
if let TypeVariable(id) = value.as_ref() {
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());
}
let ty = v.bound[0]
@ -156,7 +156,7 @@ fn infer_bool_ops<'b: 'a, 'a>(
let b = ctx.get_primitive(BOOL_TYPE);
if left == b && right == b {
Ok(Some(b.into()))
Ok(Some(b))
} else {
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());
}
}
Ok(Some(boolean.into()))
Ok(Some(boolean))
}
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_b = ctx.get_variable_def(*id_b);
if v_b.bound.len() > 0 {
if v_a.bound.len() == 0 {
if !v_b.bound.is_empty() {
if v_a.bound.is_empty() {
return Err("unbounded a".to_string());
} else {
let diff: Vec<_> = v_a
@ -47,12 +47,12 @@ fn find_subst(
.iter()
.filter(|x| !v_b.bound.contains(x))
.collect();
if diff.len() > 0 {
if diff.is_empty() {
return Err("different domain".to_string());
}
}
}
sub.insert(*id_b, a.clone().into());
sub.insert(*id_b, a.clone());
Ok(())
}
(TypeVariable(id_a), _) => {
@ -65,8 +65,8 @@ fn find_subst(
}
(_, TypeVariable(id_b)) => {
let v_b = ctx.get_variable_def(*id_b);
if v_b.bound.len() == 0 || v_b.bound.contains(&a) {
sub.insert(*id_b, a.clone().into());
if v_b.bound.is_empty() || v_b.bound.contains(&a) {
sub.insert(*id_b, a.clone());
Ok(())
} else {
Err("different domain".to_string())
@ -124,14 +124,14 @@ fn resolve_call_rec(
let mut subst = obj
.as_ref()
.map(|v| v.get_subst(ctx))
.unwrap_or(HashMap::new());
.unwrap_or_else(HashMap::new);
let fun = match &obj {
Some(obj) => {
let base = match obj.as_ref() {
TypeVariable(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());
}
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)| {
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();
if results.all(|v| v == first) {
@ -171,7 +171,7 @@ fn resolve_call_rec(
}
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() {
return Err("incorrect parameter number".to_string());
@ -233,17 +233,17 @@ mod tests {
assert_eq!(
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!(
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!(
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!(
@ -281,25 +281,25 @@ mod tests {
let v1 = ctx.add_variable(VarDef {
name: "V1",
bound: vec![
ctx.get_primitive(INT32_TYPE).into(),
ctx.get_primitive(FLOAT_TYPE).into(),
ctx.get_primitive(INT32_TYPE),
ctx.get_primitive(FLOAT_TYPE),
],
});
let v1 = ctx.get_variable(v1);
let v2 = ctx.add_variable(VarDef {
name: "V2",
bound: vec![
ctx.get_primitive(INT32_TYPE).into(),
ctx.get_primitive(FLOAT_TYPE).into(),
ctx.get_primitive(INT32_TYPE),
ctx.get_primitive(FLOAT_TYPE),
],
});
let v2 = ctx.get_variable(v2);
let v3 = ctx.add_variable(VarDef {
name: "V3",
bound: vec![
ctx.get_primitive(BOOL_TYPE).into(),
ctx.get_primitive(INT32_TYPE).into(),
ctx.get_primitive(FLOAT_TYPE).into(),
ctx.get_primitive(BOOL_TYPE),
ctx.get_primitive(INT32_TYPE),
ctx.get_primitive(FLOAT_TYPE),
],
});
let v3 = ctx.get_variable(v3);
@ -320,7 +320,7 @@ mod tests {
);
assert_eq!(
resolve_call(&ctx, Some(int32.clone()), "__add__", &[int64.clone()]),
resolve_call(&ctx, Some(int32), "__add__", &[int64]),
Err("not equal".to_string())
);
@ -334,11 +334,11 @@ mod tests {
Err("unbounded type var".to_string())
);
assert_eq!(
resolve_call(&ctx, Some(v1.clone()), "__add__", &[v0.clone()]),
resolve_call(&ctx, Some(v1.clone()), "__add__", &[v0]),
Err("different domain".to_string())
);
assert_eq!(
resolve_call(&ctx, Some(v1.clone()), "__add__", &[v2.clone()]),
resolve_call(&ctx, Some(v1.clone()), "__add__", &[v2]),
Err("different domain".to_string())
);
assert_eq!(
@ -346,11 +346,11 @@ mod tests {
Err("different domain".to_string())
);
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())
);
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())
);
}
@ -391,9 +391,9 @@ mod tests {
"foo1",
FnDef {
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);
@ -434,7 +434,7 @@ mod tests {
&ctx,
None,
"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())
);
@ -456,7 +456,7 @@ mod tests {
list.base.methods.insert(
"append",
FnDef {
args: vec![t.clone()],
args: vec![t],
result: None,
},
);
@ -494,9 +494,9 @@ mod tests {
assert_eq!(
resolve_call(
&ctx,
Some(ParametricType(LIST_TYPE, vec![v0.clone()]).into()),
Some(ParametricType(LIST_TYPE, vec![v0]).into()),
"append",
&[v1.clone()]
&[v1]
),
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 inkwell;
extern crate rustpython_parser;
@ -231,7 +234,7 @@ impl<'ctx> CodeGen<'ctx> {
},
ast::ExpressionType::Identifier { 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))
}
},
@ -413,10 +416,10 @@ impl<'ctx> CodeGen<'ctx> {
_ => Err(self.compile_error(CompileErrorKind::Unsupported("unrecognized call")))
}
} 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 } => {
if !return_type.is_none() {
if return_type.is_some() {
return Err(self.compile_error(CompileErrorKind::IncompatibleTypes));
}
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("__mod__", fun.clone());
def.methods.insert("__pow__", fun.clone());
def.methods.insert("__pow__", fun);
}
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("__rshift__", fun.clone());
def.methods.insert("__rshift__", fun);
def.methods.insert(
"__xor__",
FnDef {
@ -60,7 +60,7 @@ fn impl_eq(def: &mut TypeDef, ty: &Type) {
};
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) {
@ -72,7 +72,7 @@ fn impl_order(def: &mut TypeDef, ty: &Type) {
def.methods.insert("__lt__", fun.clone());
def.methods.insert("__gt__", 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> {
@ -172,7 +172,7 @@ pub fn basic_ctx() -> TopLevelContext<'static> {
ctx.add_fn(
"float",
FnDef {
args: args.clone(),
args,
result: Some(PrimitiveType(FLOAT_TYPE).into()),
},
);

View File

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