From 21253297e4a11a1d9f9c5069578cf9c69a3de31b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Tj=C3=A4der?= Date: Tue, 13 Oct 2020 14:16:33 +0000 Subject: [PATCH] Implement all clippy suggestions --- build.rs | 1 - macros/src/analyze.rs | 2 +- macros/src/check.rs | 6 +++--- macros/src/codegen/idle.rs | 2 +- macros/src/codegen/init.rs | 14 ++++++-------- macros/src/codegen/post_init.rs | 2 +- macros/src/codegen/software_tasks.rs | 2 +- macros/src/codegen/timer_queue.rs | 4 ++-- macros/src/codegen/util.rs | 21 +++++++++++++-------- src/export.rs | 21 +++++++++++++++++++++ src/tq.rs | 15 ++++++++++++--- 11 files changed, 61 insertions(+), 29 deletions(-) diff --git a/build.rs b/build.rs index fee1a6a5a6..e46837f07e 100644 --- a/build.rs +++ b/build.rs @@ -1,5 +1,4 @@ use std::env; -use version_check; fn main() { let target = env::var("TARGET").unwrap(); diff --git a/macros/src/analyze.rs b/macros/src/analyze.rs index e0231a2e2d..2301167b02 100644 --- a/macros/src/analyze.rs +++ b/macros/src/analyze.rs @@ -28,7 +28,7 @@ pub fn app(analysis: P, app: &App) -> P { let priorities = app .software_tasks .values() - .filter_map(|task| Some(task.args.priority)) + .map(|task| task.args.priority) .collect::>(); if !priorities.is_empty() { diff --git a/macros/src/check.rs b/macros/src/check.rs index 5a1d3aff67..cfc18d3de5 100644 --- a/macros/src/check.rs +++ b/macros/src/check.rs @@ -44,9 +44,9 @@ pub fn app<'a>(app: &'a App, _analysis: &Analysis) -> parse::Result> { let priorities = app .software_tasks .iter() - .filter_map(|(name, task)| { + .map(|(name, task)| { first = Some(name); - Some(task.args.priority) + task.args.priority }) .collect::>(); @@ -97,7 +97,7 @@ pub fn app<'a>(app: &'a App, _analysis: &Analysis) -> parse::Result> { }, "peripherals" => match v { - CustomArg::Bool(x) => peripherals = if *x { true } else { false }, + CustomArg::Bool(x) => peripherals = *x, _ => { return Err(parse::Error::new( k.span(), diff --git a/macros/src/codegen/idle.rs b/macros/src/codegen/idle.rs index 5e73329f06..72c42a3587 100644 --- a/macros/src/codegen/idle.rs +++ b/macros/src/codegen/idle.rs @@ -28,7 +28,7 @@ pub fn codegen( // call_idle TokenStream2, ) { - if app.idles.len() > 0 { + if !app.idles.is_empty() { let idle = &app.idles.first().unwrap(); let mut needs_lt = false; let mut mod_app = None; diff --git a/macros/src/codegen/init.rs b/macros/src/codegen/init.rs index 465a927d79..1746bff601 100644 --- a/macros/src/codegen/init.rs +++ b/macros/src/codegen/init.rs @@ -8,12 +8,7 @@ use crate::{ codegen::{locals, module, resources_struct, util}, }; -/// Generates support code for `#[init]` functions -pub fn codegen( - app: &App, - analysis: &Analysis, - extra: &Extra, -) -> ( +type CodegenResult = ( // mod_app_idle -- the `${init}Resources` constructor Option, // root_init -- items that must be placed in the root of the crate: @@ -28,8 +23,11 @@ pub fn codegen( Vec, // call_init -- the call to the user `#[init]` if there's one Option, -) { - if app.inits.len() > 0 { +); + +/// Generates support code for `#[init]` functions +pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> CodegenResult { + if !app.inits.is_empty() { let init = &app.inits.first().unwrap(); let mut needs_lt = false; let name = &init.name; diff --git a/macros/src/codegen/post_init.rs b/macros/src/codegen/post_init.rs index c35c697635..329d700e2d 100644 --- a/macros/src/codegen/post_init.rs +++ b/macros/src/codegen/post_init.rs @@ -9,7 +9,7 @@ pub fn codegen(app: &App, analysis: &Analysis) -> Vec { let mut stmts = vec![]; // Initialize late resources - if analysis.late_resources.len() > 0 { + if !analysis.late_resources.is_empty() { // BTreeSet wrapped in a vector for name in analysis.late_resources.first().unwrap() { // If it's live diff --git a/macros/src/codegen/software_tasks.rs b/macros/src/codegen/software_tasks.rs index 9918dea10f..17d1e6da49 100644 --- a/macros/src/codegen/software_tasks.rs +++ b/macros/src/codegen/software_tasks.rs @@ -56,7 +56,7 @@ pub fn codegen( pub static mut #fq: #fq_ty = #fq_expr; )); - let ref elems = (0..cap) + let elems = &(0..cap) .map(|_| quote!(core::mem::MaybeUninit::uninit())) .collect::>(); diff --git a/macros/src/codegen/timer_queue.rs b/macros/src/codegen/timer_queue.rs index c898a7fd55..63d72dd9ef 100644 --- a/macros/src/codegen/timer_queue.rs +++ b/macros/src/codegen/timer_queue.rs @@ -26,7 +26,7 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec>(); - let doc = format!("Tasks that can be scheduled"); + let doc = "Tasks that can be scheduled".to_string(); items.push(quote!( #[doc = #doc] #[allow(non_camel_case_types)] @@ -41,7 +41,7 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec Ident { pub fn is_exception(name: &Ident) -> bool { let s = name.to_string(); - match &*s { - "MemoryManagement" | "BusFault" | "UsageFault" | "SecureFault" | "SVCall" - | "DebugMonitor" | "PendSV" | "SysTick" => true, - - _ => false, - } + matches!( + &*s, + "MemoryManagement" + | "BusFault" + | "UsageFault" + | "SecureFault" + | "SVCall" + | "DebugMonitor" + | "PendSV" + | "SysTick" + ) } /// Generates a pre-reexport identifier for the "late resources" struct @@ -209,7 +214,7 @@ pub fn rq_ident(priority: u8) -> Ident { /// Generates an identifier for the `enum` of `schedule`-able tasks pub fn schedule_t_ident() -> Ident { - Ident::new(&format!("SCHED_T"), Span::call_site()) + Ident::new(&"SCHED_T".to_string(), Span::call_site()) } /// Generates an identifier for the `enum` of `spawn`-able tasks @@ -229,5 +234,5 @@ pub fn suffixed(name: &str) -> Ident { /// /// At most there is one timer queue pub fn tq_ident() -> Ident { - Ident::new(&format!("TQ"), Span::call_site()) + Ident::new(&"TQ".to_string(), Span::call_site()) } diff --git a/src/export.rs b/src/export.rs index 27f7f5fbf8..72d954ab14 100644 --- a/src/export.rs +++ b/src/export.rs @@ -72,6 +72,11 @@ pub struct Priority { } impl Priority { + /// Create a new Priority + /// + /// # Safety + /// + /// Will overwrite the current Priority #[inline(always)] pub unsafe fn new(value: u8) -> Self { Priority { @@ -79,12 +84,14 @@ impl Priority { } } + /// Change the current priority to `value` // These two methods are used by `lock` (see below) but can't be used from the RTIC application #[inline(always)] fn set(&self, value: u8) { self.inner.set(value) } + /// Get the current priority #[inline(always)] fn get(&self) -> u8 { self.inner.get() @@ -105,6 +112,13 @@ where { } +/// Lock the resource proxy by setting the BASEPRI +/// and running the closure with interrupt::free +/// +/// # Safety +/// +/// Writing to the BASEPRI +/// Dereferencing a raw pointer #[cfg(armv7m)] #[inline(always)] pub unsafe fn lock( @@ -135,6 +149,13 @@ pub unsafe fn lock( } } +/// Lock the resource proxy by setting the PRIMASK +/// and running the closure with interrupt::free +/// +/// # Safety +/// +/// Writing to the PRIMASK +/// Dereferencing a raw pointer #[cfg(not(armv7m))] #[inline(always)] pub unsafe fn lock( diff --git a/src/tq.rs b/src/tq.rs index 9300dbfcb6..b2a84c8593 100644 --- a/src/tq.rs +++ b/src/tq.rs @@ -24,18 +24,26 @@ where N: ArrayLength>, T: Copy, { + /// # Safety + /// + /// Writing to memory with a transmute in order to enable + /// interrupts of the SysTick timer + /// + /// Enqueue a task without checking if it is full #[inline] pub unsafe fn enqueue_unchecked(&mut self, nr: NotReady) { let mut is_empty = true; - if self + // Check if the top contains a non-empty element and if that element is + // greater than nr + let if_heap_max_greater_than_nr = self .0 .peek() .map(|head| { is_empty = false; nr.instant < head.instant }) - .unwrap_or(true) - { + .unwrap_or(true); + if if_heap_max_greater_than_nr { if is_empty { mem::transmute::<_, SYST>(()).enable_interrupt(); } @@ -47,6 +55,7 @@ where self.0.push_unchecked(nr); } + /// Dequeue a task from the TimerQueue #[inline] pub fn dequeue(&mut self) -> Option<(T, u8)> { unsafe {