Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: cargo clippy for api, compiler, config and sema crates #1013

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions kclvm/api/src/capi_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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('\\', "")
}
},
);
Expand All @@ -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('\\', "")
}
},
);
Expand Down
4 changes: 2 additions & 2 deletions kclvm/api/src/service/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
}
}
Expand All @@ -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()),
}
}
Expand Down
28 changes: 14 additions & 14 deletions kclvm/ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,24 @@ impl From<PosTuple> for Pos {
}
}

impl Into<PosTuple> for Pos {
fn into(self) -> PosTuple {
(self.0, self.1, self.2, self.3, self.4)
impl From<Pos> for PosTuple {
fn from(val: Pos) -> Self {
(val.0, val.1, val.2, val.3, val.4)
}
}

impl Into<Range> for Pos {
fn into(self) -> Range {
impl From<Pos> 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),
},
)
}
Expand Down Expand Up @@ -413,7 +413,7 @@ impl Module {
stmts.push(node_ref!(schema_stmt.clone(), stmt.pos()));
}
}
return stmts;
stmts
}

/// Get stmt on position
Expand Down Expand Up @@ -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 {
Expand All @@ -1755,7 +1755,7 @@ impl ToString for Type {
to_str(&param.node, w);
}
}
w.push_str(")");
w.push(')');
if let Some(ret) = &v.ret_ty {
w.push_str(" -> ");
to_str(&ret.node, w);
Expand Down
2 changes: 1 addition & 1 deletion kclvm/ast/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
6 changes: 3 additions & 3 deletions kclvm/ast_pretty/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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(),
);
Expand All @@ -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(),
);
Expand Down
2 changes: 1 addition & 1 deletion kclvm/compiler/src/codegen/error.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
6 changes: 3 additions & 3 deletions kclvm/compiler/src/codegen/llvm/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1562,7 +1562,7 @@ impl<'ctx> LLVMCodeGenContext<'ctx> {
let mut pkg_scopes = self.pkg_scopes.borrow_mut();
let scopes = pkg_scopes
.get_mut(&current_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.
Expand Down Expand Up @@ -2090,7 +2090,7 @@ impl<'ctx> LLVMCodeGenContext<'ctx> {
let mut pkg_scopes = self.pkg_scopes.borrow_mut();
let scopes = pkg_scopes
.get_mut(&current_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();
Expand All @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion kclvm/compiler/src/codegen/llvm/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion kclvm/compiler/src/codegen/llvm/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![]);
Expand Down
2 changes: 1 addition & 1 deletion kclvm/compiler/src/codegen/llvm/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
&[
Expand Down
2 changes: 1 addition & 1 deletion kclvm/compiler/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion kclvm/compiler/src/macros.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion kclvm/compiler/src/value/lambda.rs
Original file line number Diff line number Diff line change
@@ -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";
2 changes: 1 addition & 1 deletion kclvm/compiler/src/value/mod.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand Down
2 changes: 1 addition & 1 deletion kclvm/compiler/src/value/schema.rs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
2 changes: 1 addition & 1 deletion kclvm/config/src/cache.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion kclvm/config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion kclvm/config/src/modfile.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion kclvm/config/src/settings.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down
4 changes: 2 additions & 2 deletions kclvm/driver/src/kpm_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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() {
Expand Down
53 changes: 25 additions & 28 deletions kclvm/driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_) => {
Expand Down Expand Up @@ -191,7 +191,7 @@ pub fn lookup_compile_unit(
}
}
}
return (vec![file.to_string()], Some(load_opt));
(vec![file.to_string()], Some(load_opt))
}
}
}
Expand Down Expand Up @@ -300,7 +300,7 @@ pub fn get_pkg_list(pkgpath: &str) -> Result<Vec<String>> {
pkgpath
};

if pkgpath != "." && pkgpath.ends_with(".") {
if pkgpath != "." && pkgpath.ends_with('.') {
return Ok(Vec::new());
}

Expand All @@ -316,21 +316,19 @@ pub fn get_pkg_list(pkgpath: &str) -> Result<Vec<String>> {
_ => {
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()
}
}
}
Expand All @@ -342,18 +340,17 @@ pub fn get_pkg_list(pkgpath: &str) -> Result<Vec<String>> {

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);
}
}
}
Expand Down
Loading
Loading