From 662dcaec22cbeafb41c737747e0eae09d6323054 Mon Sep 17 00:00:00 2001 From: Yuekai Jia Date: Sat, 11 May 2024 20:13:37 +0800 Subject: [PATCH] Fix cargo clippy warnings --- api/arceos_posix_api/Cargo.toml | 1 + api/arceos_posix_api/src/imp/fd_ops.rs | 1 + api/arceos_posix_api/src/imp/io_mpx/select.rs | 2 +- apps/task/priority/src/main.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/memory_addr/src/lib.rs | 1 - crates/scheduler/src/lib.rs | 1 - crates/timer_list/src/lib.rs | 2 +- modules/axdriver/build.rs | 25 ++++++++++++++ modules/axhal/build.rs | 33 +++++++++++++++++++ modules/axhal/src/arch/x86_64/idt.rs | 1 + .../src/platform/aarch64_bsta1000b/mem.rs | 6 ++-- .../axhal/src/platform/aarch64_common/boot.rs | 3 +- .../src/platform/aarch64_qemu_virt/mem.rs | 6 ++-- .../axhal/src/platform/aarch64_raspi/mem.rs | 6 ++-- modules/axnet/src/smoltcp_impl/addr.rs | 7 ++-- modules/axtask/Cargo.toml | 2 +- modules/axtask/src/lib.rs | 3 ++ modules/axtask/src/tests.rs | 2 +- scripts/make/cargo.mk | 6 ++-- scripts/make/features.mk | 2 +- ulib/axlibc/Cargo.toml | 3 ++ 25 files changed, 105 insertions(+), 29 deletions(-) diff --git a/api/arceos_posix_api/Cargo.toml b/api/arceos_posix_api/Cargo.toml index 03081419b9..87cf2e1bb6 100644 --- a/api/arceos_posix_api/Cargo.toml +++ b/api/arceos_posix_api/Cargo.toml @@ -18,6 +18,7 @@ repository = "https://github.com/rcore-os/arceos/tree/main/api/arceos_posix_api" default = [] smp = ["axfeat/smp"] +irq = ["axfeat/irq"] alloc = ["dep:axalloc", "axfeat/alloc"] multitask = ["axtask/multitask", "axfeat/multitask", "axsync/multitask"] fd = ["alloc"] diff --git a/api/arceos_posix_api/src/imp/fd_ops.rs b/api/arceos_posix_api/src/imp/fd_ops.rs index 75ee61061f..926669eca8 100644 --- a/api/arceos_posix_api/src/imp/fd_ops.rs +++ b/api/arceos_posix_api/src/imp/fd_ops.rs @@ -11,6 +11,7 @@ use crate::ctypes; pub const AX_FILE_LIMIT: usize = 1024; +#[allow(dead_code)] pub trait FileLike: Send + Sync { fn read(&self, buf: &mut [u8]) -> LinuxResult; fn write(&self, buf: &[u8]) -> LinuxResult; 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/priority/src/main.rs b/apps/task/priority/src/main.rs index ff013efd82..ab7c3e4885 100644 --- a/apps/task/priority/src/main.rs +++ b/apps/task/priority/src/main.rs @@ -9,7 +9,7 @@ use std::sync::Arc; use std::{thread, time}; use std::{vec, vec::Vec}; -#[cfg(any(feature = "axstd", target_os = "arceos"))] +#[cfg(feature = "axstd")] use std::os::arceos::api::task::ax_set_current_priority; struct TaskParam { 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/memory_addr/src/lib.rs b/crates/memory_addr/src/lib.rs index 4a73609da9..8aff4ad0db 100644 --- a/crates/memory_addr/src/lib.rs +++ b/crates/memory_addr/src/lib.rs @@ -1,5 +1,4 @@ #![no_std] -#![feature(const_mut_refs)] #![doc = include_str!("../README.md")] use core::fmt; diff --git a/crates/scheduler/src/lib.rs b/crates/scheduler/src/lib.rs index 28f055a53a..de15661458 100644 --- a/crates/scheduler/src/lib.rs +++ b/crates/scheduler/src/lib.rs @@ -7,7 +7,6 @@ //! - [`CFScheduler`]: Completely Fair Scheduler (preemptive). #![cfg_attr(not(test), no_std)] -#![feature(const_mut_refs)] mod cfs; mod fifo; 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/axdriver/build.rs b/modules/axdriver/build.rs index d418d5ec19..3ebb4db3dc 100644 --- a/modules/axdriver/build.rs +++ b/modules/axdriver/build.rs @@ -2,6 +2,14 @@ const NET_DEV_FEATURES: &[&str] = &["ixgbe", "virtio-net"]; const BLOCK_DEV_FEATURES: &[&str] = &["ramdisk", "bcm2835-sdhci", "virtio-blk"]; const DISPLAY_DEV_FEATURES: &[&str] = &["virtio-gpu"]; +fn make_cfg_values(str_list: &[&str]) -> String { + str_list + .iter() + .map(|s| format!("{:?}", s)) + .collect::>() + .join(", ") +} + fn has_feature(feature: &str) -> bool { std::env::var(format!( "CARGO_FEATURE_{}", @@ -47,4 +55,21 @@ fn main() { enable_cfg(&format!("{dev_kind}_dev"), "dummy"); } } + + println!( + "cargo::rustc-check-cfg=cfg(bus, values({}))", + make_cfg_values(&["pci", "mmio"]) + ); + println!( + "cargo::rustc-check-cfg=cfg(net_dev, values({}, \"dummy\"))", + make_cfg_values(NET_DEV_FEATURES) + ); + println!( + "cargo::rustc-check-cfg=cfg(block_dev, values({}, \"dummy\"))", + make_cfg_values(BLOCK_DEV_FEATURES) + ); + println!( + "cargo::rustc-check-cfg=cfg(display_dev, values({}, \"dummy\"))", + make_cfg_values(DISPLAY_DEV_FEATURES) + ); } diff --git a/modules/axhal/build.rs b/modules/axhal/build.rs index 69343f4256..5bcfcb5f35 100644 --- a/modules/axhal/build.rs +++ b/modules/axhal/build.rs @@ -1,5 +1,30 @@ use std::io::Result; +const BUILTIN_PLATFORMS: &[&str] = &[ + "aarch64-bsta1000b", + "aarch64-qemu-virt", + "aarch64-raspi4", + "riscv64-qemu-virt", + "x86_64-pc-oslab", + "x86_64-qemu-q35", +]; + +const BUILTIN_PLATFORM_FAMILIES: &[&str] = &[ + "aarch64-bsta1000b", + "aarch64-qemu-virt", + "aarch64-raspi", + "riscv64-qemu-virt", + "x86-pc", +]; + +fn make_cfg_values(str_list: &[&str]) -> String { + str_list + .iter() + .map(|s| format!("{:?}", s)) + .collect::>() + .join(", ") +} + fn main() { let arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap(); let platform = axconfig::PLATFORM; @@ -9,6 +34,14 @@ fn main() { println!("cargo:rustc-cfg=platform=\"{}\"", platform); println!("cargo:rustc-cfg=platform_family=\"{}\"", axconfig::FAMILY); + println!( + "cargo::rustc-check-cfg=cfg(platform, values({}))", + make_cfg_values(BUILTIN_PLATFORMS) + ); + println!( + "cargo::rustc-check-cfg=cfg(platform_family, values({}))", + make_cfg_values(BUILTIN_PLATFORM_FAMILIES) + ); } fn gen_linker_script(arch: &str, platform: &str) -> Result<()> { 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 d4bf337520..b7f65187e5 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..31d2be1c7d 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,9 @@ 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/scripts/make/cargo.mk b/scripts/make/cargo.mk index 21f4fcc464..8d4be28fce 100644 --- a/scripts/make/cargo.mk +++ b/scripts/make/cargo.mk @@ -27,9 +27,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 := \ diff --git a/scripts/make/features.mk b/scripts/make/features.mk index 2ae6276adc..80a7e53c3a 100644 --- a/scripts/make/features.mk +++ b/scripts/make/features.mk @@ -14,7 +14,7 @@ ifeq ($(APP_TYPE),c) ax_feat_prefix := axfeat/ lib_feat_prefix := axlibc/ - lib_features := fp_simd alloc multitask fs net fd pipe select epoll + lib_features := fp_simd irq alloc multitask fs net fd pipe select epoll else # TODO: it's better to use `axfeat/` as `ax_feat_prefix`, but all apps need to have `axfeat` as a dependency ax_feat_prefix := axstd/ diff --git a/ulib/axlibc/Cargo.toml b/ulib/axlibc/Cargo.toml index 703e79e014..f625e5efcf 100644 --- a/ulib/axlibc/Cargo.toml +++ b/ulib/axlibc/Cargo.toml @@ -28,6 +28,9 @@ smp = ["arceos_posix_api/smp"] # Floating point/SIMD fp_simd = ["axfeat/fp_simd"] +# Interrupts +irq = ["arceos_posix_api/irq", "axfeat/irq"] + # Memory alloc = ["arceos_posix_api/alloc"] tls = ["alloc", "axfeat/tls"]