Skip to content

Commit 70c66dd

Browse files
committed
fix(clippy): 修复内核代码库中的 Clippy 警告并优化 Rust 习惯用法
- 为 DeviceManager, Keyboard, KernFs 等结构体实现 Default trait - 使用 is_some_and, div_ceil, flatten, strip_prefix 等现代特性 - 优化内存分配,移除 Task 结构体不必要的 Box 包装 - 完善安全文档说明 (# Safety) 并优化测试循环效率
1 parent 0bb5466 commit 70c66dd

File tree

16 files changed

+90
-98
lines changed

16 files changed

+90
-98
lines changed

kernel/build.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,10 @@ fn main() {
2727
let obj_dir = workspace_root.join("target/obj");
2828

2929
if let Ok(paths) = glob(&format!("{}/*.o", obj_dir.display())) {
30-
for path_result in paths {
31-
if let Ok(path) = path_result {
32-
// Get the absolute path
33-
let absolute_path = path.canonicalize().expect("Failed to canonicalize path");
34-
println!("cargo:rustc-link-arg={}", absolute_path.display());
35-
}
30+
for path in paths.flatten() {
31+
// Get the absolute path
32+
let absolute_path = path.canonicalize().expect("Failed to canonicalize path");
33+
println!("cargo:rustc-link-arg={}", absolute_path.display());
3634
}
3735
}
3836
}

kernel/src/drivers/device.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ impl Clone for Device {
294294
}
295295
}
296296

297+
#[derive(Default)]
297298
pub struct DeviceManager {
298299
devices: Vec<Arc<Device>>,
299300
next_minor_counters: BTreeMap<u16, u16>,
@@ -302,11 +303,7 @@ pub struct DeviceManager {
302303

303304
impl DeviceManager {
304305
pub fn new() -> Self {
305-
Self {
306-
devices: Vec::new(),
307-
next_minor_counters: BTreeMap::new(),
308-
free_minors: BTreeMap::new(),
309-
}
306+
Self::default()
310307
}
311308

312309
pub fn register_device(&mut self, mut device: Device) -> Result<Arc<Device>, DeviceError> {
@@ -396,10 +393,7 @@ impl DeviceManager {
396393
}
397394

398395
fn reclaim_device_number(&mut self, major: u16, minor: u16) {
399-
self.free_minors
400-
.entry(major)
401-
.or_insert_with(Vec::new)
402-
.push(minor);
396+
self.free_minors.entry(major).or_default().push(minor);
403397
}
404398

405399
pub fn get_device(&self, name: &str) -> Option<Arc<Device>> {

kernel/src/drivers/input/keyboard.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ pub struct Keyboard {
2020
name: String,
2121
}
2222

23-
impl Keyboard {
24-
pub fn new() -> Self {
23+
impl Default for Keyboard {
24+
fn default() -> Self {
2525
Self {
2626
inner: Mutex::new(KeyboardInner {
2727
pc_keyboard: PcKeyboard::new(
@@ -37,6 +37,12 @@ impl Keyboard {
3737
name: String::from("keyboard"),
3838
}
3939
}
40+
}
41+
42+
impl Keyboard {
43+
pub fn new() -> Self {
44+
Self::default()
45+
}
4046

4147
pub fn set_enabled(&self, enabled: bool) {
4248
x86_64::instructions::interrupts::without_interrupts(|| {
@@ -121,7 +127,7 @@ impl CharDevice for Keyboard {
121127
}
122128
}
123129

124-
if read_count == 0 && buf.len() > 0 {
130+
if read_count == 0 && !buf.is_empty() {
125131
Err(DeviceError::WouldBlock)
126132
} else {
127133
Ok(read_count)

kernel/src/fs/bio.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ impl BlockCache {
2727
}
2828

2929
let block_size = self.device.block_size();
30-
let mut buf = Vec::with_capacity(block_size);
31-
buf.resize(block_size, 0);
30+
let mut buf = alloc::vec![0; block_size];
3231

3332
if self.device.read_blocks(block_id, 1, &mut buf).is_ok() {
3433
let mut cache = self.cache.write();

kernel/src/fs/fs_impl/kernfs.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,18 @@ pub struct KernFs {
238238
root: Arc<KernInode>,
239239
}
240240

241-
impl KernFs {
242-
pub fn new() -> Self {
241+
impl Default for KernFs {
242+
fn default() -> Self {
243243
let root = KernInode::new_dir();
244244

245245
Self { root }
246246
}
247+
}
248+
249+
impl KernFs {
250+
pub fn new() -> Self {
251+
Self::default()
252+
}
247253

248254
pub fn root(&self) -> Arc<KernInode> {
249255
self.root.clone()

kernel/src/fs/vfs.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ use lazy_static::lazy_static;
1313
use spin::{Mutex, RwLock};
1414

1515
lazy_static! {
16-
pub static ref VFS: Vfs = {
17-
let fs = Vfs::new();
18-
fs
19-
};
16+
pub static ref VFS: Vfs = Vfs::new();
2017
}
2118

2219
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
@@ -201,8 +198,10 @@ pub struct Vfs {
201198
fs_registry: RwLock<BTreeMap<&'static str, Arc<dyn FileSystem>>>,
202199
}
203200

204-
impl Vfs {
205-
pub fn new() -> Self {
201+
pub type WalkEntry = (String, Vec<String>, Vec<String>);
202+
203+
impl Default for Vfs {
204+
fn default() -> Self {
206205
let mut registry: BTreeMap<&'static str, Arc<dyn FileSystem>> = BTreeMap::new();
207206
let kernfs = Arc::new(fs_impl::kernfs::KernFs::new());
208207
registry.insert("kernfs", kernfs.clone());
@@ -220,6 +219,12 @@ impl Vfs {
220219
fs_registry: RwLock::new(registry),
221220
}
222221
}
222+
}
223+
224+
impl Vfs {
225+
pub fn new() -> Self {
226+
Self::default()
227+
}
223228

224229
pub fn init_root(&self, root: Arc<dyn Inode>) {
225230
*self.root.write() = Some(root);
@@ -455,10 +460,7 @@ impl Vfs {
455460
}
456461
}
457462

458-
pub fn walk(
459-
&self,
460-
start_path: &str,
461-
) -> Result<Vec<(String, Vec<String>, Vec<String>)>, VfsError> {
463+
pub fn walk(&self, start_path: &str) -> Result<Vec<WalkEntry>, VfsError> {
462464
let mut results = Vec::new();
463465
let mut stack: Vec<String> = Vec::new();
464466
let normalized_start_path = self.normalize_path(start_path);

kernel/src/interrupts/apic.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,14 @@ const XAPIC_EOI_OFFSET: u64 = 0x0B0;
1515
const XAPIC_SIVR_OFFSET: u64 = 0xF0;
1616
pub fn apic_is_available() -> bool {
1717
let cpuid = CpuId::new();
18-
cpuid
19-
.get_feature_info()
20-
.map_or(false, |info| info.has_apic())
18+
cpuid.get_feature_info().is_some_and(|info| info.has_apic())
2119
}
2220

2321
pub fn x2apic_is_available() -> bool {
2422
let cpuid = CpuId::new();
2523
cpuid
2624
.get_feature_info()
27-
.map_or(false, |info| info.has_x2apic())
25+
.is_some_and(|info| info.has_x2apic())
2826
}
2927

3028
/// Enables x2APIC mode

kernel/src/interrupts/gdt.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ lazy_static! {
1313
static mut STACK: [u8; STACK_SIZE] = [0; STACK_SIZE];
1414

1515
let stack_start = VirtAddr::from_ptr(&raw const STACK);
16-
let stack_end = stack_start + STACK_SIZE as u64;
17-
stack_end
16+
stack_start + STACK_SIZE as u64
1817
};
1918
tss
2019
};

kernel/src/interrupts/idt.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,22 @@ lazy_static! {
4141
}
4242
// PIC 中断处理器设置
4343
// IRQ0 - 时钟中断, IRQ1 - 键盘中断
44-
idt[PIC_1_OFFSET as u8].set_handler_fn(handler::pic_interrupt_handler_0); // IRQ0
45-
idt[PIC_1_OFFSET as u8 + 1].set_handler_fn(handler::pic_interrupt_handler_1); // IRQ1
46-
idt[PIC_1_OFFSET as u8 + 2].set_handler_fn(handler::pic_interrupt_handler_2); // IRQ2
47-
idt[PIC_1_OFFSET as u8 + 3].set_handler_fn(handler::pic_interrupt_handler_3); // IRQ3
48-
idt[PIC_1_OFFSET as u8 + 4].set_handler_fn(handler::pic_interrupt_handler_4); // IRQ4
49-
idt[PIC_1_OFFSET as u8 + 5].set_handler_fn(handler::pic_interrupt_handler_5); // IRQ5
50-
idt[PIC_1_OFFSET as u8 + 6].set_handler_fn(handler::pic_interrupt_handler_6); // IRQ6
51-
idt[PIC_1_OFFSET as u8 + 7].set_handler_fn(handler::pic_interrupt_handler_7); // IRQ7
52-
idt[PIC_2_OFFSET as u8].set_handler_fn(handler::pic_interrupt_handler_8); // IRQ8
53-
idt[PIC_2_OFFSET as u8 + 1].set_handler_fn(handler::pic_interrupt_handler_9); // IRQ9
54-
idt[PIC_2_OFFSET as u8 + 2].set_handler_fn(handler::pic_interrupt_handler_10); // IRQ10
55-
idt[PIC_2_OFFSET as u8 + 3].set_handler_fn(handler::pic_interrupt_handler_11); // IRQ11
56-
idt[PIC_2_OFFSET as u8 + 4].set_handler_fn(handler::pic_interrupt_handler_12); // IRQ12
57-
idt[PIC_2_OFFSET as u8 + 5].set_handler_fn(handler::pic_interrupt_handler_13); // IRQ13
58-
idt[PIC_2_OFFSET as u8 + 6].set_handler_fn(handler::pic_interrupt_handler_14); // IRQ14
59-
idt[PIC_2_OFFSET as u8 + 7].set_handler_fn(handler::pic_interrupt_handler_15); // IRQ15
44+
idt[PIC_1_OFFSET].set_handler_fn(handler::pic_interrupt_handler_0); // IRQ0
45+
idt[PIC_1_OFFSET + 1].set_handler_fn(handler::pic_interrupt_handler_1); // IRQ1
46+
idt[PIC_1_OFFSET + 2].set_handler_fn(handler::pic_interrupt_handler_2); // IRQ2
47+
idt[PIC_1_OFFSET + 3].set_handler_fn(handler::pic_interrupt_handler_3); // IRQ3
48+
idt[PIC_1_OFFSET + 4].set_handler_fn(handler::pic_interrupt_handler_4); // IRQ4
49+
idt[PIC_1_OFFSET + 5].set_handler_fn(handler::pic_interrupt_handler_5); // IRQ5
50+
idt[PIC_1_OFFSET + 6].set_handler_fn(handler::pic_interrupt_handler_6); // IRQ6
51+
idt[PIC_1_OFFSET + 7].set_handler_fn(handler::pic_interrupt_handler_7); // IRQ7
52+
idt[PIC_2_OFFSET].set_handler_fn(handler::pic_interrupt_handler_8); // IRQ8
53+
idt[PIC_2_OFFSET + 1].set_handler_fn(handler::pic_interrupt_handler_9); // IRQ9
54+
idt[PIC_2_OFFSET + 2].set_handler_fn(handler::pic_interrupt_handler_10); // IRQ10
55+
idt[PIC_2_OFFSET + 3].set_handler_fn(handler::pic_interrupt_handler_11); // IRQ11
56+
idt[PIC_2_OFFSET + 4].set_handler_fn(handler::pic_interrupt_handler_12); // IRQ12
57+
idt[PIC_2_OFFSET + 5].set_handler_fn(handler::pic_interrupt_handler_13); // IRQ13
58+
idt[PIC_2_OFFSET + 6].set_handler_fn(handler::pic_interrupt_handler_14); // IRQ14
59+
idt[PIC_2_OFFSET + 7].set_handler_fn(handler::pic_interrupt_handler_15); // IRQ15
6060
idt
6161
};
6262
}

kernel/src/lib.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,3 @@ macro_rules! extern_safe {
102102
)*
103103
};
104104
}
105-
106-
/* The test functions */
107-
#[cfg(test)]
108-
mod tests {
109-
110-
#[test_case]
111-
fn test_success() {
112-
assert_eq!(2 + 3, 5);
113-
}
114-
}

0 commit comments

Comments
 (0)