Skip to content

Commit 34abe94

Browse files
committed
multiboot2: apply latest clippy code style guidelines
1 parent ec05bf5 commit 34abe94

18 files changed

+198
-85
lines changed

multiboot2/Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

5+
- **Breaking** All functions that returns something useful are now `#[must_use]`
56
- updated dependencies
67
- MSRV is 1.75
78
- doc fixes

multiboot2/src/boot_information.rs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub struct BootInformationHeader {
4646

4747
#[cfg(feature = "builder")]
4848
impl BootInformationHeader {
49-
pub(crate) fn new(total_size: u32) -> Self {
49+
pub(crate) const fn new(total_size: u32) -> Self {
5050
Self {
5151
total_size,
5252
_reserved: 0,
@@ -140,12 +140,14 @@ impl<'a> BootInformation<'a> {
140140
}
141141

142142
/// Get the start address of the boot info.
143+
#[must_use]
143144
pub fn start_address(&self) -> usize {
144145
self.as_ptr() as usize
145146
}
146147

147148
/// Get the start address of the boot info as pointer.
148-
pub fn as_ptr(&self) -> *const () {
149+
#[must_use]
150+
pub const fn as_ptr(&self) -> *const () {
149151
core::ptr::addr_of!(*self.0).cast()
150152
}
151153

@@ -159,12 +161,14 @@ impl<'a> BootInformation<'a> {
159161
/// # let boot_info = unsafe { BootInformation::load(ptr).unwrap() };
160162
/// let end_addr = boot_info.start_address() + boot_info.total_size();
161163
/// ```
164+
#[must_use]
162165
pub fn end_address(&self) -> usize {
163166
self.start_address() + self.total_size()
164167
}
165168

166169
/// Get the total size of the boot info struct.
167-
pub fn total_size(&self) -> usize {
170+
#[must_use]
171+
pub const fn total_size(&self) -> usize {
168172
self.0.header.total_size as usize
169173
}
170174

@@ -177,11 +181,13 @@ impl<'a> BootInformation<'a> {
177181
}*/
178182

179183
/// Search for the basic memory info tag.
184+
#[must_use]
180185
pub fn basic_memory_info_tag(&self) -> Option<&BasicMemoryInfoTag> {
181186
self.get_tag::<BasicMemoryInfoTag>()
182187
}
183188

184189
/// Search for the BootLoader name tag.
190+
#[must_use]
185191
pub fn boot_loader_name_tag(&self) -> Option<&BootLoaderNameTag> {
186192
self.get_tag::<BootLoaderNameTag>()
187193
}
@@ -192,11 +198,13 @@ impl<'a> BootInformation<'a> {
192198
}*/
193199

194200
/// Search for the Command line tag.
201+
#[must_use]
195202
pub fn command_line_tag(&self) -> Option<&CommandLineTag> {
196203
self.get_tag::<CommandLineTag>()
197204
}
198205

199206
/// Search for the EFI boot services not exited tag.
207+
#[must_use]
200208
pub fn efi_bs_not_exited_tag(&self) -> Option<&EFIBootServicesNotExitedTag> {
201209
self.get_tag::<EFIBootServicesNotExitedTag>()
202210
}
@@ -205,34 +213,37 @@ impl<'a> BootInformation<'a> {
205213
/// Otherwise, if the [`TagType::EfiBs`] tag is present, this returns `None`
206214
/// as it is strictly recommended to get the memory map from the `uefi`
207215
/// services.
216+
#[must_use]
208217
pub fn efi_memory_map_tag(&self) -> Option<&EFIMemoryMapTag> {
209218
// If the EFIBootServicesNotExited is present, then we should not use
210219
// the memory map, as it could still be in use.
211-
match self.get_tag::<EFIBootServicesNotExitedTag>() {
212-
Some(_tag) => {
213-
log::debug!("The EFI memory map is present but the UEFI Boot Services Not Existed Tag is present. Returning None.");
214-
None
215-
}
216-
None => self.get_tag::<EFIMemoryMapTag>(),
217-
}
220+
self.get_tag::<EFIBootServicesNotExitedTag>().map_or_else(
221+
|| self.get_tag::<EFIMemoryMapTag>(), |_tag| {
222+
log::debug!("The EFI memory map is present but the UEFI Boot Services Not Existed Tag is present. Returning None.");
223+
None
224+
})
218225
}
219226

220227
/// Search for the EFI 32-bit SDT tag.
228+
#[must_use]
221229
pub fn efi_sdt32_tag(&self) -> Option<&EFISdt32Tag> {
222230
self.get_tag::<EFISdt32Tag>()
223231
}
224232

225233
/// Search for the EFI 64-bit SDT tag.
234+
#[must_use]
226235
pub fn efi_sdt64_tag(&self) -> Option<&EFISdt64Tag> {
227236
self.get_tag::<EFISdt64Tag>()
228237
}
229238

230239
/// Search for the EFI 32-bit image handle pointer tag.
240+
#[must_use]
231241
pub fn efi_ih32_tag(&self) -> Option<&EFIImageHandle32Tag> {
232242
self.get_tag::<EFIImageHandle32Tag>()
233243
}
234244

235245
/// Search for the EFI 64-bit image handle pointer tag.
246+
#[must_use]
236247
pub fn efi_ih64_tag(&self) -> Option<&EFIImageHandle64Tag> {
237248
self.get_tag::<EFIImageHandle64Tag>()
238249
}
@@ -254,6 +265,7 @@ impl<'a> BootInformation<'a> {
254265
/// }
255266
/// }
256267
/// ```
268+
#[must_use]
257269
pub fn elf_sections(&self) -> Option<ElfSectionIter> {
258270
let tag = self.get_tag::<ElfSectionsTag>();
259271
tag.map(|t| {
@@ -264,6 +276,7 @@ impl<'a> BootInformation<'a> {
264276

265277
/// Search for the VBE framebuffer tag. The result is `Some(Err(e))`, if the
266278
/// framebuffer type is unknown, while the framebuffer tag is present.
279+
#[must_use]
267280
pub fn framebuffer_tag(&self) -> Option<Result<&FramebufferTag, UnknownFramebufferType>> {
268281
self.get_tag::<FramebufferTag>()
269282
.map(|tag| match tag.buffer_type() {
@@ -273,16 +286,19 @@ impl<'a> BootInformation<'a> {
273286
}
274287

275288
/// Search for the Image Load Base Physical Address tag.
289+
#[must_use]
276290
pub fn load_base_addr_tag(&self) -> Option<&ImageLoadPhysAddrTag> {
277291
self.get_tag::<ImageLoadPhysAddrTag>()
278292
}
279293

280294
/// Search for the Memory map tag.
295+
#[must_use]
281296
pub fn memory_map_tag(&self) -> Option<&MemoryMapTag> {
282297
self.get_tag::<MemoryMapTag>()
283298
}
284299

285300
/// Get an iterator of all module tags.
301+
#[must_use]
286302
pub fn module_tags(&self) -> ModuleIter {
287303
module::module_iter(self.tags())
288304
}
@@ -293,21 +309,25 @@ impl<'a> BootInformation<'a> {
293309
}*/
294310

295311
/// Search for the (ACPI 1.0) RSDP tag.
312+
#[must_use]
296313
pub fn rsdp_v1_tag(&self) -> Option<&RsdpV1Tag> {
297314
self.get_tag::<RsdpV1Tag>()
298315
}
299316

300317
/// Search for the (ACPI 2.0 or later) RSDP tag.
318+
#[must_use]
301319
pub fn rsdp_v2_tag(&self) -> Option<&RsdpV2Tag> {
302320
self.get_tag::<RsdpV2Tag>()
303321
}
304322

305323
/// Search for the SMBIOS tag.
324+
#[must_use]
306325
pub fn smbios_tag(&self) -> Option<&SmbiosTag> {
307326
self.get_tag::<SmbiosTag>()
308327
}
309328

310329
/// Search for the VBE information tag.
330+
#[must_use]
311331
pub fn vbe_info_tag(&self) -> Option<&VBEInfoTag> {
312332
self.get_tag::<VBEInfoTag>()
313333
}
@@ -367,6 +387,7 @@ impl<'a> BootInformation<'a> {
367387
/// .unwrap();
368388
/// assert_eq!(tag.name(), Ok("name"));
369389
/// ```
390+
#[must_use]
370391
pub fn get_tag<TagT: TagTrait + ?Sized + 'a>(&'a self) -> Option<&'a TagT> {
371392
self.tags()
372393
.find(|tag| tag.typ == TagT::ID)

multiboot2/src/boot_loader_name.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub struct BootLoaderNameTag {
2020

2121
impl BootLoaderNameTag {
2222
#[cfg(feature = "builder")]
23+
#[must_use]
2324
pub fn new(name: &str) -> BoxedDst<Self> {
2425
let mut bytes: Vec<_> = name.bytes().collect();
2526
if !bytes.ends_with(&[0]) {
@@ -100,7 +101,7 @@ mod tests {
100101
let tag = get_bytes();
101102
let tag = unsafe { &*tag.as_ptr().cast::<Tag>() };
102103
let tag = tag.cast_tag::<BootLoaderNameTag>();
103-
assert_eq!({ tag.typ }, TagType::BootLoaderName);
104+
assert_eq!(tag.header.typ, TagType::BootLoaderName);
104105
assert_eq!(tag.name().expect("must be valid UTF-8"), MSG);
105106
}
106107

multiboot2/src/builder/boxed_dst.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ mod tests {
144144

145145
#[test]
146146
fn can_hold_tag_trait() {
147-
fn consume<T: TagTrait + ?Sized>(_: &T) {}
147+
const fn consume<T: TagTrait + ?Sized>(_: &T) {}
148148
let content = b"hallo\0";
149149

150150
let tag = BoxedDst::<CustomTag>::new(content);

multiboot2/src/builder/information.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ impl Default for InformationBuilder {
7373

7474
impl InformationBuilder {
7575
/// Creates a new builder.
76+
#[must_use]
7677
pub const fn new() -> Self {
7778
Self(Vec::new())
7879
}
@@ -87,6 +88,7 @@ impl InformationBuilder {
8788
/// [`Self::build`]-method is called. This function assumes that the begin
8889
/// of the boot information is 8-byte aligned and automatically adds padding
8990
/// between tags to ensure that each tag is 8-byte aligned.
91+
#[must_use]
9092
pub fn expected_len(&self) -> usize {
9193
let tag_size_iter = self.0.iter().map(|(_, bytes)| bytes.len());
9294

@@ -118,6 +120,7 @@ impl InformationBuilder {
118120
}
119121

120122
/// Constructs the bytes for a valid Multiboot2 information with the given properties.
123+
#[must_use]
121124
pub fn build(self) -> BootInformationBytes {
122125
const ALIGN: usize = 8;
123126

@@ -202,92 +205,109 @@ impl InformationBuilder {
202205
}
203206

204207
/// Adds a 'basic memory information' tag (represented by [`BasicMemoryInfoTag`]) to the builder.
208+
#[must_use]
205209
pub fn basic_memory_info_tag(self, tag: BasicMemoryInfoTag) -> Self {
206210
self.add_tag(&tag).unwrap()
207211
}
208212

209213
/// Adds a 'bootloader name' tag (represented by [`BootLoaderNameTag`]) to the builder.
214+
#[must_use]
210215
pub fn bootloader_name_tag(self, tag: BoxedDst<BootLoaderNameTag>) -> Self {
211216
self.add_tag(&*tag).unwrap()
212217
}
213218

214219
/// Adds a 'command line' tag (represented by [`CommandLineTag`]) to the builder.
220+
#[must_use]
215221
pub fn command_line_tag(self, tag: BoxedDst<CommandLineTag>) -> Self {
216222
self.add_tag(&*tag).unwrap()
217223
}
218224

219225
/// Adds a 'EFI 32-bit system table pointer' tag (represented by [`EFISdt32Tag`]) to the builder.
226+
#[must_use]
220227
pub fn efisdt32_tag(self, tag: EFISdt32Tag) -> Self {
221228
self.add_tag(&tag).unwrap()
222229
}
223230

224231
/// Adds a 'EFI 64-bit system table pointer' tag (represented by [`EFISdt64Tag`]) to the builder.
232+
#[must_use]
225233
pub fn efisdt64_tag(self, tag: EFISdt64Tag) -> Self {
226234
self.add_tag(&tag).unwrap()
227235
}
228236

229237
/// Adds a 'EFI boot services not terminated' tag (represented by [`EFIBootServicesNotExitedTag`]) to the builder.
238+
#[must_use]
230239
pub fn efi_boot_services_not_exited_tag(self) -> Self {
231240
self.add_tag(&EFIBootServicesNotExitedTag::new()).unwrap()
232241
}
233242

234243
/// Adds a 'EFI 32-bit image handle pointer' tag (represented by [`EFIImageHandle32Tag`]) to the builder.
244+
#[must_use]
235245
pub fn efi_image_handle32(self, tag: EFIImageHandle32Tag) -> Self {
236246
self.add_tag(&tag).unwrap()
237247
}
238248

239249
/// Adds a 'EFI 64-bit image handle pointer' tag (represented by [`EFIImageHandle64Tag`]) to the builder.
250+
#[must_use]
240251
pub fn efi_image_handle64(self, tag: EFIImageHandle64Tag) -> Self {
241252
self.add_tag(&tag).unwrap()
242253
}
243254

244255
/// Adds a 'EFI Memory map' tag (represented by [`EFIMemoryMapTag`]) to the builder.
256+
#[must_use]
245257
pub fn efi_memory_map_tag(self, tag: BoxedDst<EFIMemoryMapTag>) -> Self {
246258
self.add_tag(&*tag).unwrap()
247259
}
248260

249261
/// Adds a 'ELF-Symbols' tag (represented by [`ElfSectionsTag`]) to the builder.
262+
#[must_use]
250263
pub fn elf_sections_tag(self, tag: BoxedDst<ElfSectionsTag>) -> Self {
251264
self.add_tag(&*tag).unwrap()
252265
}
253266

254267
/// Adds a 'Framebuffer info' tag (represented by [`FramebufferTag`]) to the builder.
268+
#[must_use]
255269
pub fn framebuffer_tag(self, tag: BoxedDst<FramebufferTag>) -> Self {
256270
self.add_tag(&*tag).unwrap()
257271
}
258272

259273
/// Adds a 'Image load base physical address' tag (represented by [`ImageLoadPhysAddrTag`]) to the builder.
274+
#[must_use]
260275
pub fn image_load_addr(self, tag: ImageLoadPhysAddrTag) -> Self {
261276
self.add_tag(&tag).unwrap()
262277
}
263278

264279
/// Adds a (*none EFI*) 'memory map' tag (represented by [`MemoryMapTag`]) to the builder.
280+
#[must_use]
265281
pub fn memory_map_tag(self, tag: BoxedDst<MemoryMapTag>) -> Self {
266282
self.add_tag(&*tag).unwrap()
267283
}
268284

269285
/// Adds a 'Modules' tag (represented by [`ModuleTag`]) to the builder.
270286
/// This tag can occur multiple times in boot information.
287+
#[must_use]
271288
pub fn add_module_tag(self, tag: BoxedDst<ModuleTag>) -> Self {
272289
self.add_tag(&*tag).unwrap()
273290
}
274291

275292
/// Adds a 'ACPI old RSDP' tag (represented by [`RsdpV1Tag`]) to the builder.
293+
#[must_use]
276294
pub fn rsdp_v1_tag(self, tag: RsdpV1Tag) -> Self {
277295
self.add_tag(&tag).unwrap()
278296
}
279297

280298
/// Adds a 'ACPI new RSDP' tag (represented by [`RsdpV2Tag`]) to the builder.
299+
#[must_use]
281300
pub fn rsdp_v2_tag(self, tag: RsdpV2Tag) -> Self {
282301
self.add_tag(&tag).unwrap()
283302
}
284303

285304
/// Adds a 'SMBIOS tables' tag (represented by [`SmbiosTag`]) to the builder.
305+
#[must_use]
286306
pub fn smbios_tag(self, tag: BoxedDst<SmbiosTag>) -> Self {
287307
self.add_tag(&*tag).unwrap()
288308
}
289309

290-
fn tag_is_allowed_multiple_times(tag_type: TagType) -> bool {
310+
const fn tag_is_allowed_multiple_times(tag_type: TagType) -> bool {
291311
matches!(
292312
tag_type,
293313
TagType::Module | TagType::Smbios | TagType::Custom(_)

multiboot2/src/command_line.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use core::str;
88
#[cfg(feature = "builder")]
99
use {crate::builder::BoxedDst, alloc::vec::Vec};
1010

11-
pub(crate) const METADATA_SIZE: usize = mem::size_of::<TagTypeId>() + mem::size_of::<u32>();
11+
pub const METADATA_SIZE: usize = mem::size_of::<TagTypeId>() + mem::size_of::<u32>();
1212

1313
/// This tag contains the command line string.
1414
///
@@ -25,6 +25,7 @@ pub struct CommandLineTag {
2525
impl CommandLineTag {
2626
/// Create a new command line tag from the given string.
2727
#[cfg(feature = "builder")]
28+
#[must_use]
2829
pub fn new(command_line: &str) -> BoxedDst<Self> {
2930
let mut bytes: Vec<_> = command_line.bytes().collect();
3031
if !bytes.ends_with(&[0]) {
@@ -107,7 +108,7 @@ mod tests {
107108
let tag = get_bytes();
108109
let tag = unsafe { &*tag.as_ptr().cast::<Tag>() };
109110
let tag = tag.cast_tag::<CommandLineTag>();
110-
assert_eq!({ tag.typ }, TagType::Cmdline);
111+
assert_eq!(tag.header.typ, TagType::Cmdline);
111112
assert_eq!(tag.cmdline().expect("must be valid UTF-8"), MSG);
112113
}
113114

0 commit comments

Comments
 (0)