Skip to content

Commit

Permalink
refactor: run cargo clippy --fix at the kclvm_tools crate. (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
Peefy committed Nov 21, 2022
1 parent cb0bb0a commit 0066ff5
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 73 deletions.
2 changes: 1 addition & 1 deletion kclvm/tools/src/format/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use pretty_assertions::assert_eq;

const FILE_INPUT_SUFFIX: &str = ".input";
const FILE_OUTPUT_SUFFIX: &str = ".golden";
const TEST_CASES: &[&'static str; 18] = &[
const TEST_CASES: &[&str; 18] = &[
"assert",
"check",
"blankline",
Expand Down
2 changes: 1 addition & 1 deletion kclvm/tools/src/langserver/go_to_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ use kclvm_error::Position;

/// Get the definition of an identifier.
pub fn go_to_def(pos: Position) -> Option<Position> {
return Some(pos);
Some(pos)
}
14 changes: 7 additions & 7 deletions kclvm/tools/src/langserver/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(dead_code)]

use crate::util;
use anyhow::Result;
use kclvm_error::Position;
Expand Down Expand Up @@ -29,12 +31,10 @@ pub fn word_at_pos(pos: Position) -> Option<String> {
if pos.line >= lines.len() as u64 {
return None;
}
if pos.column.is_none() {
return None;
}
pos.column?;
let col = pos.column.unwrap();
let line_words = line_to_words(lines[pos.line as usize].to_string());
if line_words.len() == 0
if line_words.is_empty()
|| col < line_words.first().unwrap().startpos
|| col >= line_words.last().unwrap().endpos
{
Expand All @@ -45,7 +45,7 @@ pub fn word_at_pos(pos: Position) -> Option<String> {
return Some(line_word.word);
}
}
return None;
None
}

pub fn read_file(path: &String) -> Result<String> {
Expand Down Expand Up @@ -81,7 +81,7 @@ pub fn line_to_words(text: String) -> Vec<LineWord> {
words.push(LineWord {
startpos: start_pos as u64,
endpos: i as u64,
word: chars[start_pos..i].into_iter().collect::<String>().clone(),
word: chars[start_pos..i].iter().collect::<String>().clone(),
});
}
// Reset the start position.
Expand Down Expand Up @@ -124,7 +124,7 @@ pub fn match_word(path: String, name: String) -> Vec<Position> {
}
Err(_) => {}
}
return res;
res
}

// Convert pos format
Expand Down
20 changes: 10 additions & 10 deletions kclvm/tools/src/langserver/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,32 +96,32 @@ mod tests {
let path_prefix = "./src/langserver/".to_string();
let datas = vec![
Position {
filename: (path_prefix.clone() + "test_data/inherit.k").to_string(),
filename: (path_prefix.clone() + "test_data/inherit.k"),
line: 0,
column: Some(0),
},
Position {
filename: (path_prefix.clone() + "test_data/inherit.k").to_string(),
filename: (path_prefix.clone() + "test_data/inherit.k"),
line: 1,
column: Some(5),
},
Position {
filename: (path_prefix.clone() + "test_data/inherit.k").to_string(),
filename: (path_prefix.clone() + "test_data/inherit.k"),
line: 3,
column: Some(7),
},
Position {
filename: (path_prefix.clone() + "test_data/inherit.k").to_string(),
filename: (path_prefix.clone() + "test_data/inherit.k"),
line: 3,
column: Some(10),
},
Position {
filename: (path_prefix.clone() + "test_data/inherit.k").to_string(),
filename: (path_prefix.clone() + "test_data/inherit.k"),
line: 4,
column: Some(8),
},
Position {
filename: (path_prefix.clone() + "test_data/inherit.k").to_string(),
filename: (path_prefix + "test_data/inherit.k"),
line: 4,
column: Some(100),
},
Expand Down Expand Up @@ -176,17 +176,17 @@ mod tests {
let mut mp = langserver::word_map::WorkSpaceWordMap::new(path);
mp.build();
let _res = fs::rename(
"./src/langserver/test_data/test_word_workspace_map/inherit_pkg.k".to_string(),
"./src/langserver/test_data/test_word_workspace_map/inherit_bak.k".to_string(),
"./src/langserver/test_data/test_word_workspace_map/inherit_pkg.k",
"./src/langserver/test_data/test_word_workspace_map/inherit_bak.k",
);
mp.rename_file(
"./src/langserver/test_data/test_word_workspace_map/inherit_pkg.k".to_string(),
"./src/langserver/test_data/test_word_workspace_map/inherit_bak.k".to_string(),
);
mp.delete_file("./src/langserver/test_data/test_word_workspace_map/inherit.k".to_string());
let _res = fs::rename(
"./src/langserver/test_data/test_word_workspace_map/inherit_bak.k".to_string(),
"./src/langserver/test_data/test_word_workspace_map/inherit_pkg.k".to_string(),
"./src/langserver/test_data/test_word_workspace_map/inherit_bak.k",
"./src/langserver/test_data/test_word_workspace_map/inherit_pkg.k",
);

let except = vec![Position {
Expand Down
10 changes: 4 additions & 6 deletions kclvm/tools/src/langserver/word_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ pub struct FileWordMap {

impl FileWordMap {
pub fn new(file_name: String) -> Self {
let mut map = FileWordMap {
Self {
file_name,
word_map: HashMap::new(),
};
map
}
}

// Clear records
Expand Down Expand Up @@ -64,11 +63,10 @@ pub struct WorkSpaceWordMap {

impl WorkSpaceWordMap {
pub fn new(path: String) -> Self {
let mut map = WorkSpaceWordMap {
Self {
path,
file_map: HashMap::new(),
};
map
}
}

// when user edit a file, the filemap of this file need to rebuild
Expand Down
2 changes: 1 addition & 1 deletion kclvm/tools/src/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub fn lint_files(
opts: Option<LoadProgramOptions>,
) -> (IndexSet<Diagnostic>, IndexSet<Diagnostic>) {
// Parse AST program.
let mut program = load_program(&files, opts).unwrap();
let mut program = load_program(files, opts).unwrap();
let scope = resolve_program(&mut program);
let (mut errs, mut warnings) = (IndexSet::new(), IndexSet::new());
for diag in &scope.diagnostics {
Expand Down
2 changes: 1 addition & 1 deletion kclvm/tools/src/lint/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::lint_files;

#[test]
fn test_lint() {
let (_, warnings) = lint_files(&vec!["./src/lint/test_data/lint.k"], None);
let (_, warnings) = lint_files(&["./src/lint/test_data/lint.k"], None);
let msgs = [
"Importstmt should be placed at the top of the module",
"Module 'a' is reimported multiple times",
Expand Down
4 changes: 2 additions & 2 deletions kclvm/tools/src/util/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Loader<serde_json::Value> for DataLoader {
/// Load data into Json value.
fn load(&self) -> Result<serde_json::Value> {
let v = match self.kind {
LoaderKind::JSON => serde_json::from_str(&self.get_data())
LoaderKind::JSON => serde_json::from_str(self.get_data())
.with_context(|| format!("Failed to String '{}' to Json", self.get_data()))?,
_ => {
bail!("Failed to String to Json Value")
Expand All @@ -71,7 +71,7 @@ impl Loader<serde_yaml::Value> for DataLoader {
/// Load data into Yaml value.
fn load(&self) -> Result<serde_yaml::Value> {
let v = match self.kind {
LoaderKind::YAML => serde_yaml::from_str(&self.get_data())
LoaderKind::YAML => serde_yaml::from_str(self.get_data())
.with_context(|| format!("Failed to String '{}' to Yaml", self.get_data()))?,
_ => {
bail!("Failed to String to Yaml Value")
Expand Down
19 changes: 9 additions & 10 deletions kclvm/tools/src/util/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use anyhow::{Context, Result};

const CARGO_DIR: &str = env!("CARGO_MANIFEST_DIR");
const REL_PATH: &str = "src/util/test_datas";
const FILE_TEST_CASES: &'static [&'static str] = &["test"];
const FILE_TEST_CASES: &[&str] = &["test"];

const FILE_EXTENSIONS: &'static [&'static str] = &[".json", ".yaml"];
const FILE_EXTENSIONS: &[&str] = &[".json", ".yaml"];

const JSON_STR_TEST_CASES: &'static [&'static str] = &[r#"{
const JSON_STR_TEST_CASES: &[&str] = &[r#"{
"name": "John Doe",
"age": 43,
"address": {
Expand All @@ -22,7 +22,7 @@ const JSON_STR_TEST_CASES: &'static [&'static str] = &[r#"{
}
"#];

const YAML_STR_TEST_CASES: &'static [&'static str] = &[r#"languages:
const YAML_STR_TEST_CASES: &[&str] = &[r#"languages:
- Ruby
- Perl
- Python
Expand Down Expand Up @@ -55,13 +55,12 @@ mod test_loader {

fn data_loader_from_file(loader_kind: LoaderKind, file_path: &str) -> DataLoader {
let test_case_path = construct_full_path(file_path).unwrap();
let data_loader = DataLoader::new_with_file_path(loader_kind, &test_case_path).unwrap();
data_loader

DataLoader::new_with_file_path(loader_kind, &test_case_path).unwrap()
}

fn data_loader_from_str(loader_kind: LoaderKind, s: &str) -> DataLoader {
let data_loader = DataLoader::new_with_str(loader_kind, &s).unwrap();
data_loader
DataLoader::new_with_str(loader_kind, s).unwrap()
}

#[test]
Expand Down Expand Up @@ -93,7 +92,7 @@ mod test_loader {
#[test]
fn test_new_with_str_json() {
for test_case in JSON_STR_TEST_CASES {
let json_loader = data_loader_from_str(LoaderKind::JSON, &test_case);
let json_loader = data_loader_from_str(LoaderKind::JSON, test_case);
assert_eq!(json_loader.get_data(), *test_case);
}
}
Expand Down Expand Up @@ -124,7 +123,7 @@ websites:
#[test]
fn test_new_with_str_yaml() {
for test_case in YAML_STR_TEST_CASES {
let yaml_loader = data_loader_from_str(LoaderKind::JSON, &test_case);
let yaml_loader = data_loader_from_str(LoaderKind::JSON, test_case);
assert_eq!(yaml_loader.get_data(), *test_case);
}
}
Expand Down
18 changes: 9 additions & 9 deletions kclvm/tools/src/vet/expr_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ impl ExprBuilder {
match self.loader.get_kind() {
LoaderKind::JSON => {
let value = <DataLoader as Loader<serde_json::Value>>::load(&self.loader)
.with_context(|| format!("Failed to Load JSON"))?;
.with_context(|| "Failed to Load JSON".to_string())?;
Ok(self
.generate(&value, &schema_name)
.with_context(|| format!("Failed to Load JSON"))?)
.with_context(|| "Failed to Load JSON".to_string())?)
}
LoaderKind::YAML => {
let value = <DataLoader as Loader<serde_yaml::Value>>::load(&self.loader)
.with_context(|| format!("Failed to Load YAML"))?;
.with_context(|| "Failed to Load YAML".to_string())?;
Ok(self
.generate(&value, &schema_name)
.with_context(|| format!("Failed to Load YAML"))?)
.with_context(|| "Failed to Load YAML".to_string())?)
}
}
}
Expand Down Expand Up @@ -123,7 +123,7 @@ impl ExprGenerator<serde_yaml::Value> for ExprBuilder {
for j_arr_item in j_arr {
j_arr_ast_nodes.push(
self.generate(j_arr_item, schema_name)
.with_context(|| format!("Failed to Load Validated File"))?,
.with_context(|| "Failed to Load Validated File".to_string())?,
);
}
Ok(node_ref!(Expr::List(ListExpr {
Expand All @@ -138,10 +138,10 @@ impl ExprGenerator<serde_yaml::Value> for ExprBuilder {
// The configuration builder already in the schema no longer needs a schema name
let k = self
.generate(k, &None)
.with_context(|| format!("Failed to Load Validated File"))?;
.with_context(|| "Failed to Load Validated File".to_string())?;
let v = self
.generate(v, &None)
.with_context(|| format!("Failed to Load Validated File"))?;
.with_context(|| "Failed to Load Validated File".to_string())?;

let config_entry = node_ref!(ConfigEntry {
key: Some(k),
Expand Down Expand Up @@ -246,7 +246,7 @@ impl ExprGenerator<serde_json::Value> for ExprBuilder {
for j_arr_item in j_arr {
j_arr_ast_nodes.push(
self.generate(j_arr_item, schema_name)
.with_context(|| format!("Failed to Load Validated File"))?,
.with_context(|| "Failed to Load Validated File".to_string())?,
);
}
Ok(node_ref!(Expr::List(ListExpr {
Expand All @@ -266,7 +266,7 @@ impl ExprGenerator<serde_json::Value> for ExprBuilder {
};
let v = self
.generate(v, &None)
.with_context(|| format!("Failed to Load Validated File"))?;
.with_context(|| "Failed to Load Validated File".to_string())?;

let config_entry = node_ref!(ConfigEntry {
key: Some(node_ref!(Expr::StringLit(k))),
Expand Down

0 comments on commit 0066ff5

Please sign in to comment.