434: Clippy fixes r=Dirbaio a=Dirbaio

Fixes most clippy lints. 

Added `#![allow(clippy::new_without_default)]` because I really, really don't agree with `new()` being the same as `default()`...


Co-authored-by: Dario Nieuwenhuis <dirbaio@dirbaio.net>
This commit is contained in:
bors[bot] 2021-10-17 23:19:45 +00:00 committed by GitHub
commit 90f6b56cba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 38 additions and 33 deletions

View file

@ -4,6 +4,7 @@
"rust-analyzer.assist.importGranularity": "module",
"rust-analyzer.checkOnSave.allFeatures": false,
"rust-analyzer.checkOnSave.allTargets": false,
"rust-analyzer.checkOnSave.command": "clippy",
"rust-analyzer.cargo.noDefaultFeatures": true,
"rust-analyzer.checkOnSave.noDefaultFeatures": true,
"rust-analyzer.cargo.target": "thumbv7em-none-eabi",

View file

@ -1,4 +1,5 @@
#![no_std]
#![allow(clippy::new_without_default)]
// This mod MUST go first, so that the others see its macros.
pub(crate) mod fmt;

View file

@ -118,6 +118,7 @@ pub fn task(args: TokenStream, item: TokenStream) -> TokenStream {
use #embassy_path::executor::raw::TaskStorage;
#task_fn
type F = #impl_ty;
#[allow(clippy::declare_interior_mutable_const)]
const NEW_TASK: TaskStorage<F> = TaskStorage::new();
static POOL: [TaskStorage<F>; #pool_size] = [NEW_TASK; #pool_size];
unsafe { TaskStorage::spawn_pool(&POOL, move || task(#arg_names)) }

View file

@ -1,4 +1,5 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::new_without_default)]
// This mod MUST go first, so that the others see its macros.
pub(crate) mod fmt;

View file

@ -140,7 +140,7 @@ impl Stack {
self.waker.register(cx.waker());
let timestamp = instant_to_smoltcp(Instant::now());
if let Err(_) = self.iface.poll(&mut self.sockets, timestamp) {
if self.iface.poll(&mut self.sockets, timestamp).is_err() {
// If poll() returns error, it may not be done yet, so poll again later.
cx.waker().wake_by_ref();
return;
@ -152,18 +152,14 @@ impl Stack {
// Print when changed
if old_link_up != self.link_up {
if self.link_up {
info!("Link up!");
} else {
info!("Link down!");
}
info!("link_up = {:?}", self.link_up);
}
if old_link_up || self.link_up {
self.poll_configurator(timestamp)
}
if let Some(poll_at) = self.iface.poll_at(&mut self.sockets, timestamp) {
if let Some(poll_at) = self.iface.poll_at(&self.sockets, timestamp) {
let t = Timer::at(instant_from_smoltcp(poll_at));
pin_mut!(t);
if t.poll(cx).is_ready() {
@ -215,7 +211,7 @@ pub fn init<const ADDR: usize, const SOCK: usize, const NEIGH: usize>(
let mut res = [0u8; 2];
rand(&mut res);
let port = u16::from_le_bytes(res);
if port >= LOCAL_PORT_MIN && port <= LOCAL_PORT_MAX {
if (LOCAL_PORT_MIN..=LOCAL_PORT_MAX).contains(&port) {
break port;
}
};

View file

@ -130,7 +130,7 @@ impl<'a> AsyncBufRead for TcpSocket<'a> {
) -> Poll<io::Result<&'z [u8]>> {
self.with(|socket| match socket.peek(1 << 30) {
// No data ready
Ok(buf) if buf.len() == 0 => {
Ok(buf) if buf.is_empty() => {
socket.register_recv_waker(cx.waker());
Poll::Pending
}

View file

@ -443,7 +443,7 @@ pub trait OptionalPin: Unborrow<Target = Self> + sealed::OptionalPin + Sized {
#[inline]
fn psel_bits(&self) -> u32 {
self.pin().map_or(1u32 << 31, |pin| Pin::psel_bits(pin))
self.pin().map_or(1u32 << 31, Pin::psel_bits)
}
/// Convert from concrete pin type PX_XX to type erased `Option<AnyPin>`.

View file

@ -22,6 +22,7 @@ pub const PIN_COUNT: usize = 48;
#[cfg(not(any(feature = "nrf52833", feature = "nrf52840")))]
pub const PIN_COUNT: usize = 32;
#[allow(clippy::declare_interior_mutable_const)]
const NEW_AW: AtomicWaker = AtomicWaker::new();
static CHANNEL_WAKERS: [AtomicWaker; CHANNEL_COUNT] = [NEW_AW; CHANNEL_COUNT];
static PORT_WAKERS: [AtomicWaker; PIN_COUNT] = [NEW_AW; PIN_COUNT];

View file

@ -4,8 +4,8 @@ pub trait Delay {
type DelayFuture<'a>: Future<Output = ()> + 'a;
/// Future that completes after now + millis
fn delay_ms<'a>(&'a mut self, millis: u64) -> Self::DelayFuture<'a>;
fn delay_ms(&mut self, millis: u64) -> Self::DelayFuture<'_>;
/// Future that completes after now + micros
fn delay_us<'a>(&'a mut self, micros: u64) -> Self::DelayFuture<'a>;
fn delay_us(&mut self, micros: u64) -> Self::DelayFuture<'_>;
}

View file

@ -37,7 +37,7 @@ pub trait Flash {
/// Erases a single page from the flash device.
///
/// address must be a multiple of self.erase_size().
fn erase<'a>(&'a mut self, address: usize) -> Self::ErasePageFuture<'a>;
fn erase(&mut self, address: usize) -> Self::ErasePageFuture<'_>;
/// Returns the total size, in bytes.
/// This is not guaranteed to be a power of 2.

View file

@ -8,7 +8,7 @@ pub trait WaitForHigh {
///
/// If the pin is already high, the future completes immediately.
/// Otherwise, it completes when it becomes high.
fn wait_for_high<'a>(&'a mut self) -> Self::Future<'a>;
fn wait_for_high(&mut self) -> Self::Future<'_>;
}
/// Wait for a pin to become low.
@ -19,7 +19,7 @@ pub trait WaitForLow {
///
/// If the pin is already low, the future completes immediately.
/// Otherwise, it completes when it becomes low.
fn wait_for_low<'a>(&'a mut self) -> Self::Future<'a>;
fn wait_for_low(&mut self) -> Self::Future<'_>;
}
/// Wait for a rising edge (transition from low to high)
@ -27,7 +27,7 @@ pub trait WaitForRisingEdge {
type Future<'a>: Future<Output = ()> + 'a;
/// Wait for a rising edge (transition from low to high)
fn wait_for_rising_edge<'a>(&'a mut self) -> Self::Future<'a>;
fn wait_for_rising_edge(&mut self) -> Self::Future<'_>;
}
/// Wait for a falling edge (transition from high to low)
@ -35,7 +35,7 @@ pub trait WaitForFallingEdge {
type Future<'a>: Future<Output = ()> + 'a;
/// Wait for a falling edge (transition from high to low)
fn wait_for_falling_edge<'a>(&'a mut self) -> Self::Future<'a>;
fn wait_for_falling_edge(&'_ mut self) -> Self::Future<'_>;
}
/// Wait for any edge (any transition, high to low or low to high)
@ -43,5 +43,5 @@ pub trait WaitForAnyEdge {
type Future<'a>: Future<Output = ()> + 'a;
/// Wait for any edge (any transition, high to low or low to high)
fn wait_for_any_edge<'a>(&'a mut self) -> Self::Future<'a>;
fn wait_for_any_edge(&mut self) -> Self::Future<'_>;
}

View file

@ -26,7 +26,7 @@ impl<T: Rng> Random<T> {
Self { rng }
}
pub async fn next_u8<'a>(&'a mut self, range: u8) -> Result<u8, T::Error> {
pub async fn next_u8(&mut self, range: u8) -> Result<u8, T::Error> {
// Lemire's method
let t = (-(range as i8) % (range as i8)) as u8;
loop {
@ -42,7 +42,7 @@ impl<T: Rng> Random<T> {
}
}
pub async fn next_u16<'a>(&'a mut self, range: u16) -> Result<u16, T::Error> {
pub async fn next_u16(&mut self, range: u16) -> Result<u16, T::Error> {
// Lemire's method
let t = (-(range as i16) % (range as i16)) as u16;
loop {
@ -58,7 +58,7 @@ impl<T: Rng> Random<T> {
}
}
pub async fn next_u32<'a>(&'a mut self, range: u32) -> Result<u32, T::Error> {
pub async fn next_u32(&mut self, range: u32) -> Result<u32, T::Error> {
// Lemire's method
let t = (-(range as i32) % (range as i32)) as u32;
loop {

View file

@ -130,7 +130,7 @@ where
/// closed by `recv` until they are all consumed.
///
/// [`close`]: Self::close
pub fn recv<'m>(&'m mut self) -> RecvFuture<'m, M, T, N> {
pub fn recv(&mut self) -> RecvFuture<'_, M, T, N> {
RecvFuture {
channel: self.channel,
}
@ -469,7 +469,7 @@ impl<T, const N: usize> ChannelState<T, N> {
}
Err(message) => {
cx.into_iter()
.for_each(|cx| self.set_senders_waker(&cx.waker()));
.for_each(|cx| self.set_senders_waker(cx.waker()));
Err(TrySendError::Full(message))
}
}
@ -487,7 +487,7 @@ impl<T, const N: usize> ChannelState<T, N> {
fn is_closed_with_context(&mut self, cx: Option<&mut Context<'_>>) -> bool {
if self.closed {
cx.into_iter()
.for_each(|cx| self.set_senders_waker(&cx.waker()));
.for_each(|cx| self.set_senders_waker(cx.waker()));
true
} else {
false

View file

@ -12,6 +12,7 @@ impl<T> UninitCell<T> {
(*self.0.as_ptr()).get()
}
#[allow(clippy::mut_from_ref)]
pub unsafe fn as_mut(&self) -> &mut T {
&mut *self.as_mut_ptr()
}

View file

@ -95,7 +95,7 @@ impl Spawner {
/// fails. This is here to allow conditional use of `defmt::unwrap!`
/// without introducing a `defmt` feature in the `embassy_macros` package,
/// which would require use of `-Z namespaced-features`.
pub fn must_spawn<F>(&self, token: SpawnToken<F>) -> () {
pub fn must_spawn<F>(&self, token: SpawnToken<F>) {
unwrap!(self.spawn(token));
}

View file

@ -39,7 +39,7 @@ impl<R: AsyncBufRead + ?Sized + Unpin> Future for ReadExact<'_, R> {
this.buf[..n].copy_from_slice(&buf[..n]);
Pin::new(&mut this.reader).consume(n);
{
let (_, rest) = mem::replace(&mut this.buf, &mut []).split_at_mut(n);
let (_, rest) = mem::take(&mut this.buf).split_at_mut(n);
this.buf = rest;
}
}

View file

@ -31,7 +31,7 @@ impl<W: AsyncWrite + ?Sized + Unpin> Future for WriteAll<'_, W> {
while !this.buf.is_empty() {
let n = ready!(Pin::new(&mut this.writer).poll_write(cx, this.buf))?;
{
let (_, rest) = mem::replace(&mut this.buf, &[]).split_at(n);
let (_, rest) = mem::take(&mut this.buf).split_at(n);
this.buf = rest;
}
if n == 0 {

View file

@ -3,6 +3,7 @@
#![feature(const_fn_trait_bound)]
#![feature(const_fn_fn_ptr_basics)]
#![feature(type_alias_impl_trait)]
#![allow(clippy::new_without_default)]
// This mod MUST go first, so that the others see its macros.
pub(crate) mod fmt;

View file

@ -13,10 +13,10 @@ pub struct Delay;
impl crate::traits::delay::Delay for Delay {
type DelayFuture<'a> = impl Future<Output = ()> + 'a;
fn delay_ms<'a>(&'a mut self, millis: u64) -> Self::DelayFuture<'a> {
fn delay_ms(&mut self, millis: u64) -> Self::DelayFuture<'_> {
Timer::after(Duration::from_millis(millis))
}
fn delay_us<'a>(&'a mut self, micros: u64) -> Self::DelayFuture<'a> {
fn delay_us(&mut self, micros: u64) -> Self::DelayFuture<'_> {
Timer::after(Duration::from_micros(micros))
}
}

View file

@ -45,6 +45,7 @@ impl<T> Forever<T> {
///
/// Returns a mutable reference to the stored value.
#[inline(always)]
#[allow(clippy::mut_from_ref)]
pub fn put(&'static self, val: T) -> &'static mut T {
if self
.used
@ -63,6 +64,7 @@ impl<T> Forever<T> {
}
#[inline(always)]
#[allow(clippy::mut_from_ref)]
pub fn put_with(&'static self, val: impl FnOnce() -> T) -> &'static mut T {
if self
.used
@ -81,6 +83,7 @@ impl<T> Forever<T> {
}
#[inline(always)]
#[allow(clippy::mut_from_ref)]
pub unsafe fn steal(&'static self) -> &'static mut T {
let p = self.t.get();
let p = (&mut *p).as_mut_ptr();

View file

@ -15,6 +15,6 @@ futures = { version = "0.3.17", default-features = false, features = ["async-awa
log = "0.4.14"
nix = "0.22.1"
libc = "0.2.101"
clap = { version = "3.0.0-beta.4", features = ["derive"] }
clap = { version = "3.0.0-beta.5", features = ["derive"] }
rand_core = { version = "0.6.3", features = ["std"] }
heapless = { version = "0.7.5", default-features = false }

View file

@ -1,6 +1,6 @@
#![feature(type_alias_impl_trait)]
use clap::{AppSettings, Clap};
use clap::Parser;
use embassy::executor::{Executor, Spawner};
use embassy::io::AsyncWriteExt;
use embassy::util::Forever;
@ -18,9 +18,8 @@ static CONFIG_STATIC: Forever<StaticConfigurator> = Forever::new();
static CONFIG_DYNAMIC: Forever<DhcpConfigurator> = Forever::new();
static NET_RESOURCES: Forever<StackResources<1, 2, 8>> = Forever::new();
#[derive(Clap)]
#[derive(Parser)]
#[clap(version = "1.0")]
#[clap(setting = AppSettings::ColoredHelp)]
struct Opts {
/// TAP device name
#[clap(long, default_value = "tap0")]