Skip to content

Commit bbea7a5

Browse files
committed
fix: refine clippy
1 parent 75498fb commit bbea7a5

18 files changed

+50
-65
lines changed

.github/workflows/main.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ jobs:
1313
- name: Setup Environment
1414
run: cd os && make env
1515
- name: Check user
16-
run: cd usr/rust/ && cargo fmt -- --check && cargo clippy -- -D warnings
16+
run: |
17+
export USER_IMG = ../usr/build/riscv64.img
18+
cd usr/rust/ && cargo fmt -- --check && cargo clippy -- -D warnings
1719
- name: Check os
1820
run: cd os && cargo fmt -- --check && cargo clippy -- -D warnings
1921
build:

os/src/context.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ extern "C" {
6262

6363
impl ContextContent {
6464
fn new_kernel_thread(entry: usize, kstack_top: usize, satp: usize) -> ContextContent {
65-
let content = ContextContent {
65+
ContextContent {
6666
ra: __trapret as usize,
6767
satp,
6868
s: [0; 12],
@@ -76,8 +76,7 @@ impl ContextContent {
7676
tf.sstatus.set_sie(false);
7777
tf
7878
},
79-
};
80-
content
79+
}
8180
}
8281

8382
fn new_user_thread(entry: usize, ustack_top: usize, satp: usize) -> Self {

os/src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
#![no_main]
33

44
#[allow(unused_imports)]
5+
#[allow(clippy::single_component_path_imports)]
56
use os;

os/src/memory/frame_allocator.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ impl SegmentTreeAllocator {
1414
self.n = r - l;
1515
self.m = 1;
1616
while self.m < self.n + 2 {
17-
self.m = self.m << 1;
17+
self.m <<= 1;
1818
}
1919
for i in 1..(self.m << 1) {
2020
self.a[i] = 1;
@@ -35,7 +35,7 @@ impl SegmentTreeAllocator {
3535
let mut p = 1;
3636
while p < self.m {
3737
if self.a[p << 1] == 0 {
38-
p = p << 1;
38+
p <<= 1;
3939
} else {
4040
p = (p << 1) | 1;
4141
}

os/src/memory/memory_set/area.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ impl MemoryArea {
4242
MemoryArea {
4343
start: start_addr,
4444
end: end_addr,
45-
handler: handler,
46-
attr: attr,
45+
handler,
46+
attr,
4747
}
4848
}
4949

os/src/memory/memory_set/attr.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
use crate::memory::paging::PageEntry;
22

3-
#[derive(Clone, Debug)]
3+
#[derive(Clone, Default, Debug)]
44
pub struct MemoryAttr {
55
user: bool,
66
readonly: bool,
77
execute: bool,
88
}
99

1010
impl MemoryAttr {
11-
pub fn new() -> Self {
12-
MemoryAttr {
13-
user: false,
14-
readonly: false,
15-
execute: false,
16-
}
17-
}
18-
1911
pub fn set_user(mut self) -> Self {
2012
self.user = true;
2113
self

os/src/memory/memory_set/handler.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,10 @@ impl MemoryHandler for Linear {
4747
let dst = core::slice::from_raw_parts_mut(va as *mut u8, PAGE_SIZE);
4848
if length > 0 {
4949
let src = core::slice::from_raw_parts(src as *const u8, PAGE_SIZE);
50-
for i in 0..length {
51-
dst[i] = src[i];
52-
}
50+
dst[..length].clone_from_slice(&src[..length]);
5351
}
54-
for i in length..PAGE_SIZE {
55-
dst[i] = 0;
52+
for item in dst.iter_mut().take(PAGE_SIZE).skip(length) {
53+
*item = 0;
5654
}
5755
}
5856
}
@@ -85,12 +83,10 @@ impl MemoryHandler for ByFrame {
8583
let dst = core::slice::from_raw_parts_mut(access_pa_via_va(pa) as *mut u8, PAGE_SIZE);
8684
if length > 0 {
8785
let src = core::slice::from_raw_parts(src as *const u8, PAGE_SIZE);
88-
for i in 0..length {
89-
dst[i] = src[i];
90-
}
86+
dst[..length].clone_from_slice(&src[..length]);
9187
}
92-
for i in length..PAGE_SIZE {
93-
dst[i] = 0;
88+
for item in dst.iter_mut().take(PAGE_SIZE).skip(length) {
89+
*item = 0;
9490
}
9591
}
9692
}

os/src/memory/memory_set/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -68,39 +68,39 @@ impl MemorySet {
6868
self.push(
6969
stext as usize,
7070
etext as usize,
71-
MemoryAttr::new().set_readonly().set_execute(),
71+
MemoryAttr::default().set_readonly().set_execute(),
7272
Linear::new(offset),
7373
None,
7474
);
7575
// .rodata R
7676
self.push(
7777
srodata as usize,
7878
erodata as usize,
79-
MemoryAttr::new().set_readonly(),
79+
MemoryAttr::default().set_readonly(),
8080
Linear::new(offset),
8181
None,
8282
);
8383
// .data R|W
8484
self.push(
8585
sdata as usize,
8686
edata as usize,
87-
MemoryAttr::new(),
87+
MemoryAttr::default(),
8888
Linear::new(offset),
8989
None,
9090
);
9191
// .bss R|W
9292
self.push(
9393
sbss as usize,
9494
ebss as usize,
95-
MemoryAttr::new(),
95+
MemoryAttr::default(),
9696
Linear::new(offset),
9797
None,
9898
);
9999
// 物理内存 R|W
100100
self.push(
101101
(end as usize / PAGE_SIZE + 1) * PAGE_SIZE,
102102
access_pa_via_va(PHYSICAL_MEMORY_END),
103-
MemoryAttr::new(),
103+
MemoryAttr::default(),
104104
Linear::new(offset),
105105
None,
106106
);

os/src/memory/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,21 @@ pub fn kernel_remap() {
5050
memory_set.push(
5151
bootstack as usize,
5252
bootstacktop as usize,
53-
MemoryAttr::new(),
53+
MemoryAttr::default(),
5454
Linear::new(PHYSICAL_MEMORY_OFFSET),
5555
None,
5656
);
5757
memory_set.push(
5858
access_pa_via_va(0x0c00_2000),
5959
access_pa_via_va(0x0c00_3000),
60-
MemoryAttr::new(),
60+
MemoryAttr::default(),
6161
Linear::new(PHYSICAL_MEMORY_OFFSET),
6262
None,
6363
);
6464
memory_set.push(
6565
access_pa_via_va(0x1000_0000),
6666
access_pa_via_va(0x1000_1000),
67-
MemoryAttr::new(),
67+
MemoryAttr::default(),
6868
Linear::new(PHYSICAL_MEMORY_OFFSET),
6969
None,
7070
);

os/src/memory/paging.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl PageTableImpl {
122122

123123
pub fn get_entry(&mut self, va: usize) -> Option<&mut PageEntry> {
124124
let page = Page::of_addr(VirtAddr::new(va));
125-
if let Ok(e) = self.page_table.ref_entry(page.clone()) {
125+
if let Ok(e) = self.page_table.ref_entry(page) {
126126
let e = unsafe { &mut *(e as *mut PageTableEntry) };
127127
self.entry = Some(PageEntry(e, page));
128128
Some(self.entry.as_mut().unwrap())

os/src/process/processor.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ impl Processor {
3434
}
3535
}
3636

37+
#[allow(clippy::mut_from_ref)]
3738
fn inner(&self) -> &mut ProcessorInner {
3839
unsafe { &mut *self.inner.get() }
3940
.as_mut()
@@ -68,14 +69,12 @@ impl Processor {
6869

6970
pub fn tick(&self) {
7071
let inner = self.inner();
71-
if !inner.current.is_none() {
72-
if inner.pool.tick() {
73-
let flags = disable_and_store();
72+
if inner.current.is_some() && inner.pool.tick() {
73+
let flags = disable_and_store();
7474

75-
inner.current.as_mut().unwrap().1.switch_to(&mut inner.idle);
75+
inner.current.as_mut().unwrap().1.switch_to(&mut inner.idle);
7676

77-
restore(flags);
78-
}
77+
restore(flags);
7978
}
8079
}
8180

@@ -103,7 +102,7 @@ impl Processor {
103102
#[allow(unused_unsafe)]
104103
pub fn yield_now(&self) {
105104
let inner = self.inner();
106-
if !inner.current.is_none() {
105+
if inner.current.is_some() {
107106
unsafe {
108107
let flags = disable_and_store();
109108
let tid = inner.current.as_mut().unwrap().0;
@@ -133,6 +132,7 @@ impl Processor {
133132
self.inner().current.as_mut().unwrap().0 as usize
134133
}
135134

135+
#[allow(clippy::mut_from_ref)]
136136
pub fn current_thread_mut(&self) -> &mut Thread {
137137
self.inner().current.as_mut().unwrap().1.as_mut()
138138
}

os/src/process/scheduler.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,9 @@ impl Scheduler for RRScheduler {
7979
let tid = self.current;
8080
if tid != 0 {
8181
self.threads[tid].time -= 1;
82-
if self.threads[tid].time == 0 {
83-
return true;
84-
} else {
85-
return false;
86-
}
82+
return self.threads[tid].time == 0;
8783
}
88-
return true;
84+
true
8985
}
9086

9187
fn exit(&mut self, tid: Tid) {

os/src/process/structs.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl Thread {
8787
vm.push(
8888
ustack_bottom,
8989
ustack_top,
90-
MemoryAttr::new().set_user(),
90+
MemoryAttr::default().set_user(),
9191
ByFrame::new(),
9292
None,
9393
);
@@ -98,7 +98,7 @@ impl Thread {
9898

9999
let mut thread = Thread {
100100
context: Context::new_user_thread(entry_addr, ustack_top, kstack.top(), vm.token()),
101-
kstack: kstack,
101+
kstack,
102102
wait: wait_thread,
103103
ofile: [None; NOFILE],
104104
};
@@ -191,7 +191,7 @@ trait ToMemoryAttr {
191191
}
192192
impl ToMemoryAttr for Flags {
193193
fn to_attr(&self) -> MemoryAttr {
194-
let mut flags = MemoryAttr::new().set_user();
194+
let mut flags = MemoryAttr::default().set_user();
195195
if self.is_execute() {
196196
flags = flags.set_execute();
197197
}

os/src/process/thread_pool.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ impl ThreadPool {
4646
if let Some(tid) = self.scheduler.pop() {
4747
let mut thread_info = self.threads[tid].as_mut().expect("thread not exist!");
4848
thread_info.status = Status::Running(tid);
49-
return Some((tid, thread_info.thread.take().expect("thread not exist!")));
49+
Some((tid, thread_info.thread.take().expect("thread not exist!")))
5050
} else {
51-
return None;
51+
None
5252
}
5353
}
5454

@@ -65,8 +65,7 @@ impl ThreadPool {
6565
}
6666

6767
pub fn tick(&mut self) -> bool {
68-
let ret = self.scheduler.tick();
69-
ret
68+
self.scheduler.tick()
7069
}
7170

7271
pub fn exit(&mut self, tid: Tid) {

os/src/syscall.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ unsafe fn sys_read(fd: usize, base: *mut u8, len: usize) -> isize {
5252
if fd == 0 {
5353
// 如果是标准输入
5454
*base = crate::fs::stdio::STDIN.pop() as u8;
55-
return 1;
55+
1
5656
} else {
5757
let thread = process::current_thread_mut();
5858
assert!(thread.ofile[fd].is_some());
@@ -69,7 +69,7 @@ unsafe fn sys_read(fd: usize, base: *mut u8, len: usize) -> isize {
6969
.unwrap();
7070
offset += s;
7171
file.set_offset(offset);
72-
return s as isize;
72+
s as isize
7373
}
7474
_ => {
7575
panic!("fdtype not handled!");
@@ -82,7 +82,7 @@ unsafe fn sys_write(fd: usize, base: *const u8, len: usize) -> isize {
8282
if fd == 1 {
8383
assert!(len == 1);
8484
crate::io::putchar(*base as char);
85-
return 1;
85+
1
8686
} else {
8787
let thread = process::current_thread_mut();
8888
assert!(thread.ofile[fd].is_some());
@@ -99,7 +99,7 @@ unsafe fn sys_write(fd: usize, base: *const u8, len: usize) -> isize {
9999
.unwrap();
100100
offset += s;
101101
file.set_offset(offset);
102-
return s as isize;
102+
s as isize
103103
}
104104
_ => {
105105
panic!("fdtype not handled!");
@@ -119,5 +119,5 @@ fn sys_exec(path: *const u8) -> isize {
119119
if valid {
120120
process::yield_now();
121121
}
122-
return 0;
122+
0
123123
}

usr/rust/src/bin/write.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub fn main() -> usize {
2424
// 将字符串从文件 temp 读入内存
2525
let read_fd = sys_open(FILE.as_ptr(), O_RDONLY);
2626
let read = [0u8; BUFFER_SIZE];
27-
sys_read(read_fd as usize, &read[0] as *const u8, BUFFER_SIZE);
27+
sys_read(read_fd as usize, read[0] as *mut u8, BUFFER_SIZE);
2828
println!("read from file 'temp' successfully...");
2929

3030
// 检查功能是否正确

usr/rust/src/io.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ pub const STDIN: usize = 0;
4242
pub const STDOUT: usize = 1;
4343

4444
pub fn getc() -> u8 {
45-
let c = 0u8;
46-
assert_eq!(sys_read(STDIN, &c, 1), 1);
45+
let mut c = 0u8;
46+
assert_eq!(sys_read(STDIN, &mut c, 1), 1);
4747
c
4848
}
4949

usr/rust/src/syscall.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub fn sys_exit(code: usize) -> ! {
4040
loop {}
4141
}
4242

43-
pub fn sys_read(fd: usize, base: *const u8, len: usize) -> i64 {
43+
pub fn sys_read(fd: usize, base: *mut u8, len: usize) -> i64 {
4444
sys_call(SyscallId::Read, fd, base as usize, len, 0)
4545
}
4646

0 commit comments

Comments
 (0)