Skip to content

Commit e197fdd

Browse files
jinankjainjyao1
authored andcommitted
misc: Fix reported clippy warnings
Various components are throwing some trivial clippy warnings which are against the idiomatic rust pattterns. Signed-off-by: Jinank Jain <[email protected]>
1 parent 0b87fe7 commit e197fdd

File tree

18 files changed

+51
-60
lines changed

18 files changed

+51
-60
lines changed

cc-measurement/src/log.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl<'a> CcEventLogWriter<'a> {
127127

128128
// Write the event header into event log memory and update the 'size' and 'last'
129129
let event_offset = self
130-
.log_cc_event_header(mr_index, event_type, &sha384, event_data_size as u32)
130+
.log_cc_event_header(mr_index, event_type, sha384, event_data_size as u32)
131131
.ok_or(CcEventLogError::OutOfResource)?;
132132

133133
let mut data_offset = size_of::<CcEventHeader>();
@@ -242,10 +242,10 @@ impl<'a> Iterator for CcEvents<'a> {
242242
if end_of_event < self.bytes.len() {
243243
let event_data = &self.bytes[size_of::<CcEventHeader>()..end_of_event];
244244
self.bytes = &self.bytes[end_of_event..];
245-
return Some((event_header, event_data));
245+
Some((event_header, event_data))
246246
} else {
247-
return None;
248-
};
247+
None
248+
}
249249
}
250250
}
251251

@@ -277,7 +277,7 @@ impl<'a> CcEventLogReader<'a> {
277277
specific_id_event,
278278
};
279279

280-
return Some(cc_event_log);
280+
Some(cc_event_log)
281281
}
282282

