From e0cf28860ab72659ef2f22527d912a302a70c4ed Mon Sep 17 00:00:00 2001 From: xunilrj Date: Fri, 9 Aug 2024 10:32:10 +0100 Subject: [PATCH] clippy and fmt issues --- .../ast_node/expression/intrinsic_function.rs | 4 +- .../ast_node/expression/typed_expression.rs | 79 ------------------- .../to_parsed_lang/convert_parse_tree.rs | 45 ++++++----- sway-lsp/src/traverse/parsed_tree.rs | 31 +++----- 4 files changed, 37 insertions(+), 122 deletions(-) diff --git a/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs b/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs index 678c5a5e40e..8d7594f08b0 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs @@ -238,7 +238,7 @@ fn type_check_slice( }; // start index argument - let start_ty_expr = if let Some(v) = arguments.get(1) { + let start_ty_expr = if arguments.get(1).is_some() { let start_type = type_engine.insert( engines, TypeInfo::UnsignedInteger(IntegerBits::SixtyFour), @@ -259,7 +259,7 @@ fn type_check_slice( }; // end index argument - let end_ty_expr = if let Some(v) = arguments.get(2) { + let end_ty_expr = if arguments.get(2).is_some() { let end_type = type_engine.insert( engines, TypeInfo::UnsignedInteger(IntegerBits::SixtyFour), diff --git a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs index f9cdbf53f75..47115ec611e 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs @@ -1876,85 +1876,6 @@ impl ty::TyExpression { }) } - fn type_check_array_index( - handler: &Handler, - mut ctx: TypeCheckContext, - prefix: &Expression, - index: &Expression, - span: Span, - ) -> Result { - let type_engine = ctx.engines.te(); - let engines = ctx.engines(); - - let mut current_prefix_te = Box::new({ - let ctx = ctx - .by_ref() - .with_help_text("") - .with_type_annotation(type_engine.insert(engines, TypeInfo::Unknown, None)); - - ty::TyExpression::type_check(handler, ctx, prefix)? - }); - - let mut current_type = type_engine.get_unaliased(current_prefix_te.return_type); - - let prefix_type_id = current_prefix_te.return_type; - let prefix_span = current_prefix_te.span.clone(); - - // Create the prefix part of the final array index expression. - // This might be an expression that directly evaluates to an array type, - // or an arbitrary number of dereferencing expressions where the last one - // dereference to an array type. - // - // We will either hit an array at the end or return an error, so the - // loop cannot be endless. - while !current_type.is_array() { - match &*current_type { - TypeInfo::Ref { - referenced_type, .. - } => { - let referenced_type_id = referenced_type.type_id; - - current_prefix_te = Box::new(ty::TyExpression { - expression: ty::TyExpressionVariant::Deref(current_prefix_te), - return_type: referenced_type_id, - span: prefix_span.clone(), - }); - - current_type = type_engine.get_unaliased(referenced_type_id); - } - TypeInfo::ErrorRecovery(err) => return Err(*err), - _ => { - return Err(handler.emit_err(CompileError::NotIndexable { - actually: engines.help_out(prefix_type_id).to_string(), - span: prefix_span, - })) - } - }; - } - - let TypeInfo::Array(array_type_argument, _) = &*current_type else { - panic!("The current type must be an array."); - }; - - let index_te = { - let type_info_u64 = TypeInfo::UnsignedInteger(IntegerBits::SixtyFour); - let ctx = ctx - .with_help_text("Array index must be of type \"u64\".") - .with_type_annotation(type_engine.insert(engines, type_info_u64, None)); - - ty::TyExpression::type_check(handler, ctx, index)? - }; - - Ok(ty::TyExpression { - expression: ty::TyExpressionVariant::ArrayIndex { - prefix: current_prefix_te, - index: Box::new(index_te), - }, - return_type: array_type_argument.type_id, - span, - }) - } - fn type_check_intrinsic_function( handler: &Handler, ctx: TypeCheckContext, diff --git a/sway-core/src/transform/to_parsed_lang/convert_parse_tree.rs b/sway-core/src/transform/to_parsed_lang/convert_parse_tree.rs index 487d1853589..a2d579cb871 100644 --- a/sway-core/src/transform/to_parsed_lang/convert_parse_tree.rs +++ b/sway-core/src/transform/to_parsed_lang/convert_parse_tree.rs @@ -44,7 +44,7 @@ use sway_types::{ use sway_types::{Ident, Span, Spanned}; use std::{ - collections::HashSet, convert::TryFrom, iter, mem::MaybeUninit, str::FromStr, sync::Arc + collections::HashSet, convert::TryFrom, iter, mem::MaybeUninit, str::FromStr, sync::Arc, }; pub fn convert_parse_tree( @@ -2183,7 +2183,7 @@ fn expr_to_expression( kind: ExpressionKind::Deref(Box::new(expr)), span, } - }, + } Expr::MethodCall { target, path_seg, @@ -2269,21 +2269,25 @@ fn expr_to_expression( }, Expr::Ref { mut_token, expr, .. - } => { - match *expr { - Expr::Index { target, arg } => { - let target = expr_to_expression(context, handler, engines, *target)?; - let arg = expr_to_expression(context, handler, engines, *arg.inner)?; - desugar_into_index_trait(mut_token.is_some(), arg.span(), span.clone(), target, arg)? - }, - expr => Expression { - kind: ExpressionKind::Ref(RefExpression { - to_mutable_value: mut_token.is_some(), - value: Box::new(expr_to_expression(context, handler, engines, expr)?), - }), - span, - } + } => match *expr { + Expr::Index { target, arg } => { + let target = expr_to_expression(context, handler, engines, *target)?; + let arg = expr_to_expression(context, handler, engines, *arg.inner)?; + desugar_into_index_trait( + mut_token.is_some(), + arg.span(), + span.clone(), + target, + arg, + )? } + expr => Expression { + kind: ExpressionKind::Ref(RefExpression { + to_mutable_value: mut_token.is_some(), + value: Box::new(expr_to_expression(context, handler, engines, expr)?), + }), + span, + }, }, Expr::Deref { expr, .. } => Expression { kind: ExpressionKind::Deref(Box::new(expr_to_expression( @@ -2540,11 +2544,7 @@ fn desugar_into_index_trait( span: span.clone(), }; - let name = if mutable { - "index_mut" - } else { - "index" - }; + let name = if mutable { "index_mut" } else { "index" }; let call_path_binding = TypeBinding { inner: CallPath { @@ -4504,8 +4504,7 @@ fn assignable_to_expression( let target = element_access_to_expression(context, handler, engines, *target, span.clone())?; let arg = expr_to_expression(context, handler, engines, *arg.inner)?; - let call = - desugar_into_index_trait(true, arg.span(), span.clone(), target, arg)?; + let call = desugar_into_index_trait(true, arg.span(), span.clone(), target, arg)?; Expression { kind: ExpressionKind::Deref(Box::new(call)), span, diff --git a/sway-lsp/src/traverse/parsed_tree.rs b/sway-lsp/src/traverse/parsed_tree.rs index 880915f915b..c079b7f475b 100644 --- a/sway-lsp/src/traverse/parsed_tree.rs +++ b/sway-lsp/src/traverse/parsed_tree.rs @@ -18,20 +18,19 @@ use sway_core::{ decl_engine::parsed_id::ParsedDeclId, language::{ parsed::{ - AbiCastExpression, AbiDeclaration, AmbiguousPathExpression, ArrayExpression, - ArrayIndexExpression, AstNode, AstNodeContent, ConfigurableDeclaration, - ConstantDeclaration, Declaration, DelineatedPathExpression, EnumDeclaration, - EnumVariant, Expression, ExpressionKind, ForLoopExpression, - FunctionApplicationExpression, FunctionDeclaration, FunctionParameter, IfExpression, - ImplItem, ImplSelfOrTrait, ImportType, IncludeStatement, IntrinsicFunctionExpression, - LazyOperatorExpression, MatchExpression, MethodApplicationExpression, MethodName, - ParseModule, ParseProgram, ParseSubmodule, QualifiedPathType, ReassignmentExpression, - ReassignmentTarget, RefExpression, Scrutinee, StorageAccessExpression, - StorageDeclaration, StorageEntry, StorageField, StorageNamespace, StructDeclaration, - StructExpression, StructExpressionField, StructField, StructScrutineeField, - SubfieldExpression, Supertrait, TraitDeclaration, TraitFn, TraitItem, - TraitTypeDeclaration, TupleIndexExpression, TypeAliasDeclaration, UseStatement, - VariableDeclaration, WhileLoopExpression, + AbiCastExpression, AbiDeclaration, AmbiguousPathExpression, ArrayExpression, AstNode, + AstNodeContent, ConfigurableDeclaration, ConstantDeclaration, Declaration, + DelineatedPathExpression, EnumDeclaration, EnumVariant, Expression, ExpressionKind, + ForLoopExpression, FunctionApplicationExpression, FunctionDeclaration, + FunctionParameter, IfExpression, ImplItem, ImplSelfOrTrait, ImportType, + IncludeStatement, IntrinsicFunctionExpression, LazyOperatorExpression, MatchExpression, + MethodApplicationExpression, MethodName, ParseModule, ParseProgram, ParseSubmodule, + QualifiedPathType, ReassignmentExpression, ReassignmentTarget, RefExpression, + Scrutinee, StorageAccessExpression, StorageDeclaration, StorageEntry, StorageField, + StorageNamespace, StructDeclaration, StructExpression, StructExpressionField, + StructField, StructScrutineeField, SubfieldExpression, Supertrait, TraitDeclaration, + TraitFn, TraitItem, TraitTypeDeclaration, TupleIndexExpression, TypeAliasDeclaration, + UseStatement, VariableDeclaration, WhileLoopExpression, }, CallPathTree, HasSubmodules, Literal, }, @@ -308,10 +307,6 @@ impl Parse for Expression { ExpressionKind::AbiCast(abi_cast_expression) => { abi_cast_expression.parse(ctx); } - ExpressionKind::ArrayIndex(ArrayIndexExpression { prefix, index, .. }) => { - prefix.parse(ctx); - index.parse(ctx); - } ExpressionKind::StorageAccess(StorageAccessExpression { field_names, namespace_names,