From 0b53d347dc7355303aff1a7d75ac5f58dfb9ec28 Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Fri, 10 Jan 2025 09:59:30 +1100 Subject: [PATCH] add clippy lints --- Cargo.toml | 15 +++++++++++++++ charge-controller-supervisor/src/config.rs | 11 ++++++----- charge-controller-supervisor/src/controller.rs | 2 +- charge-controller-supervisor/src/controller/pl.rs | 12 +++++++----- .../src/controller/tristar.rs | 4 ++-- charge-controller-supervisor/src/main.rs | 2 ++ charge-controller-supervisor/src/web.rs | 3 ++- .../src/web/static_handler.rs | 10 ++++++---- 8 files changed, 41 insertions(+), 18 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fca7975..2b8a16d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,21 @@ version = "1.9.9-pre-20" [workspace.lints.clippy] pedantic = "warn" +branches_sharing_code = "warn" +derive_partial_eq_without_eq = "warn" +equatable_if_let = "warn" +fallible_impl_from = "warn" +large_stack_frames = "warn" +missing_const_for_fn = "warn" +needless_collect = "warn" +needless_pass_by_ref_mut = "warn" +or_fun_call = "warn" +redundant_clone = "warn" +significant_drop_in_scrutinee = "warn" +significant_drop_tightening = "warn" +too_long_first_doc_paragraph = "warn" +trait_duplication_in_bounds = "warn" + cast-possible-truncation = { level = "allow", priority = 1 } cast-precision-loss = { level = "allow", priority = 1 } default-trait-access = { level = "allow", priority = 1 } diff --git a/charge-controller-supervisor/src/config.rs b/charge-controller-supervisor/src/config.rs index 51c48b6..0a2caf0 100644 --- a/charge-controller-supervisor/src/config.rs +++ b/charge-controller-supervisor/src/config.rs @@ -73,7 +73,7 @@ impl ConfigWatcher { async fn overwrite_config(config: Config) -> eyre::Result<()> { let mut h = CONFIG .get() - .ok_or(eyre::eyre!("could not get config"))? + .ok_or_else(|| eyre::eyre!("could not get config"))? .write() .await; if h.charge_controllers != config.charge_controllers @@ -82,6 +82,7 @@ async fn overwrite_config(config: Config) -> eyre::Result<()> { log::warn!("charge controller configuration changed on disk; please restart"); } *h = config; + drop(h); Ok(()) } @@ -162,7 +163,7 @@ impl ConfigStorage { } } -#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Default)] #[serde(default)] pub struct Config { pub primary_charge_controller: String, @@ -183,7 +184,7 @@ impl Config { } } -#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] pub struct ChargeControllerConfig { pub name: String, pub watch_interval_seconds: u64, @@ -193,13 +194,13 @@ pub struct ChargeControllerConfig { pub transport: Transport, } -#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] pub enum ChargeControllerVariant { Tristar, Pl { timeout_milliseconds: u64 }, } -#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] #[serde(rename_all = "lowercase")] pub enum Transport { Serial { port: String, baud_rate: u32 }, diff --git a/charge-controller-supervisor/src/controller.rs b/charge-controller-supervisor/src/controller.rs index cb9d465..c8563c7 100644 --- a/charge-controller-supervisor/src/controller.rs +++ b/charge-controller-supervisor/src/controller.rs @@ -118,7 +118,7 @@ impl Controller { Ok(()) } - pub fn timeout_interval(&self) -> std::time::Duration { + pub const fn timeout_interval(&self) -> std::time::Duration { self.interval } diff --git a/charge-controller-supervisor/src/controller/pl.rs b/charge-controller-supervisor/src/controller/pl.rs index 724a5c5..0204317 100644 --- a/charge-controller-supervisor/src/controller/pl.rs +++ b/charge-controller-supervisor/src/controller/pl.rs @@ -260,7 +260,7 @@ impl Pli { Ok(()) } - fn flush(&mut self) -> eyre::Result<()> { + fn flush(&self) -> eyre::Result<()> { self.port.clear(tokio_serial::ClearBuffer::All)?; Ok(()) } @@ -286,9 +286,11 @@ impl Pli { } } - Err(last_err.unwrap_or(eyre::eyre!( - "no error was stored in read_ram_with_retries: this should be unreachable??" - ))) + Err(last_err.unwrap_or_else(|| { + eyre::eyre!( + "no error was stored in read_ram_with_retries: this should be unreachable??" + ) + })) } async fn read_ram_single(&mut self, address: T) -> eyre::Result @@ -398,6 +400,6 @@ impl From for u8 { } } -fn command(operation: u8, address: u8, data: u8) -> [u8; 4] { +const fn command(operation: u8, address: u8, data: u8) -> [u8; 4] { [operation, address, data, !operation] } diff --git a/charge-controller-supervisor/src/controller/tristar.rs b/charge-controller-supervisor/src/controller/tristar.rs index a2cd697..bd6e830 100644 --- a/charge-controller-supervisor/src/controller/tristar.rs +++ b/charge-controller-supervisor/src/controller/tristar.rs @@ -216,7 +216,7 @@ impl ChargeStateGauges { } } - fn zero_all(&mut self) { + fn zero_all(&self) { self.start.set(0); self.night_check.set(0); self.disconnect.set(0); @@ -230,7 +230,7 @@ impl ChargeStateGauges { self.unknown.set(0); } - fn set(&mut self, state: ChargeState) { + fn set(&self, state: ChargeState) { match state { ChargeState::Start => { self.zero_all(); diff --git a/charge-controller-supervisor/src/main.rs b/charge-controller-supervisor/src/main.rs index 2748c57..da2e361 100644 --- a/charge-controller-supervisor/src/main.rs +++ b/charge-controller-supervisor/src/main.rs @@ -131,6 +131,8 @@ async fn watch(args: Args) -> eyre::Result<()> { primary.set_tx_to_secondary(follow_voltage_tx.clone()); } + drop(config); + let controller_tasks = futures::stream::FuturesUnordered::new(); for controller in controllers { controller_tasks.push(run_loop(controller)); diff --git a/charge-controller-supervisor/src/web.rs b/charge-controller-supervisor/src/web.rs index fd315bf..3a3a9a6 100644 --- a/charge-controller-supervisor/src/web.rs +++ b/charge-controller-supervisor/src/web.rs @@ -87,7 +87,8 @@ async fn all_interfaces( let mut data = Vec::new(); for (k, v) in &state.map { - if let Some(v) = v.read().await.as_ref() { + let v = v.read().await; + if let Some(v) = v.as_ref() { data.push((k.clone(), v.common().clone())); } } diff --git a/charge-controller-supervisor/src/web/static_handler.rs b/charge-controller-supervisor/src/web/static_handler.rs index a36c2dd..9a8f2d7 100644 --- a/charge-controller-supervisor/src/web/static_handler.rs +++ b/charge-controller-supervisor/src/web/static_handler.rs @@ -51,10 +51,12 @@ impl Handler for UiStatic { data: v.contents().to_vec(), name: p, }) - .or(UI_DIR_FILES.get_file(&plus_index).map(|v| RawHtml { - data: v.contents().to_vec(), - name: plus_index, - })); + .or_else(|| { + UI_DIR_FILES.get_file(&plus_index).map(|v| RawHtml { + data: v.contents().to_vec(), + name: plus_index, + }) + }); file.respond_to(req).or_forward((data, Status::NotFound)) } }