Skip to content

Commit

Permalink
refactor: clippy (#1223)
Browse files Browse the repository at this point in the history
* refactor: clippy

* refactor: review
  • Loading branch information
hugocaillard authored Oct 26, 2023
1 parent f0941f8 commit faa5a3e
Show file tree
Hide file tree
Showing 66 changed files with 1,157 additions and 1,401 deletions.
4 changes: 1 addition & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
"cargo",
"clippy",
"--package=clarinet-cli",
"--message-format=json",
"--",
"--no-deps"
"--message-format=json"
]
}
2 changes: 2 additions & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type-complexity-threshold = 1000
too-many-arguments-threshold = 12
22 changes: 7 additions & 15 deletions components/clarinet-cli/src/deployments/mod.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
pub mod types;
mod ui;

use std::fs::{self};
use std::path::PathBuf;
pub use ui::start_ui;

use hiro_system_kit;

use clarinet_deployments::types::{DeploymentGenerationArtifacts, DeploymentSpecification};

use clarinet_files::{FileLocation, ProjectManifest};

use clarinet_files::chainhook_types::StacksNetwork;

use serde_yaml;

use std::fs::{self};

use std::path::PathBuf;
use clarinet_files::{FileLocation, ProjectManifest};

#[derive(Deserialize, Debug)]
pub struct Balance {
Expand Down Expand Up @@ -79,7 +71,7 @@ fn get_deployments_files(
let is_extension_valid = file
.extension()
.and_then(|ext| ext.to_str())
.and_then(|ext| Some(ext == "yml" || ext == "yaml"));
.map(|ext| ext == "yml" || ext == "yaml");

if let Some(true) = is_extension_valid {
let relative_path = file.clone();
Expand All @@ -99,13 +91,13 @@ pub fn write_deployment(
if target_location.exists() && prompt_override {
println!(
"Deployment {} already exists.\n{}?",
target_location.to_string(),
target_location,
yellow!("Overwrite [Y/n]")
);
let mut buffer = String::new();
std::io::stdin().read_line(&mut buffer).unwrap();
if buffer.starts_with("n") {
return Err(format!("deployment update aborted"));
if buffer.starts_with('n') {
return Err("deployment update aborted".to_string());
}
}

Expand Down
4 changes: 2 additions & 2 deletions components/clarinet-cli/src/deployments/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ impl DeploymentSynthesis {
Ok(res) => res,
Err(err) => panic!("unable to serialize deployment {}", err),
};
return DeploymentSynthesis {
DeploymentSynthesis {
total_cost,
blocks_count,
content,
};
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion components/clarinet-cli/src/deployments/ui/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[allow(dead_code)]
mod app;
#[allow(dead_code)]

#[allow(clippy::module_inception)]
mod ui;

use app::App;
Expand Down
23 changes: 12 additions & 11 deletions components/clarinet-cli/src/frontend/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@ use super::telemetry::{telemetry_report_event, DeveloperUsageDigest, DeveloperUs
/// For Clarinet documentation, refer to https://docs.hiro.so/clarinet/introduction.
/// Report any issues here https://github.com/hirosystems/clarinet/issues/new.
#[derive(Parser, PartialEq, Clone, Debug)]
#[clap(version = option_env!("CARGO_PKG_VERSION").expect("Unable to detect version"), name = "clarinet", bin_name = "clarinet")]
#[clap(version = env!("CARGO_PKG_VERSION"), name = "clarinet", bin_name = "clarinet")]
struct Opts {
#[clap(subcommand)]
command: Command,
}

#[allow(clippy::upper_case_acronyms)]
#[derive(Subcommand, PartialEq, Clone, Debug)]
enum Command {
/// Create and scaffold a new project
Expand Down Expand Up @@ -123,6 +124,7 @@ enum Requirements {
AddRequirement(AddRequirement),
}

#[allow(clippy::enum_variant_names)]
#[derive(Subcommand, PartialEq, Clone, Debug)]
#[clap(bin_name = "deployment", aliases = &["deployment"])]
enum Deployments {
Expand All @@ -137,6 +139,7 @@ enum Deployments {
ApplyDeployment(ApplyDeployment),
}

#[allow(clippy::enum_variant_names)]
#[derive(Subcommand, PartialEq, Clone, Debug)]
#[clap(bin_name = "chainhook", aliases = &["chainhook"])]
enum Chainhooks {
Expand Down Expand Up @@ -663,7 +666,7 @@ pub fn main() {
#[cfg(feature = "telemetry")]
telemetry_report_event(DeveloperUsageEvent::NewProject(DeveloperUsageDigest::new(
&project_opts.name,
&vec![],
&[],
)));
}
}
Expand Down Expand Up @@ -834,9 +837,9 @@ pub fn main() {
println!("{}", yellow!("Continue [Y/n]?"));
let mut buffer = String::new();
std::io::stdin().read_line(&mut buffer).unwrap();
if !buffer.starts_with("Y")
&& !buffer.starts_with("y")
&& !buffer.starts_with("\n")
if !buffer.starts_with('Y')
&& !buffer.starts_with('y')
&& !buffer.starts_with('\n')
{
println!("Deployment aborted");
std::process::exit(1);
Expand Down Expand Up @@ -1154,8 +1157,7 @@ pub fn main() {
}

if success {
println!("{} Syntax of contract successfully checked", green!("✔"));
return;
println!("{} Syntax of contract successfully checked", green!("✔"))
} else {
std::process::exit(1);
}
Expand Down Expand Up @@ -1394,9 +1396,8 @@ fn load_manifest_or_exit(path: Option<String>) -> ProjectManifest {
}

fn load_manifest_or_warn(path: Option<String>) -> Option<ProjectManifest> {
let manifest_location = get_manifest_location_or_warn(path);
if manifest_location.is_some() {
let manifest = match ProjectManifest::from_location(&manifest_location.unwrap()) {
if let Some(manifest_location) = get_manifest_location_or_warn(path) {
let manifest = match ProjectManifest::from_location(&manifest_location) {
Ok(manifest) => manifest,
Err(message) => {
println!(
Expand Down Expand Up @@ -1584,7 +1585,7 @@ pub fn load_deployment_if_exists(
println!("{}", yellow!("Overwrite? [Y/n]"));
let mut buffer = String::new();
std::io::stdin().read_line(&mut buffer).unwrap();
if buffer.starts_with("n") {
if buffer.starts_with('n') {
Some(load_deployment(manifest, &default_deployment_location))
} else {
default_deployment_location
Expand Down
14 changes: 6 additions & 8 deletions components/clarinet-cli/src/frontend/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ pub struct DeveloperUsageDigest {
}

impl DeveloperUsageDigest {
pub fn new(project_id: &str, team_id: &Vec<String>) -> Self {
pub fn new(project_id: &str, team_id: &[String]) -> Self {
let hashed_project_id = Hash160::from_data(project_id.as_bytes());
let hashed_team_id = Hash160::from_data(team_id.join(",").as_bytes());
Self {
project_id: format!("0x{}", bytes_to_hex(&hashed_project_id.to_bytes().to_vec())),
team_id: format!("0x{}", bytes_to_hex(&hashed_team_id.to_bytes().to_vec())),
project_id: format!("0x{}", bytes_to_hex(hashed_project_id.to_bytes().as_ref())),
team_id: format!("0x{}", bytes_to_hex(hashed_team_id.to_bytes().as_ref())),
}
}
}
Expand All @@ -48,9 +48,7 @@ pub fn telemetry_report_event(event: DeveloperUsageEvent) {
async fn send_event(event: DeveloperUsageEvent) {
let segment_api_key = "Q3xpmFRvy0psXnwBEXErtMBIeabOVjbC";

let clarinet_version = option_env!("CARGO_PKG_VERSION")
.expect("Unable to detect version")
.to_string();
let clarinet_version = env!("CARGO_PKG_VERSION").to_string();
let ci_mode = option_env!("CLARINET_MODE_CI").unwrap_or("0").to_string();
let os = std::env::consts::OS;

Expand Down Expand Up @@ -153,10 +151,10 @@ async fn send_event(event: DeveloperUsageEvent) {
segment_api_key.to_string(),
Message::from(Track {
user: User::UserId {
user_id: format!("0x{}", bytes_to_hex(&user_id.to_bytes().to_vec())),
user_id: format!("0x{}", bytes_to_hex(user_id.to_bytes().as_ref())),
},
event: event_name.into(),
properties: properties,
properties,
..Default::default()
}),
)
Expand Down
2 changes: 1 addition & 1 deletion components/clarinet-cli/src/generate/chainhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ networks:
.expect("unable to retrieve project root");
new_file.append_path(&format!("chainhooks/{}", name))?;
if new_file.exists() {
return Err(format!("{} already exists", new_file.to_string()));
return Err(format!("{} already exists", new_file));
}
let change = FileCreation {
comment: format!("{} chainhooks/{}", green!("Created file"), name),
Expand Down
4 changes: 2 additions & 2 deletions components/clarinet-cli/src/generate/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl GetChangesForNewContract {
new_file.append_path("contracts")?;
new_file.append_path(&name)?;
if new_file.exists() {
return Err(format!("{} already exists", new_file.to_string()));
return Err(format!("{} already exists", new_file));
}
let change = FileCreation {
comment: format!("{} contracts/{}", green!("Created file"), name),
Expand Down Expand Up @@ -121,7 +121,7 @@ describe("example tests", () => {
new_file.append_path("tests")?;
new_file.append_path(&name)?;
if new_file.exists() {
return Err(format!("{} already exists", new_file.to_string()));
return Err(format!("{} already exists", new_file));
}
let change = FileCreation {
comment: format!("{} tests/{}", green!("Created file"), name),
Expand Down
6 changes: 3 additions & 3 deletions components/clarinet-cli/src/integrate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,20 @@ pub fn run_devnet(
.network_config
.as_ref()
.and_then(|c| c.devnet.as_ref())
.and_then(|d| Some(d.working_dir.to_string()))
.map(|d| d.working_dir.to_string())
.ok_or("unable to read settings/Devnet.toml")?;
fs::create_dir_all(&working_dir)
.map_err(|_| format!("unable to create dir {}", working_dir))?;
let mut log_path = PathBuf::from_str(&working_dir)
.map_err(|e| format!("unable to working_dir {}\n{}", working_dir, e.to_string()))?;
.map_err(|e| format!("unable to working_dir {}\n{}", working_dir, e))?;
log_path.push("devnet.log");

let file = OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(log_path)
.map_err(|e| format!("unable to create log file {}", e.to_string()))?;
.map_err(|e| format!("unable to create log file {}", e))?;

let decorator = slog_term::PlainDecorator::new(file);
let drain = slog_term::FullFormat::new(decorator).build().fuse();
Expand Down
8 changes: 3 additions & 5 deletions components/clarinet-cli/src/lsp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ use clarity_repl::clarity::vm::diagnostic::{
};
use crossbeam_channel::unbounded;
use std::sync::mpsc;
use tokio;
use tower_lsp::lsp_types::{Diagnostic, DiagnosticSeverity, Position, Range};
use tower_lsp::{LspService, Server};

pub fn run_lsp() {
match block_on(do_run_lsp()) {
Err(_e) => std::process::exit(1),
_ => {}
if let Err(_e) = block_on(do_run_lsp()) {
std::process::exit(1)
};
}

Expand Down Expand Up @@ -49,7 +47,7 @@ async fn do_run_lsp() -> Result<(), String> {
}

pub fn clarity_diagnostics_to_tower_lsp_type(
diagnostics: &mut Vec<ClarityDiagnostic>,
diagnostics: &mut [ClarityDiagnostic],
) -> Vec<tower_lsp::lsp_types::Diagnostic> {
let mut dst = vec![];
for d in diagnostics.iter_mut() {
Expand Down
Loading

0 comments on commit faa5a3e

Please sign in to comment.