From d222c1e259772b31046cbaaa43e5c0741d1412ef Mon Sep 17 00:00:00 2001 From: Yuekai Jia Date: Thu, 25 Apr 2024 23:59:32 +0800 Subject: [PATCH] Fix cargo clippy warnings --- api/arceos_posix_api/src/imp/io_mpx/select.rs | 2 +- apps/task/tls/src/main.rs | 9 +++++---- crates/axerrno/src/lib.rs | 2 +- crates/kernel_guard/src/arch/mod.rs | 2 +- crates/linked_list/src/unsafe_list.rs | 6 +++--- crates/timer_list/src/lib.rs | 2 +- modules/axhal/src/arch/x86_64/idt.rs | 1 + modules/axhal/src/platform/aarch64_bsta1000b/mem.rs | 6 ++++-- modules/axhal/src/platform/aarch64_common/boot.rs | 3 ++- modules/axhal/src/platform/aarch64_qemu_virt/mem.rs | 6 ++++-- modules/axhal/src/platform/aarch64_raspi/mem.rs | 6 ++++-- modules/axnet/src/smoltcp_impl/addr.rs | 8 +++++--- modules/axtask/Cargo.toml | 2 +- modules/axtask/src/lib.rs | 3 +++ modules/axtask/src/tests.rs | 2 +- rust-toolchain.toml | 2 +- scripts/make/cargo.mk | 6 ++++-- 17 files changed, 42 insertions(+), 26 deletions(-) diff --git a/api/arceos_posix_api/src/imp/io_mpx/select.rs b/api/arceos_posix_api/src/imp/io_mpx/select.rs index 6986598b5c..792c10d993 100644 --- a/api/arceos_posix_api/src/imp/io_mpx/select.rs +++ b/api/arceos_posix_api/src/imp/io_mpx/select.rs @@ -24,7 +24,7 @@ impl FdSets { let nfds = nfds.min(FD_SETSIZE); let nfds_usizes = nfds.div_ceil(BITS_PER_USIZE); let mut bits = core::mem::MaybeUninit::<[usize; FD_SETSIZE_USIZES * 3]>::uninit(); - let bits_ptr = unsafe { core::mem::transmute(bits.as_mut_ptr()) }; + let bits_ptr: *mut usize = unsafe { core::mem::transmute(bits.as_mut_ptr()) }; let copy_from_fd_set = |bits_ptr: *mut usize, fds: *const ctypes::fd_set| unsafe { let dst = core::slice::from_raw_parts_mut(bits_ptr, nfds_usizes); diff --git a/apps/task/tls/src/main.rs b/apps/task/tls/src/main.rs index 07d8709222..89028c8cb8 100644 --- a/apps/task/tls/src/main.rs +++ b/apps/task/tls/src/main.rs @@ -7,6 +7,7 @@ #[cfg(feature = "axstd")] extern crate axstd as std; +use std::{ptr::addr_of, str::from_utf8_unchecked}; use std::{thread, vec::Vec}; #[thread_local] @@ -56,14 +57,14 @@ fn main() { get!(U16), get!(U32), get!(U64), - get!(std::str::from_utf8_unchecked(&STR)) + get!(from_utf8_unchecked(&*addr_of!(STR))) ); assert!(get!(BOOL)); assert_eq!(get!(U8), 0xAA); assert_eq!(get!(U16), 0xcafe); assert_eq!(get!(U32), 0xdeadbeed); assert_eq!(get!(U64), 0xa2ce05_a2ce05); - assert_eq!(get!(&STR), b"Hello, world!"); + assert_eq!(get!(&*addr_of!(STR)), b"Hello, world!"); let mut tasks = Vec::new(); for i in 1..=10 { @@ -85,7 +86,7 @@ fn main() { get!(U16), get!(U32), get!(U64), - get!(std::str::from_utf8_unchecked(&STR)) + get!(from_utf8_unchecked(&*addr_of!(STR))) ); assert_eq!(get!(BOOL), i % 2 == 0); assert_eq!(get!(U8), 0xAA + i as u8); @@ -105,7 +106,7 @@ fn main() { assert_eq!(get!(U16), 0xcafe); assert_eq!(get!(U32), 0xdeadbeed); assert_eq!(get!(U64), 0xa2ce05_a2ce05); - assert_eq!(get!(&STR), b"Hello, world!"); + assert_eq!(get!(&*addr_of!(STR)), b"Hello, world!"); println!("TLS tests run OK!"); } diff --git a/crates/axerrno/src/lib.rs b/crates/axerrno/src/lib.rs index 986040cb4d..81bb6d82fd 100644 --- a/crates/axerrno/src/lib.rs +++ b/crates/axerrno/src/lib.rs @@ -225,7 +225,7 @@ impl TryFrom for AxError { #[inline] fn try_from(value: i32) -> Result { if value > 0 && value <= core::mem::variant_count::() as i32 { - Ok(unsafe { core::mem::transmute(value) }) + Ok(unsafe { core::mem::transmute::(value) }) } else { Err(value) } diff --git a/crates/kernel_guard/src/arch/mod.rs b/crates/kernel_guard/src/arch/mod.rs index 3a05149726..58928d8ce7 100644 --- a/crates/kernel_guard/src/arch/mod.rs +++ b/crates/kernel_guard/src/arch/mod.rs @@ -1,4 +1,4 @@ -#![cfg_attr(not(target_os = "none"), allow(dead_code))] +#![cfg_attr(not(target_os = "none"), allow(dead_code, unused_imports))] cfg_if::cfg_if! { if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] { diff --git a/crates/linked_list/src/unsafe_list.rs b/crates/linked_list/src/unsafe_list.rs index 68fe46cb4c..af91f367c1 100644 --- a/crates/linked_list/src/unsafe_list.rs +++ b/crates/linked_list/src/unsafe_list.rs @@ -242,7 +242,7 @@ impl List { // SAFETY: The safety requirements of this function satisfy those of `insert_after`. unsafe { self.insert_after(self.inner_ref(existing).prev, new) }; - if self.first.unwrap() == existing { + if core::ptr::eq(self.first.unwrap().as_ptr(), existing.as_ptr()) { // Update the pointer to the first element as we're inserting before it. self.first = Some(NonNull::from(new)); } @@ -459,7 +459,7 @@ impl CommonCursor { if let Some(head) = list.first { // SAFETY: Per the function safety requirements, `cur` is in the list. let links = unsafe { list.inner_ref(cur) }; - if links.next != head { + if !core::ptr::eq(links.next.as_ptr(), head.as_ptr()) { self.cur = Some(links.next); } } @@ -480,7 +480,7 @@ impl CommonCursor { let next = match self.cur.take() { None => head, Some(cur) => { - if cur == head { + if core::ptr::eq(cur.as_ptr(), head.as_ptr()) { return; } cur diff --git a/crates/timer_list/src/lib.rs b/crates/timer_list/src/lib.rs index b73f848683..32f04b272c 100644 --- a/crates/timer_list/src/lib.rs +++ b/crates/timer_list/src/lib.rs @@ -73,7 +73,7 @@ impl Ord for TimerEventWrapper { impl PartialEq for TimerEventWrapper { fn eq(&self, other: &Self) -> bool { - self.deadline.eq(&other.deadline) + self.deadline == other.deadline } } diff --git a/modules/axhal/src/arch/x86_64/idt.rs b/modules/axhal/src/arch/x86_64/idt.rs index ffccff7d4c..ce488bdac8 100644 --- a/modules/axhal/src/arch/x86_64/idt.rs +++ b/modules/axhal/src/arch/x86_64/idt.rs @@ -32,6 +32,7 @@ impl IdtStruct { ) }; for i in 0..NUM_INT { + #[allow(clippy::missing_transmute_annotations)] entries[i].set_handler_fn(unsafe { core::mem::transmute(ENTRIES[i]) }); } idt diff --git a/modules/axhal/src/platform/aarch64_bsta1000b/mem.rs b/modules/axhal/src/platform/aarch64_bsta1000b/mem.rs index e6faa5ef4e..d4b6989791 100644 --- a/modules/axhal/src/platform/aarch64_bsta1000b/mem.rs +++ b/modules/axhal/src/platform/aarch64_bsta1000b/mem.rs @@ -7,9 +7,11 @@ pub(crate) fn platform_regions() -> impl Iterator { } pub(crate) unsafe fn init_boot_page_table( - boot_pt_l0: &mut [A64PTE; 512], - boot_pt_l1: &mut [A64PTE; 512], + boot_pt_l0: *mut [A64PTE; 512], + boot_pt_l1: *mut [A64PTE; 512], ) { + let boot_pt_l0 = &mut *boot_pt_l0; + let boot_pt_l1 = &mut *boot_pt_l1; // 0x0000_0000_0000 ~ 0x0080_0000_0000, table boot_pt_l0[0] = A64PTE::new_table(PhysAddr::from(boot_pt_l1.as_ptr() as usize)); // 0x0000_0000_0000..0x0000_4000_0000, 1G block, device memory diff --git a/modules/axhal/src/platform/aarch64_common/boot.rs b/modules/axhal/src/platform/aarch64_common/boot.rs index a944e18116..8b3e759d07 100644 --- a/modules/axhal/src/platform/aarch64_common/boot.rs +++ b/modules/axhal/src/platform/aarch64_common/boot.rs @@ -1,4 +1,5 @@ use aarch64_cpu::{asm, asm::barrier, registers::*}; +use core::ptr::addr_of_mut; use memory_addr::PhysAddr; use page_table_entry::aarch64::{MemAttr, A64PTE}; use tock_registers::interfaces::{ReadWriteable, Readable, Writeable}; @@ -97,7 +98,7 @@ unsafe fn enable_fp() { } unsafe fn init_boot_page_table() { - crate::platform::mem::init_boot_page_table(&mut BOOT_PT_L0, &mut BOOT_PT_L1); + crate::platform::mem::init_boot_page_table(addr_of_mut!(BOOT_PT_L0), addr_of_mut!(BOOT_PT_L1)); } /// The earliest entry point for the primary CPU. diff --git a/modules/axhal/src/platform/aarch64_qemu_virt/mem.rs b/modules/axhal/src/platform/aarch64_qemu_virt/mem.rs index 8218bda677..6e2bb92c72 100644 --- a/modules/axhal/src/platform/aarch64_qemu_virt/mem.rs +++ b/modules/axhal/src/platform/aarch64_qemu_virt/mem.rs @@ -7,9 +7,11 @@ pub(crate) fn platform_regions() -> impl Iterator { } pub(crate) unsafe fn init_boot_page_table( - boot_pt_l0: &mut [A64PTE; 512], - boot_pt_l1: &mut [A64PTE; 512], + boot_pt_l0: *mut [A64PTE; 512], + boot_pt_l1: *mut [A64PTE; 512], ) { + let boot_pt_l0 = &mut *boot_pt_l0; + let boot_pt_l1 = &mut *boot_pt_l1; // 0x0000_0000_0000 ~ 0x0080_0000_0000, table boot_pt_l0[0] = A64PTE::new_table(PhysAddr::from(boot_pt_l1.as_ptr() as usize)); // 0x0000_0000_0000..0x0000_4000_0000, 1G block, device memory diff --git a/modules/axhal/src/platform/aarch64_raspi/mem.rs b/modules/axhal/src/platform/aarch64_raspi/mem.rs index 7c426e08f9..a397917937 100644 --- a/modules/axhal/src/platform/aarch64_raspi/mem.rs +++ b/modules/axhal/src/platform/aarch64_raspi/mem.rs @@ -14,9 +14,11 @@ pub(crate) fn platform_regions() -> impl Iterator { } pub(crate) unsafe fn init_boot_page_table( - boot_pt_l0: &mut [A64PTE; 512], - boot_pt_l1: &mut [A64PTE; 512], + boot_pt_l0: *mut [A64PTE; 512], + boot_pt_l1: *mut [A64PTE; 512], ) { + let boot_pt_l0 = &mut *boot_pt_l0; + let boot_pt_l1 = &mut *boot_pt_l1; // 0x0000_0000_0000 ~ 0x0080_0000_0000, table boot_pt_l0[0] = A64PTE::new_table(PhysAddr::from(boot_pt_l1.as_ptr() as usize)); // 0x0000_0000_0000..0x0000_4000_0000, 1G block, device memory diff --git a/modules/axnet/src/smoltcp_impl/addr.rs b/modules/axnet/src/smoltcp_impl/addr.rs index 5d683fee05..a9d47582c2 100644 --- a/modules/axnet/src/smoltcp_impl/addr.rs +++ b/modules/axnet/src/smoltcp_impl/addr.rs @@ -1,4 +1,4 @@ -use core::net::{IpAddr, SocketAddr}; +use core::net::{IpAddr, Ipv4Addr, SocketAddr}; use smoltcp::wire::{IpAddress, IpEndpoint, Ipv4Address}; pub const fn from_core_ipaddr(ip: IpAddr) -> IpAddress { @@ -10,8 +10,10 @@ pub const fn from_core_ipaddr(ip: IpAddr) -> IpAddress { pub const fn into_core_ipaddr(ip: IpAddress) -> IpAddr { match ip { - IpAddress::Ipv4(ipv4) => IpAddr::V4(unsafe { core::mem::transmute(ipv4.0) }), - // _ => panic!("IPv6 not supported"), + IpAddress::Ipv4(ipv4) => { + IpAddr::V4(unsafe { core::mem::transmute::<[u8; 4], Ipv4Addr>(ipv4.0) }) + } + _ => panic!("IPv6 not supported"), } } diff --git a/modules/axtask/Cargo.toml b/modules/axtask/Cargo.toml index 73ce843ff0..e1eb52709d 100644 --- a/modules/axtask/Cargo.toml +++ b/modules/axtask/Cargo.toml @@ -43,4 +43,4 @@ crate_interface = { path = "../../crates/crate_interface", optional = true } [dev-dependencies] rand = "0.8" axhal = { path = "../axhal", features = ["fp_simd"] } -axtask = { path = ".", features = ["test"] } +axtask = { path = ".", features = ["test", "multitask"] } diff --git a/modules/axtask/src/lib.rs b/modules/axtask/src/lib.rs index 2147f4646e..b02a1ea1f2 100644 --- a/modules/axtask/src/lib.rs +++ b/modules/axtask/src/lib.rs @@ -29,6 +29,9 @@ #![feature(doc_cfg)] #![feature(doc_auto_cfg)] +#[cfg(test)] +mod tests; + cfg_if::cfg_if! { if #[cfg(feature = "multitask")] { #[macro_use] diff --git a/modules/axtask/src/tests.rs b/modules/axtask/src/tests.rs index c3dbe0af99..47a85e83ed 100644 --- a/modules/axtask/src/tests.rs +++ b/modules/axtask/src/tests.rs @@ -1,7 +1,7 @@ use core::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Mutex, Once}; -use crate::{self as axtask, current, WaitQueue}; +use crate::{api as axtask, current, WaitQueue}; static INIT: Once = Once::new(); static SERIAL: Mutex<()> = Mutex::new(()); diff --git a/rust-toolchain.toml b/rust-toolchain.toml index 3170b4a5a5..16632f80e1 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,5 +1,5 @@ [toolchain] profile = "minimal" channel = "nightly-2024-01-19" -components = ["rust-src", "llvm-tools-preview", "rustfmt", "clippy"] +components = ["rust-src", "llvm-tools", "rustfmt", "clippy"] targets = ["x86_64-unknown-none", "riscv64gc-unknown-none-elf", "aarch64-unknown-none-softfloat"] diff --git a/scripts/make/cargo.mk b/scripts/make/cargo.mk index fc07ea2dae..954138b7c0 100644 --- a/scripts/make/cargo.mk +++ b/scripts/make/cargo.mk @@ -31,9 +31,11 @@ define cargo_build $(call run_cmd,cargo build,$(build_args) $(1) --features "$(strip $(2))") endef +clippy_args := -A clippy::new_without_default + define cargo_clippy - $(call run_cmd,cargo clippy,--all-features --workspace --exclude axlog $(1) $(verbose)) - $(call run_cmd,cargo clippy,-p axlog -p percpu -p percpu_macros $(1) $(verbose)) + $(call run_cmd,cargo clippy,--all-features --workspace --exclude axlog $(1) $(verbose) -- $(clippy_args)) + $(call run_cmd,cargo clippy,-p axlog -p percpu -p percpu_macros $(1) $(verbose) -- $(clippy_args)) endef all_packages := \