283283
pub fn query(&self, key: &[u8]) -> Option<CcEventHeader> {

td-exception/src/idt.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ impl Idt {
102102
current_idt[20].set_func(interrupt::default_exception as usize);
103103
current_idt[21].set_func(interrupt::control_flow as usize);
104104
// reset exception reserved
105-
for i in 22..32 {
106-
current_idt[i].set_func(interrupt::default_exception as usize);
105+
for idt in current_idt.iter_mut().take(32).skip(22) {
106+
idt.set_func(interrupt::default_exception as usize);
107107
}
108108
// Setup reset potential interrupt handler.
109-
for i in 32..IDT_ENTRY_COUNT {
110-
current_idt[i].set_func(interrupt::default_interrupt as usize);
109+
for idt in current_idt.iter_mut().take(IDT_ENTRY_COUNT).skip(32) {
110+
idt.set_func(interrupt::default_interrupt as usize);
111111
}
112112
}
113113

@@ -179,7 +179,7 @@ impl IdtEntry {
179179
// A function to set the offset more easily
180180
pub fn set_func(&mut self, func: usize) {
181181
self.set_flags(IdtFlags::PRESENT | IdtFlags::INTERRUPT);
182-
self.set_offset(CS::get_reg().0, func as usize); // GDT_KERNEL_CODE 1u16
182+
self.set_offset(CS::get_reg().0, func); // GDT_KERNEL_CODE 1u16
183183
}
184184

185185
pub fn set_ist(&mut self, index: u8) {

td-exception/src/interrupt.rs

+11-17
Original file line numberDiff line numberDiff line change
@@ -507,15 +507,15 @@ fn handle_tdx_ioexit(ve_info: &tdx::TdVeInfo, stack: &mut InterruptNoErrorStack)
507507
let io_read = |size, port| match size {
508508
1 => tdx::tdvmcall_io_read_8(port) as u32,
509509
2 => tdx::tdvmcall_io_read_16(port) as u32,
510-
4 => tdx::tdvmcall_io_read_32(port) as u32,
510+
4 => tdx::tdvmcall_io_read_32(port),
511511
_ => 0,
512512
};
513513

514514
// Define closure to perform IO port write with different size operands
515515
let io_write = |size, port, data| match size {
516516
1 => tdx::tdvmcall_io_write_8(port, data as u8),
517517
2 => tdx::tdvmcall_io_write_16(port, data as u16),
518-
4 => tdx::tdvmcall_io_write_32(port, data as u32),
518+
4 => tdx::tdvmcall_io_write_32(port, data),
519519
_ => {}
520520
};
521521

@@ -525,36 +525,30 @@ fn handle_tdx_ioexit(ve_info: &tdx::TdVeInfo, stack: &mut InterruptNoErrorStack)
525525
if read {
526526
let val = io_read(size, port);
527527
unsafe {
528-
let rsi = core::slice::from_raw_parts_mut(
529-
stack.scratch.rdi as *mut u8,
530-
size as usize,
531-
);
528+
let rsi = core::slice::from_raw_parts_mut(stack.scratch.rdi as *mut u8, size);
532529
// Safety: size is smaller than 4
533530
rsi.copy_from_slice(&u32::to_le_bytes(val)[..size])
534531
}
535-
stack.scratch.rdi += size as usize;
532+
stack.scratch.rdi += size;
536533
} else {
537534
let mut val = 0;
538535
unsafe {
539-
let rsi =
540-
core::slice::from_raw_parts(stack.scratch.rsi as *mut u8, size as usize);
536+
let rsi = core::slice::from_raw_parts(stack.scratch.rsi as *mut u8, size);
541537
for (idx, byte) in rsi.iter().enumerate() {
542538
val |= (*byte as u32) << (idx * 8);
543539
}
544540
}
545541
io_write(size, port, val);
546-
stack.scratch.rsi += size as usize;
542+
stack.scratch.rsi += size;
547543
}
548544
stack.scratch.rcx -= 1;
549545
}
546+
} else if read {
547+
// Write the IO read result to the low $size-bytes of rax
548+
stack.scratch.rax = (stack.scratch.rax & !(2_usize.pow(size as u32 * 8) - 1))
549+
| (io_read(size, port) as usize & (2_usize.pow(size as u32 * 8) - 1));
550550
} else {
551-
if read {
552-
// Write the IO read result to the low $size-bytes of rax
553-
stack.scratch.rax = (stack.scratch.rax & !(2_usize.pow(size as u32 * 8) - 1))
554-
| (io_read(size, port) as usize & (2_usize.pow(size as u32 * 8) - 1));
555-
} else {
556-
io_write(size, port, stack.scratch.rax as u32);
557-
}
551+
io_write(size, port, stack.scratch.rax as u32);
558552
}
559553

560554
true

td-layout/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl RuntimeMemoryLayout {
9393
self.regions
9494
.iter()
9595
.find(|item| item.name == name.as_str())
96-
.map(|region| *region)
96+
.copied()
9797
}
9898

9999
pub unsafe fn get_mem_slice(&self, name: SliceType) -> Option<&'static [u8]> {

td-layout/src/runtime/exec.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub const PAYLOAD_PAGE_TABLE_SIZE: usize = 0x20000; // 128 KB
4040
pub const RELOCATED_MAILBOX_SIZE: usize = 0x2000; // 8 KB
4141
pub const EVENT_LOG_SIZE: usize = 0x100000; // 1 MB
4242

43-
pub const MEMORY_LAYOUT_CONFIG: &[(&'static str, usize, &'static str)] = &[
43+
pub const MEMORY_LAYOUT_CONFIG: &[(&str, usize, &str)] = &[
4444
// (name of memory region, region size, region type)
4545
("Bootloader", 0x800000, "Memory"),
4646
("TdHob", 0x20000, "Memory"),

td-layout/src/runtime/linux.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub const PAYLOAD_PAGE_TABLE_SIZE: usize = 0x20000; // 128 KB
4949
pub const RELOCATED_MAILBOX_SIZE: usize = 0x2000; // 8 KB
5050
pub const EVENT_LOG_SIZE: usize = 0x100000; // 1 MB
5151

52-
pub const MEMORY_LAYOUT_CONFIG: &[(&'static str, usize, &'static str)] = &[
52+
pub const MEMORY_LAYOUT_CONFIG: &[(&str, usize, &str)] = &[
5353
// (name of memory region, region size, region type)
5454
("Bootloader", 0x800000, "Memory"),
5555
("TdHob", 0x20000, "Memory"),

td-loader/src/elf.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ pub fn relocate_elf_with_per_program_header(
8181
}
8282

8383
Some((
84-
elf.header.e_entry.checked_add(new_image_base as u64)? as u64,
85-
bottom as u64,
86-
top.checked_sub(bottom)? as u64,
84+
elf.header.e_entry.checked_add(new_image_base as u64)?,
85+
bottom,
86+
top.checked_sub(bottom)?,
8787
))
8888
}
8989

@@ -108,9 +108,9 @@ pub fn parse_finit_array_section(loaded_image: &[u8]) -> Option<Range<usize>> {
108108
/// flag true align to low address else high address
109109
fn align_value(value: u64, align: u64, flag: bool) -> u64 {
110110
if flag {
111-
value & ((!(align - 1)) as u64)
111+
value & (!(align - 1))
112112
} else {
113-
value - (value & (align - 1)) as u64 + align
113+
value - (value & (align - 1)) + align
114114
}
115115
}
116116

td-loader/src/pe.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ pub fn relocate_with_per_section(
191191
.pwrite(new_image_base as u64, coff_optional_offset)
192192
.ok()?;
193193

194-
let sections = Sections::parse(sections_buffer, num_sections as usize)?;
194+
let sections = Sections::parse(sections_buffer, num_sections)?;
195195
// Load the PE header into the destination memory
196196
for section in sections {
197197
let section_size = section.section_size() as usize;
@@ -200,8 +200,8 @@ pub fn relocate_with_per_section(
200200
let dst_start = section.virtual_address as usize;
201201
let dst_end = dst_start.checked_add(section_size)?;
202202

203-
image_buffer.len().checked_sub(src_end as usize)?;
204-
loaded_buffer.len().checked_sub(dst_end as usize)?;
203+
image_buffer.len().checked_sub(src_end)?;
204+
loaded_buffer.len().checked_sub(dst_end)?;
205205
loaded_buffer[dst_start..dst_end].copy_from_slice(&image_buffer[src_start..src_end]);
206206
if section.virtual_size as usize > section_size {
207207
let fill_end = dst_start.checked_add(section.virtual_size as usize)?;
@@ -210,15 +210,15 @@ pub fn relocate_with_per_section(
210210
}
211211
}
212212

213-
let sections = Sections::parse(sections_buffer, num_sections as usize)?;
213+
let sections = Sections::parse(sections_buffer, num_sections)?;
214214
for section in sections {
215215
if &section.name == b".reloc\0\0" && image_base != new_image_base as u64 {
216216
reloc_to_base(
217217
loaded_buffer,
218218
image_buffer,
219219
&section,
220220
image_base as usize,
221-
new_image_base as usize,
221+
new_image_base,
222222
)?;
223223
}
224224
}
@@ -435,7 +435,7 @@ impl<'a> Iterator for Relocations<'a> {
435435
bytes.len().checked_sub(block_size as usize)?;
436436
self.offset += block_size as usize;
437437

438-
let entries = &bytes[(core::mem::size_of::<u32>() * 2) as usize..block_size as usize];
438+
let entries = &bytes[(core::mem::size_of::<u32>() * 2)..block_size as usize];
439439
Some(Relocation {
440440
page_rva,
441441
block_size,
@@ -466,10 +466,7 @@ fn reloc_to_base(
466466
.checked_sub(image_base as u64)?
467467
.checked_add(new_image_base as u64)?;
468468
loaded_buffer
469-
.pwrite(
470-
value - image_base as u64 + new_image_base as u64,
471-
location as usize,
472-
)
469+
.pwrite(value - image_base as u64 + new_image_base as u64, location)
473470
.ok()?;
474471
log::trace!(
475472
"reloc {:08x}: {:012x} -> {:012x}",

td-logger/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const SERIAL_IO_PORT: u16 = 0x3F8;
5858

5959
#[cfg(feature = "tdx")]
6060
fn dbg_port_write(byte: u8) {
61-
let _ = tdx_tdcall::tdx::tdvmcall_io_write_8(SERIAL_IO_PORT, byte);
61+
tdx_tdcall::tdx::tdvmcall_io_write_8(SERIAL_IO_PORT, byte);
6262
}
6363

6464
#[cfg(all(not(feature = "tdx"), feature = "serial-port"))]

td-paging/src/page_table.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub fn create_mapping_with_flags(
8787
|| va.as_u64() & (ALIGN_4K - 1) != 0
8888
|| sz & (ALIGN_4K - 1) != 0
8989
|| ps.count_ones() != 1
90-
|| ps < ALIGN_4K as u64
90+
|| ps < ALIGN_4K
9191
{
9292
return Err(Error::InvalidArguments);
9393
}

td-payload/src/arch/x86_64/shared.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub fn encrypt(addr: u64, length: usize) {
2323
if tdx_tdcall::tdx::tdvmcall_mapgpa(false, addr, length).is_err() {
2424
panic!("Fail to map GPA to private memory with TDVMCALL");
2525
}
26-
accept_memory(addr, length as usize);
26+
accept_memory(addr, length);
2727
}
2828

2929
fn accept_memory(addr: u64, length: usize) {

td-payload/src/mm/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub fn get_usable(size: usize) -> Option<u64> {
7373

7474
if entry.r#type == E820Type::Memory as u32 && entry.size >= size as u64 {
7575
entry.size -= size as u64;
76-
return Some(entry.addr + entry.size as u64);
76+
return Some(entry.addr + entry.size);
7777
}
7878
}
7979

td-shim/src/acpi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl Xsdt {
116116
let table_num =
117117
(self.header.length as usize - size_of::<GenericSdtHeader>()) / size_of::<u64>();
118118
if table_num < ACPI_TABLES_MAX_NUM {
119-
self.tables[table_num] = addr as u64;
119+
self.tables[table_num] = addr;
120120
self.header.length += size_of::<u64>() as u32;
121121
Ok(())
122122
} else {

td-shim/src/bin/td-shim/memory.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ impl<'a> Memory<'a> {
239239
false
240240
}
241241

242-
#[cfg(all(feature = "tdx"))]
242+
#[cfg(feature = "tdx")]
243243
/// Build a 2M granularity bitmap for kernel to track the unaccepted memory
244244
pub fn build_unaccepted_memory_bitmap(&self) -> u64 {
245245
#[cfg(not(feature = "lazy-accept"))]

td-shim/src/metadata.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl TdxMetadataGuid {
171171
/// * `buffer` - A buffer contains TdxMetadata guid.
172172
pub fn from_bytes(buffer: &[u8; 16]) -> Option<TdxMetadataGuid> {
173173
let guid = Guid::from_bytes(buffer);
174-
let metadata_guid = TdxMetadataGuid { guid: guid };
174+
let metadata_guid = TdxMetadataGuid { guid };
175175
if metadata_guid.is_valid() {
176176
Some(metadata_guid)
177177
} else {
@@ -428,10 +428,10 @@ pub fn validate_sections(sections: &[TdxMetadataSection]) -> Result<(), TdxMetad
428428
}
429429

430430
//TdInfo. If present, it shall be included in BFV section.
431-
if td_info_cnt != 0 {
432-
if td_info_start < bfv_start || td_info_start >= bfv_end || td_info_end > bfv_end {
433-
return Err(TdxMetadataError::InvalidSection);
434-
}
431+
if td_info_cnt != 0
432+
&& (td_info_start < bfv_start || td_info_start >= bfv_end || td_info_end > bfv_end)
433+
{
434+
return Err(TdxMetadataError::InvalidSection);
435435
}
436436

437437
Ok(())

td-uefi-pi/src/pi/fv.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,8 @@ impl FfsFileHeader {
268268
// Validate the checksum of the FfsFileHeader
269269
pub fn validate_checksum(&self) -> bool {
270270
let sum = sum8(self.as_bytes());
271-
sum ^ ((EFI_FILE_HEADER_CONSTRUCTION | EFI_FILE_HEADER_VALID | EFI_FILE_DATA_VALID) as u8
272-
+ FFS_FIXED_CHECKSUM as u8)
271+
sum ^ ((EFI_FILE_HEADER_CONSTRUCTION | EFI_FILE_HEADER_VALID | EFI_FILE_DATA_VALID)
272+
+ FFS_FIXED_CHECKSUM)
273273
== 0
274274
}
275275
}

td-uefi-pi/src/pi/hob.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ impl MemoryAllocation {
150150
pub fn dump(&self) {
151151
log::info!(
152152
"MemoryAllocation type: 0x{:08x} base: 0x{:016x} length: 0x{:016x}\n",
153-
self.alloc_descriptor.memory_type as u32,
153+
self.alloc_descriptor.memory_type,
154154
self.alloc_descriptor.memory_base_address,
155155
self.alloc_descriptor.memory_length
156156
);

tdx-tdcall/src/tdx.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ pub fn tdvmcall_cpuid(eax: u32, ecx: u32) -> CpuIdInfo {
402402
pub fn tdvmcall_setup_event_notify(vector: u64) -> Result<(), TdVmcallError> {
403403
let mut args = TdVmcallArgs {
404404
r11: TDVMCALL_SETUPEVENTNOTIFY,
405-
r12: vector as u64,
405+
r12: vector,
406406
..Default::default()
407407
};
408408

0 commit comments

Comments
 (0)