clippy again

This commit is contained in:
pca006132 2021-01-04 12:28:06 +08:00
parent 2b2e220723
commit 67963fc21f

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