From 6bc6a67c80bc99ad40226ddb3f14843d2f5a2328 Mon Sep 17 00:00:00 2001 From: Peefy Date: Mon, 29 Jan 2024 17:35:39 +0800 Subject: [PATCH] refactor: cargo clippy for api, compiler, config and sema crates (#1013) Signed-off-by: peefy --- kclvm/api/src/capi_test.rs | 4 +- kclvm/api/src/service/into.rs | 4 +- kclvm/ast/src/ast.rs | 28 ++++++------ kclvm/ast/src/lib.rs | 2 +- kclvm/ast_pretty/src/tests.rs | 6 +-- kclvm/compiler/src/codegen/error.rs | 2 +- kclvm/compiler/src/codegen/llvm/context.rs | 6 +-- kclvm/compiler/src/codegen/llvm/node.rs | 2 +- kclvm/compiler/src/codegen/llvm/schema.rs | 2 +- kclvm/compiler/src/codegen/llvm/utils.rs | 2 +- kclvm/compiler/src/lib.rs | 2 +- kclvm/compiler/src/macros.rs | 2 +- kclvm/compiler/src/value/lambda.rs | 2 +- kclvm/compiler/src/value/mod.rs | 2 +- kclvm/compiler/src/value/schema.rs | 2 +- kclvm/config/src/cache.rs | 2 +- kclvm/config/src/lib.rs | 2 +- kclvm/config/src/modfile.rs | 2 +- kclvm/config/src/settings.rs | 2 +- kclvm/driver/src/kpm_metadata.rs | 4 +- kclvm/driver/src/lib.rs | 53 ++++++++++------------ kclvm/driver/src/tests.rs | 19 ++++---- kclvm/parser/src/lib.rs | 2 +- kclvm/sema/src/builtin/system_module.rs | 2 +- kclvm/version/src/lib.rs | 2 +- 25 files changed, 79 insertions(+), 79 deletions(-) diff --git a/kclvm/api/src/capi_test.rs b/kclvm/api/src/capi_test.rs index cac81ade8..963b996ad 100644 --- a/kclvm/api/src/capi_test.rs +++ b/kclvm/api/src/capi_test.rs @@ -78,7 +78,7 @@ fn test_c_api_get_full_schema_type() { "get-full-schema-type.response.json", |r| { for s_ty in &mut r.schema_type_list { - s_ty.filename = s_ty.filename.replace("/", "").replace("\\", "") + s_ty.filename = s_ty.filename.replace('/', "").replace('\\', "") } }, ); @@ -92,7 +92,7 @@ fn test_c_api_get_all_full_schema_types() { "get-all-full-schema-types.response.json", |r| { for s_ty in &mut r.schema_type_list { - s_ty.filename = s_ty.filename.replace("/", "").replace("\\", "") + s_ty.filename = s_ty.filename.replace('/', "").replace('\\', "") } }, ); diff --git a/kclvm/api/src/service/into.rs b/kclvm/api/src/service/into.rs index cf90367e7..08ed6741a 100644 --- a/kclvm/api/src/service/into.rs +++ b/kclvm/api/src/service/into.rs @@ -93,7 +93,7 @@ impl IntoSymbolIndex for SymbolRef { let (index, generation) = self.get_id().into_raw_parts(); SymbolIndex { i: index as u64, - g: generation as u64, + g: generation, kind: format!("{:?}", self.get_kind()), } } @@ -104,7 +104,7 @@ impl IntoScopeIndex for ScopeRef { let (index, generation) = self.get_id().into_raw_parts(); ScopeIndex { i: index as u64, - g: generation as u64, + g: generation, kind: format!("{:?}", self.get_kind()), } } diff --git a/kclvm/ast/src/ast.rs b/kclvm/ast/src/ast.rs index 79a6971de..d877efaf2 100644 --- a/kclvm/ast/src/ast.rs +++ b/kclvm/ast/src/ast.rs @@ -61,24 +61,24 @@ impl From for Pos { } } -impl Into for Pos { - fn into(self) -> PosTuple { - (self.0, self.1, self.2, self.3, self.4) +impl From for PosTuple { + fn from(val: Pos) -> Self { + (val.0, val.1, val.2, val.3, val.4) } } -impl Into for Pos { - fn into(self) -> Range { +impl From for Range { + fn from(val: Pos) -> Self { ( Position { - filename: self.0.clone(), - line: self.1, - column: Some(self.2), + filename: val.0.clone(), + line: val.1, + column: Some(val.2), }, Position { - filename: self.0, - line: self.3, - column: Some(self.4), + filename: val.0, + line: val.3, + column: Some(val.4), }, ) } @@ -413,7 +413,7 @@ impl Module { stmts.push(node_ref!(schema_stmt.clone(), stmt.pos())); } } - return stmts; + stmts } /// Get stmt on position @@ -1746,7 +1746,7 @@ impl ToString for Type { } }, Type::Function(v) => { - w.push_str("("); + w.push('('); if let Some(params) = &v.params_ty { for (i, param) in params.iter().enumerate() { if i > 0 { @@ -1755,7 +1755,7 @@ impl ToString for Type { to_str(¶m.node, w); } } - w.push_str(")"); + w.push(')'); if let Some(ret) = &v.ret_ty { w.push_str(" -> "); to_str(&ret.node, w); diff --git a/kclvm/ast/src/lib.rs b/kclvm/ast/src/lib.rs index 8eeb09a1f..a6bed9d3c 100644 --- a/kclvm/ast/src/lib.rs +++ b/kclvm/ast/src/lib.rs @@ -1,4 +1,4 @@ -// Copyright 2021 The KCL Authors. All rights reserved. +//! Copyright The KCL Authors. All rights reserved. pub mod ast; pub mod config; diff --git a/kclvm/ast_pretty/src/tests.rs b/kclvm/ast_pretty/src/tests.rs index b75a6c3e5..2867c1a38 100644 --- a/kclvm/ast_pretty/src/tests.rs +++ b/kclvm/ast_pretty/src/tests.rs @@ -6,7 +6,7 @@ use pretty_assertions::assert_eq; const FILE_INPUT_SUFFIX: &str = ".input"; const FILE_OUTPUT_SUFFIX: &str = ".output"; -const TEST_CASES: &[&'static str; 16] = &[ +const TEST_CASES: &[&str; 16] = &[ "arguments", "empty", "if_stmt", @@ -30,7 +30,7 @@ fn read_data(data_name: &str) -> (String, String) { filename.push( Path::new("src") .join("test_data") - .join(&format!("{}{}", data_name, FILE_INPUT_SUFFIX)) + .join(format!("{}{}", data_name, FILE_INPUT_SUFFIX)) .display() .to_string(), ); @@ -41,7 +41,7 @@ fn read_data(data_name: &str) -> (String, String) { filename_expect.push( Path::new("src") .join("test_data") - .join(&format!("{}{}", data_name, FILE_OUTPUT_SUFFIX)) + .join(format!("{}{}", data_name, FILE_OUTPUT_SUFFIX)) .display() .to_string(), ); diff --git a/kclvm/compiler/src/codegen/error.rs b/kclvm/compiler/src/codegen/error.rs index 50cc0fb2c..1b165e80f 100644 --- a/kclvm/compiler/src/codegen/error.rs +++ b/kclvm/compiler/src/codegen/error.rs @@ -1,4 +1,4 @@ -// Copyright 2021 The KCL Authors. All rights reserved. +//! Copyright The KCL Authors. All rights reserved. use std::error; use std::fmt::{self, Debug}; diff --git a/kclvm/compiler/src/codegen/llvm/context.rs b/kclvm/compiler/src/codegen/llvm/context.rs index feec66f67..311210b4d 100644 --- a/kclvm/compiler/src/codegen/llvm/context.rs +++ b/kclvm/compiler/src/codegen/llvm/context.rs @@ -1562,7 +1562,7 @@ impl<'ctx> LLVMCodeGenContext<'ctx> { let mut pkg_scopes = self.pkg_scopes.borrow_mut(); let scopes = pkg_scopes .get_mut(¤t_pkgpath) - .expect(&format!("pkgpath {} is not found", current_pkgpath)); + .unwrap_or_else(|| panic!("pkgpath {} is not found", current_pkgpath)); if let Some(last) = scopes.last_mut() { let mut scalars = last.scalars.borrow_mut(); // TODO: To avoid conflicts, only the last schema scalar expressions are allowed. @@ -2090,7 +2090,7 @@ impl<'ctx> LLVMCodeGenContext<'ctx> { let mut pkg_scopes = self.pkg_scopes.borrow_mut(); let scopes = pkg_scopes .get_mut(¤t_pkgpath) - .expect(&format!("pkgpath {} is not found", current_pkgpath)); + .unwrap_or_else(|| panic!("pkgpath {} is not found", current_pkgpath)); // The global scope. let scope = scopes.last().expect(kcl_error::INTERNAL_ERROR_MSG); let scalars = scope.scalars.borrow(); @@ -2099,7 +2099,7 @@ impl<'ctx> LLVMCodeGenContext<'ctx> { let global_dict = self.dict_value(); // Deal scalars for scalar in scalars.iter() { - self.dict_safe_insert(global_dict, SCALAR_KEY, scalar.clone(), 0, -1); + self.dict_safe_insert(global_dict, SCALAR_KEY, *scalar, 0, -1); } // Deal global variables for (name, ptr) in globals.iter() { diff --git a/kclvm/compiler/src/codegen/llvm/node.rs b/kclvm/compiler/src/codegen/llvm/node.rs index fcd32850e..953717329 100644 --- a/kclvm/compiler/src/codegen/llvm/node.rs +++ b/kclvm/compiler/src/codegen/llvm/node.rs @@ -966,7 +966,7 @@ impl<'ctx> TypedResultWalker<'ctx> for LLVMCodeGenContext<'ctx> { } else { if_level = 0; } - self.walk_stmt(*stmt).expect(kcl_error::COMPILE_ERROR_MSG); + self.walk_stmt(stmt).expect(kcl_error::COMPILE_ERROR_MSG); // Backtrack meta end if matches!(&stmt.node, ast::Stmt::If(..)) { *self.backtrack_meta.borrow_mut() = None diff --git a/kclvm/compiler/src/codegen/llvm/schema.rs b/kclvm/compiler/src/codegen/llvm/schema.rs index dc994920f..38b40203a 100644 --- a/kclvm/compiler/src/codegen/llvm/schema.rs +++ b/kclvm/compiler/src/codegen/llvm/schema.rs @@ -72,7 +72,7 @@ impl<'ctx> LLVMCodeGenContext<'ctx> { if let Some(index_signature) = index_signature { self.default_collection_insert_value( cal_map, - &kclvm_runtime::CAL_MAP_INDEX_SIGNATURE, + kclvm_runtime::CAL_MAP_INDEX_SIGNATURE, self.int_value(index_signature.line as i64), ); place_holder_map.insert(kclvm_runtime::CAL_MAP_INDEX_SIGNATURE.to_string(), vec![]); diff --git a/kclvm/compiler/src/codegen/llvm/utils.rs b/kclvm/compiler/src/codegen/llvm/utils.rs index ac6a2af81..b6c1cd11f 100644 --- a/kclvm/compiler/src/codegen/llvm/utils.rs +++ b/kclvm/compiler/src/codegen/llvm/utils.rs @@ -13,7 +13,7 @@ use super::context::LLVMCodeGenContext; */ /// Update runtime context pkgpath -pub fn update_ctx_pkgpath<'ctx>(gen: &'ctx LLVMCodeGenContext, pkgpath: &str) { +pub fn update_ctx_pkgpath(gen: &LLVMCodeGenContext, pkgpath: &str) { gen.build_void_call( &ApiFunc::kclvm_context_set_kcl_pkgpath.name(), &[ diff --git a/kclvm/compiler/src/lib.rs b/kclvm/compiler/src/lib.rs index 949b320cd..446c83911 100644 --- a/kclvm/compiler/src/lib.rs +++ b/kclvm/compiler/src/lib.rs @@ -1,4 +1,4 @@ -// Copyright 2021 The KCL Authors. All rights reserved. +//! Copyright The KCL Authors. All rights reserved. pub mod codegen; pub mod value; diff --git a/kclvm/compiler/src/macros.rs b/kclvm/compiler/src/macros.rs index 68fe9492d..31cd1e64d 100644 --- a/kclvm/compiler/src/macros.rs +++ b/kclvm/compiler/src/macros.rs @@ -1,4 +1,4 @@ -// Copyright 2021 The KCL Authors. All rights reserved. +//! Copyright The KCL Authors. All rights reserved. #[macro_export] macro_rules! check_backtrack_stop { diff --git a/kclvm/compiler/src/value/lambda.rs b/kclvm/compiler/src/value/lambda.rs index 5948071d2..4e7c84440 100644 --- a/kclvm/compiler/src/value/lambda.rs +++ b/kclvm/compiler/src/value/lambda.rs @@ -1,4 +1,4 @@ -// Copyright 2021 The KCL Authors. All rights reserved. +//! Copyright The KCL Authors. All rights reserved. pub const LAMBDA_NAME: &str = "kclvm_lambda"; pub const LAMBDA_CLOSURE: &str = "$lambda_closure"; diff --git a/kclvm/compiler/src/value/mod.rs b/kclvm/compiler/src/value/mod.rs index 3d10ce495..1578aef2e 100644 --- a/kclvm/compiler/src/value/mod.rs +++ b/kclvm/compiler/src/value/mod.rs @@ -1,4 +1,4 @@ -// Copyright 2021 The KCL Authors. All rights reserved. +//! Copyright The KCL Authors. All rights reserved. mod lambda; pub use self::lambda::*; diff --git a/kclvm/compiler/src/value/schema.rs b/kclvm/compiler/src/value/schema.rs index 3f2cf46b0..019a121cc 100644 --- a/kclvm/compiler/src/value/schema.rs +++ b/kclvm/compiler/src/value/schema.rs @@ -1,4 +1,4 @@ -// Copyright 2021 The KCL Authors. All rights reserved. +//! Copyright The KCL Authors. All rights reserved. pub const SCHEMA_NAME: &str = "$kclvm_schema"; pub const SCHEMA_ATTR_NAME: &str = "$kclvm_schema_attr"; diff --git a/kclvm/config/src/cache.rs b/kclvm/config/src/cache.rs index cc5f26a48..c7ae46bcc 100644 --- a/kclvm/config/src/cache.rs +++ b/kclvm/config/src/cache.rs @@ -1,4 +1,4 @@ -// Copyright 2021 The KCL Authors. All rights reserved. +//! Copyright The KCL Authors. All rights reserved. extern crate chrono; use super::modfile::KCL_FILE_SUFFIX; use anyhow::Result; diff --git a/kclvm/config/src/lib.rs b/kclvm/config/src/lib.rs index d702922c6..edeb16cb9 100644 --- a/kclvm/config/src/lib.rs +++ b/kclvm/config/src/lib.rs @@ -1,4 +1,4 @@ -// Copyright 2021 The KCL Authors. All rights reserved. +//! Copyright The KCL Authors. All rights reserved. pub mod cache; pub mod modfile; diff --git a/kclvm/config/src/modfile.rs b/kclvm/config/src/modfile.rs index f266c446d..2f3a13d84 100644 --- a/kclvm/config/src/modfile.rs +++ b/kclvm/config/src/modfile.rs @@ -1,4 +1,4 @@ -// Copyright 2021 The KCL Authors. All rights reserved. +//! Copyright The KCL Authors. All rights reserved. use anyhow::Result; use kclvm_utils::path::PathPrefix; diff --git a/kclvm/config/src/settings.rs b/kclvm/config/src/settings.rs index 6e404329c..ae8af986c 100644 --- a/kclvm/config/src/settings.rs +++ b/kclvm/config/src/settings.rs @@ -1,4 +1,4 @@ -// Copyright 2021 The KCL Authors. All rights reserved. +//! Copyright The KCL Authors. All rights reserved. use anyhow::{Context, Result}; use serde::{ de::{DeserializeSeed, Error, MapAccess, SeqAccess, Unexpected, Visitor}, diff --git a/kclvm/driver/src/kpm_metadata.rs b/kclvm/driver/src/kpm_metadata.rs index 6275588ed..996d404fa 100644 --- a/kclvm/driver/src/kpm_metadata.rs +++ b/kclvm/driver/src/kpm_metadata.rs @@ -28,7 +28,7 @@ pub(crate) fn fill_pkg_maps_for_k_file( None => return Ok(()), }; - return Ok(()); + Ok(()) } #[derive(Deserialize, Serialize, Default, Debug, Clone)] @@ -126,7 +126,7 @@ pub(crate) fn lookup_the_nearest_file_dir( loop { let found_path = current_dir.join(the_nearest_file); if found_path.is_file() { - return Some(current_dir.canonicalize().ok()?); + return current_dir.canonicalize().ok(); } match current_dir.parent() { diff --git a/kclvm/driver/src/lib.rs b/kclvm/driver/src/lib.rs index 533cbb595..a454f65d2 100644 --- a/kclvm/driver/src/lib.rs +++ b/kclvm/driver/src/lib.rs @@ -70,7 +70,7 @@ pub fn canonicalize_input_files( // join with the work directory path and convert it to a absolute path. let path = ModRelativePath::from(file.to_string()); let abs_path = if !is_absolute && !path.is_relative_path().map_err(|err| err.to_string())? { - let filepath = Path::new(&work_dir).join(file.to_string()); + let filepath = Path::new(&work_dir).join(file); match filepath.canonicalize() { Ok(path) => Some(path.adjust_canonicalization()), Err(_) => { @@ -191,7 +191,7 @@ pub fn lookup_compile_unit( } } } - return (vec![file.to_string()], Some(load_opt)); + (vec![file.to_string()], Some(load_opt)) } } } @@ -300,7 +300,7 @@ pub fn get_pkg_list(pkgpath: &str) -> Result> { pkgpath }; - if pkgpath != "." && pkgpath.ends_with(".") { + if pkgpath != "." && pkgpath.ends_with('.') { return Ok(Vec::new()); } @@ -316,21 +316,19 @@ pub fn get_pkg_list(pkgpath: &str) -> Result> { _ => { if Path::new(&pkgpath).is_absolute() { pkgpath.clone() + } else if !pkgpath.contains('/') && !pkgpath.contains('\\') { + pkgpath.replace('.', "/") } else { - if !pkgpath.contains('/') && !pkgpath.contains('\\') { - pkgpath.replace(".", "/") + let pkgroot = + get_pkg_root(cwd.to_str().ok_or(anyhow::anyhow!("cwd path not found"))?) + .unwrap_or_default(); + if !pkgroot.is_empty() { + PathBuf::from(pkgroot) + .join(&pkgpath) + .to_string_lossy() + .to_string() } else { - let pkgroot = - get_pkg_root(&cwd.to_str().ok_or(anyhow::anyhow!("cwd path not found"))?) - .unwrap_or_default(); - if !pkgroot.is_empty() { - PathBuf::from(pkgroot) - .join(&pkgpath) - .to_string_lossy() - .to_string() - } else { - Path::new(&cwd).join(&pkgpath).to_string_lossy().to_string() - } + Path::new(&cwd).join(&pkgpath).to_string_lossy().to_string() } } } @@ -342,18 +340,17 @@ pub fn get_pkg_list(pkgpath: &str) -> Result> { for entry in WalkDir::new(&pkgpath).into_iter().filter_map(|e| e.ok()) { let path = entry.path(); - if !path.is_dir() { - if path.extension().and_then(|ext| ext.to_str()) == Some(KCL_FILE_EXTENSION) - && !path - .file_name() - .map(|name| name.to_string_lossy().starts_with("_")) - .unwrap_or(false) - { - if let Some(dir) = path.parent().map(|p| p.to_string_lossy().to_string()) { - if !dir_map.contains(&dir) { - dir_list.push(dir.clone()); - dir_map.insert(dir); - } + if !path.is_dir() + && path.extension().and_then(|ext| ext.to_str()) == Some(KCL_FILE_EXTENSION) + && !path + .file_name() + .map(|name| name.to_string_lossy().starts_with('_')) + .unwrap_or(false) + { + if let Some(dir) = path.parent().map(|p| p.to_string_lossy().to_string()) { + if !dir_map.contains(&dir) { + dir_list.push(dir.clone()); + dir_map.insert(dir); } } } diff --git a/kclvm/driver/src/tests.rs b/kclvm/driver/src/tests.rs index 53d61bd94..2d218f393 100644 --- a/kclvm/driver/src/tests.rs +++ b/kclvm/driver/src/tests.rs @@ -42,11 +42,11 @@ fn test_expand_input_files_with_kcl_mod() { ]; let got_paths: Vec = expand_input_files(&input_files) .iter() - .map(|s| s.replace("/", "").replace("\\", "")) + .map(|s| s.replace('/', "").replace('\\', "")) .collect(); let expect_paths: Vec = expected_files .iter() - .map(|s| s.replace("/", "").replace("\\", "")) + .map(|s| s.replace('/', "").replace('\\', "")) .collect(); assert_eq!(got_paths, expect_paths); } @@ -172,7 +172,7 @@ fn test_parse_key_value_pair() { ), ]; for (value, pair) in cases { - let result = parse_key_value_pair(&value).unwrap(); + let result = parse_key_value_pair(value).unwrap(); assert_eq!(result.key, pair.key); assert_eq!(result.value, pair.value); } @@ -282,7 +282,7 @@ fn test_lookup_the_nearest_file_dir() { .join("test_data") .join("kpm_metadata"); let result = lookup_the_nearest_file_dir(path.clone(), "kcl.mod"); - assert_eq!(result.is_some(), true); + assert!(result.is_some()); assert_eq!( result.unwrap().display().to_string(), path.canonicalize().unwrap().display().to_string() @@ -290,7 +290,7 @@ fn test_lookup_the_nearest_file_dir() { let main_path = path.join("subdir").join("main.k"); let result = lookup_the_nearest_file_dir(main_path, "kcl.mod"); - assert_eq!(result.is_some(), true); + assert!(result.is_some()); assert_eq!( result.unwrap().display().to_string(), path.canonicalize().unwrap().display().to_string() @@ -327,13 +327,16 @@ fn test_fetch_metadata() { let metadata = fetch_metadata(path.clone()); // Show more information when the test fails. println!("{:?}", metadata); - assert_eq!(metadata.is_err(), false); + assert!(!metadata.is_err()); let pkgs = metadata.unwrap().packages.clone(); assert_eq!(pkgs.len(), 1); assert!(pkgs.get("kcl4").is_some()); - assert_eq!(pkgs.get("kcl4").clone().unwrap().name, "kcl4"); + assert_eq!(pkgs.get("kcl4").unwrap().name, "kcl4"); assert_eq!( - PathBuf::from(pkgs.get("kcl4").unwrap().manifest_path.clone()) + pkgs.get("kcl4") + .unwrap() + .manifest_path + .clone() .canonicalize() .unwrap() .display() diff --git a/kclvm/parser/src/lib.rs b/kclvm/parser/src/lib.rs index bcc6f6c80..32fddf76e 100644 --- a/kclvm/parser/src/lib.rs +++ b/kclvm/parser/src/lib.rs @@ -1,4 +1,4 @@ -// Copyright 2021 The KCL Authors. All rights reserved. +//! Copyright The KCL Authors. All rights reserved. pub mod entry; pub mod file_graph; diff --git a/kclvm/sema/src/builtin/system_module.rs b/kclvm/sema/src/builtin/system_module.rs index 90ea7dcfa..cd5ca61d8 100644 --- a/kclvm/sema/src/builtin/system_module.rs +++ b/kclvm/sema/src/builtin/system_module.rs @@ -1,4 +1,4 @@ -// Copyright The KCL Authors. All rights reserved. +//! Copyright The KCL Authors. All rights reserved. use std::sync::Arc; diff --git a/kclvm/version/src/lib.rs b/kclvm/version/src/lib.rs index db7f10201..4f76524b1 100644 --- a/kclvm/version/src/lib.rs +++ b/kclvm/version/src/lib.rs @@ -1,4 +1,4 @@ -// Copyright 2021 The KCL Authors. All rights reserved. +//! Copyright The KCL Authors. All rights reserved. pub const VERSION: &str = include_str!("./../../../VERSION"); pub const CHECK_SUM: &str = "20ab3eb4b9179219d6837a57f5d35286";