clippy again

This commit is contained in:
pca006132 2021-01-04 12:28:06 +08:00
parent 2b2e220723
commit 67963fc21f
1 changed files with 21 additions and 21 deletions

View File

@ -111,12 +111,12 @@ fn infer_tuple<'b: 'a, 'a>(ctx: &mut InferenceContext<'a>, elements: &'b [Expres
}
}
fn infer_attribute<'b: 'a, 'a>(
fn infer_attribute<'a>(
ctx: &mut InferenceContext<'a>,
value: &'a Expression,
name: &String,
name: &str,
) -> ParserResult {
let value = infer_expr(ctx, value)?.ok_or("no value".to_string())?;
let value = infer_expr(ctx, value)?.ok_or_else(|| "no value".to_string())?;
if let TypeVariable(id) = value.as_ref() {
let v = ctx.get_variable_def(*id);
if v.bound.is_empty() {
@ -124,12 +124,12 @@ fn infer_attribute<'b: 'a, 'a>(
}
let ty = v.bound[0]
.get_base(ctx)
.and_then(|v| v.fields.get(name.as_str()));
.and_then(|v| v.fields.get(name));
if ty.is_none() {
return Err("unknown field".into());
}
for x in v.bound[1..].iter() {
let ty1 = x.get_base(ctx).and_then(|v| v.fields.get(name.as_str()));
let ty1 = x.get_base(ctx).and_then(|v| v.fields.get(name));
if ty1 != ty {
return Err("unknown field (type mismatch between variants)".into());
}
@ -138,7 +138,7 @@ fn infer_attribute<'b: 'a, 'a>(
}
match value.get_base(ctx) {
Some(b) => match b.fields.get(name.as_str()) {
Some(b) => match b.fields.get(name) {
Some(t) => Ok(Some(t.clone())),
None => Err("no such field".into()),
},
@ -146,13 +146,13 @@ fn infer_attribute<'b: 'a, 'a>(
}
}
fn infer_bool_ops<'b: 'a, 'a>(
fn infer_bool_ops<'a>(
ctx: &mut InferenceContext<'a>,
values: &'a [Expression],
) -> ParserResult {
assert_eq!(values.len(), 2);
let left = infer_expr(ctx, &values[0])?.ok_or("no value".to_string())?;
let right = infer_expr(ctx, &values[1])?.ok_or("no value".to_string())?;
let left = infer_expr(ctx, &values[0])?.ok_or_else(|| "no value".to_string())?;
let right = infer_expr(ctx, &values[1])?.ok_or_else(|| "no value".to_string())?;
let b = ctx.get_primitive(BOOL_TYPE);
if left == b && right == b {
@ -168,8 +168,8 @@ fn infer_bin_ops<'b: 'a, 'a>(
left: &'b Expression,
right: &'b Expression,
) -> ParserResult {
let left = infer_expr(ctx, left)?.ok_or("no value".to_string())?;
let right = infer_expr(ctx, right)?.ok_or("no value".to_string())?;
let left = infer_expr(ctx, left)?.ok_or_else(|| "no value".to_string())?;
let right = infer_expr(ctx, right)?.ok_or_else(|| "no value".to_string())?;
let fun = binop_name(op);
resolve_call(ctx, Some(left), fun, &[right])
}
@ -179,7 +179,7 @@ fn infer_unary_ops<'b: 'a, 'a>(
op: &UnaryOperator,
obj: &'b Expression,
) -> ParserResult {
let ty = infer_expr(ctx, obj)?.ok_or("no value".to_string())?;
let ty = infer_expr(ctx, obj)?.ok_or_else(|| "no value".to_string())?;
if let UnaryOperator::Not = op {
if ty == ctx.get_primitive(BOOL_TYPE) {
Ok(Some(ty))
@ -208,7 +208,7 @@ fn infer_compare<'b: 'a, 'a>(
let right = &types[1..];
for ((a, b), op) in left.iter().zip(right.iter()).zip(ops.iter()) {
let fun = comparison_name(op).ok_or("unsupported comparison".to_string())?;
let fun = comparison_name(op).ok_or_else(|| "unsupported comparison".to_string())?;
let ty = resolve_call(ctx, Some(a.clone()), fun, &[b.clone()])?;
if ty.is_none() || ty.unwrap() != boolean {
return Err("comparison result must be boolean".into());
@ -232,7 +232,7 @@ fn infer_call<'b: 'a, 'a>(
let (obj, fun) = match &function.node {
ExpressionType::Identifier { name } => (None, name),
ExpressionType::Attribute { value, name } => (
Some(infer_expr(ctx, &value)?.ok_or("no value".to_string())?),
Some(infer_expr(ctx, &value)?.ok_or_else(|| "no value".to_string())?),
name,
),
_ => return Err("not supported".into()),
@ -245,7 +245,7 @@ fn infer_subscript<'b: 'a, 'a>(
a: &'b Expression,
b: &'b Expression,
) -> ParserResult {
let a = infer_expr(ctx, a)?.ok_or("no value".to_string())?;
let a = infer_expr(ctx, a)?.ok_or_else(|| "no value".to_string())?;
let t = if let ParametricType(LIST_TYPE, ls) = a.as_ref() {
ls[0].clone()
} else {
@ -265,7 +265,7 @@ fn infer_subscript<'b: 'a, 'a>(
}
})
.collect();
let types = types?.ok_or("slice must have type".to_string())?;
let types = types?.ok_or_else(|| "slice must have type".to_string())?;
if types.iter().all(|v| v == &int32) {
Ok(Some(a))
} else {
@ -273,7 +273,7 @@ fn infer_subscript<'b: 'a, 'a>(
}
}
_ => {
let b = infer_expr(ctx, b)?.ok_or("no value".to_string())?;
let b = infer_expr(ctx, b)?.ok_or_else(|| "no value".to_string())?;
if b == ctx.get_primitive(INT32_TYPE) {
Ok(Some(t))
} else {
@ -289,7 +289,7 @@ fn infer_if_expr<'b: 'a, 'a>(
body: &'b Expression,
orelse: &'b Expression,
) -> ParserResult {
let test = infer_expr(ctx, test)?.ok_or("no value".to_string())?;
let test = infer_expr(ctx, test)?.ok_or_else(|| "no value".to_string())?;
if test != ctx.get_primitive(BOOL_TYPE) {
return Err("test should be bool".into());
}
@ -346,7 +346,7 @@ fn infer_list_comprehension<'b: 'a, 'a>(
return Err("async is not supported".into());
}
let iter = infer_expr(ctx, &comprehension.iter)?.ok_or("no value".to_string())?;
let iter = infer_expr(ctx, &comprehension.iter)?.ok_or_else(|| "no value".to_string())?;
if let ParametricType(LIST_TYPE, ls) = iter.as_ref() {
ctx.with_scope(|ctx| {
infer_simple_binding(ctx, &comprehension.target, ls[0].clone())?;
@ -354,12 +354,12 @@ fn infer_list_comprehension<'b: 'a, 'a>(
let boolean = ctx.get_primitive(BOOL_TYPE);
for test in comprehension.ifs.iter() {
let result =
infer_expr(ctx, test)?.ok_or("no value in test".to_string())?;
infer_expr(ctx, test)?.ok_or_else(|| "no value in test".to_string())?;
if result != boolean {
return Err("test must be bool".into());
}
}
let result = infer_expr(ctx, element)?.ok_or("no value")?;
let result = infer_expr(ctx, element)?.ok_or_else(|| "no value")?;
Ok(Some(ParametricType(LIST_TYPE, vec![result]).into()))
}).1
} else {