Quellcode durchsuchen

multiboot2: apply latest clippy code style guidelines

Philipp Schuster vor 8 Monaten
Ursprung
Commit
34abe94b4a

+ 1 - 0
multiboot2/Changelog.md

@@ -2,6 +2,7 @@
 
 ## Unreleased
 
+- **Breaking** All functions that returns something useful are now `#[must_use]`
 - updated dependencies
 - MSRV is 1.75
 - doc fixes

+ 31 - 10
multiboot2/src/boot_information.rs

@@ -46,7 +46,7 @@ pub struct BootInformationHeader {
 
 #[cfg(feature = "builder")]
 impl BootInformationHeader {
-    pub(crate) fn new(total_size: u32) -> Self {
+    pub(crate) const fn new(total_size: u32) -> Self {
         Self {
             total_size,
             _reserved: 0,
@@ -140,12 +140,14 @@ impl<'a> BootInformation<'a> {
     }
 
     /// Get the start address of the boot info.
+    #[must_use]
     pub fn start_address(&self) -> usize {
         self.as_ptr() as usize
     }
 
     /// Get the start address of the boot info as pointer.
-    pub fn as_ptr(&self) -> *const () {
+    #[must_use]
+    pub const fn as_ptr(&self) -> *const () {
         core::ptr::addr_of!(*self.0).cast()
     }
 
@@ -159,12 +161,14 @@ impl<'a> BootInformation<'a> {
     /// # let boot_info = unsafe { BootInformation::load(ptr).unwrap() };
     /// let end_addr = boot_info.start_address() + boot_info.total_size();
     /// ```
+    #[must_use]
     pub fn end_address(&self) -> usize {
         self.start_address() + self.total_size()
     }
 
     /// Get the total size of the boot info struct.
-    pub fn total_size(&self) -> usize {
+    #[must_use]
+    pub const fn total_size(&self) -> usize {
         self.0.header.total_size as usize
     }
 
@@ -177,11 +181,13 @@ impl<'a> BootInformation<'a> {
     }*/
 
     /// Search for the basic memory info tag.
+    #[must_use]
     pub fn basic_memory_info_tag(&self) -> Option<&BasicMemoryInfoTag> {
         self.get_tag::<BasicMemoryInfoTag>()
     }
 
     /// Search for the BootLoader name tag.
+    #[must_use]
     pub fn boot_loader_name_tag(&self) -> Option<&BootLoaderNameTag> {
         self.get_tag::<BootLoaderNameTag>()
     }
@@ -192,11 +198,13 @@ impl<'a> BootInformation<'a> {
     }*/
 
     /// Search for the Command line tag.
+    #[must_use]
     pub fn command_line_tag(&self) -> Option<&CommandLineTag> {
         self.get_tag::<CommandLineTag>()
     }
 
     /// Search for the EFI boot services not exited tag.
+    #[must_use]
     pub fn efi_bs_not_exited_tag(&self) -> Option<&EFIBootServicesNotExitedTag> {
         self.get_tag::<EFIBootServicesNotExitedTag>()
     }
@@ -205,34 +213,37 @@ impl<'a> BootInformation<'a> {
     /// Otherwise, if the [`TagType::EfiBs`] tag is present, this returns `None`
     /// as it is strictly recommended to get the memory map from the `uefi`
     /// services.
+    #[must_use]
     pub fn efi_memory_map_tag(&self) -> Option<&EFIMemoryMapTag> {
         // If the EFIBootServicesNotExited is present, then we should not use
         // the memory map, as it could still be in use.
-        match self.get_tag::<EFIBootServicesNotExitedTag>() {
-            Some(_tag) => {
-                log::debug!("The EFI memory map is present but the UEFI Boot Services Not Existed Tag is present. Returning None.");
-                None
-            }
-            None => self.get_tag::<EFIMemoryMapTag>(),
-        }
+        self.get_tag::<EFIBootServicesNotExitedTag>().map_or_else(
+            || self.get_tag::<EFIMemoryMapTag>(), |_tag| {
+                            log::debug!("The EFI memory map is present but the UEFI Boot Services Not Existed Tag is present. Returning None.");
+                             None
+                        })
     }
 
     /// Search for the EFI 32-bit SDT tag.
+    #[must_use]
     pub fn efi_sdt32_tag(&self) -> Option<&EFISdt32Tag> {
         self.get_tag::<EFISdt32Tag>()
     }
 
     /// Search for the EFI 64-bit SDT tag.
+    #[must_use]
     pub fn efi_sdt64_tag(&self) -> Option<&EFISdt64Tag> {
         self.get_tag::<EFISdt64Tag>()
     }
 
     /// Search for the EFI 32-bit image handle pointer tag.
+    #[must_use]
     pub fn efi_ih32_tag(&self) -> Option<&EFIImageHandle32Tag> {
         self.get_tag::<EFIImageHandle32Tag>()
     }
 
     /// Search for the EFI 64-bit image handle pointer tag.
+    #[must_use]
     pub fn efi_ih64_tag(&self) -> Option<&EFIImageHandle64Tag> {
         self.get_tag::<EFIImageHandle64Tag>()
     }
@@ -254,6 +265,7 @@ impl<'a> BootInformation<'a> {
     ///     }
     /// }
     /// ```
+    #[must_use]
     pub fn elf_sections(&self) -> Option<ElfSectionIter> {
         let tag = self.get_tag::<ElfSectionsTag>();
         tag.map(|t| {
@@ -264,6 +276,7 @@ impl<'a> BootInformation<'a> {
 
     /// Search for the VBE framebuffer tag. The result is `Some(Err(e))`, if the
     /// framebuffer type is unknown, while the framebuffer tag is present.
+    #[must_use]
     pub fn framebuffer_tag(&self) -> Option<Result<&FramebufferTag, UnknownFramebufferType>> {
         self.get_tag::<FramebufferTag>()
             .map(|tag| match tag.buffer_type() {
@@ -273,16 +286,19 @@ impl<'a> BootInformation<'a> {
     }
 
     /// Search for the Image Load Base Physical Address tag.
+    #[must_use]
     pub fn load_base_addr_tag(&self) -> Option<&ImageLoadPhysAddrTag> {
         self.get_tag::<ImageLoadPhysAddrTag>()
     }
 
     /// Search for the Memory map tag.
+    #[must_use]
     pub fn memory_map_tag(&self) -> Option<&MemoryMapTag> {
         self.get_tag::<MemoryMapTag>()
     }
 
     /// Get an iterator of all module tags.
+    #[must_use]
     pub fn module_tags(&self) -> ModuleIter {
         module::module_iter(self.tags())
     }
@@ -293,21 +309,25 @@ impl<'a> BootInformation<'a> {
     }*/
 
     /// Search for the (ACPI 1.0) RSDP tag.
+    #[must_use]
     pub fn rsdp_v1_tag(&self) -> Option<&RsdpV1Tag> {
         self.get_tag::<RsdpV1Tag>()
     }
 
     /// Search for the (ACPI 2.0 or later) RSDP tag.
+    #[must_use]
     pub fn rsdp_v2_tag(&self) -> Option<&RsdpV2Tag> {
         self.get_tag::<RsdpV2Tag>()
     }
 
     /// Search for the SMBIOS tag.
+    #[must_use]
     pub fn smbios_tag(&self) -> Option<&SmbiosTag> {
         self.get_tag::<SmbiosTag>()
     }
 
     /// Search for the VBE information tag.
+    #[must_use]
     pub fn vbe_info_tag(&self) -> Option<&VBEInfoTag> {
         self.get_tag::<VBEInfoTag>()
     }
@@ -367,6 +387,7 @@ impl<'a> BootInformation<'a> {
     ///     .unwrap();
     /// assert_eq!(tag.name(), Ok("name"));
     /// ```
+    #[must_use]
     pub fn get_tag<TagT: TagTrait + ?Sized + 'a>(&'a self) -> Option<&'a TagT> {
         self.tags()
             .find(|tag| tag.typ == TagT::ID)

+ 2 - 1
multiboot2/src/boot_loader_name.rs

@@ -20,6 +20,7 @@ pub struct BootLoaderNameTag {
 
 impl BootLoaderNameTag {
     #[cfg(feature = "builder")]
+    #[must_use]
     pub fn new(name: &str) -> BoxedDst<Self> {
         let mut bytes: Vec<_> = name.bytes().collect();
         if !bytes.ends_with(&[0]) {
@@ -100,7 +101,7 @@ mod tests {
         let tag = get_bytes();
         let tag = unsafe { &*tag.as_ptr().cast::<Tag>() };
         let tag = tag.cast_tag::<BootLoaderNameTag>();
-        assert_eq!({ tag.typ }, TagType::BootLoaderName);
+        assert_eq!(tag.header.typ, TagType::BootLoaderName);
         assert_eq!(tag.name().expect("must be valid UTF-8"), MSG);
     }
 

+ 1 - 1
multiboot2/src/builder/boxed_dst.rs

@@ -144,7 +144,7 @@ mod tests {
 
     #[test]
     fn can_hold_tag_trait() {
-        fn consume<T: TagTrait + ?Sized>(_: &T) {}
+        const fn consume<T: TagTrait + ?Sized>(_: &T) {}
         let content = b"hallo\0";
 
         let tag = BoxedDst::<CustomTag>::new(content);

+ 21 - 1
multiboot2/src/builder/information.rs

@@ -73,6 +73,7 @@ impl Default for InformationBuilder {
 
 impl InformationBuilder {
     /// Creates a new builder.
+    #[must_use]
     pub const fn new() -> Self {
         Self(Vec::new())
     }
@@ -87,6 +88,7 @@ impl InformationBuilder {
     /// [`Self::build`]-method is called. This function assumes that the begin
     /// of the boot information is 8-byte aligned and automatically adds padding
     /// between tags to ensure that each tag is 8-byte aligned.
+    #[must_use]
     pub fn expected_len(&self) -> usize {
         let tag_size_iter = self.0.iter().map(|(_, bytes)| bytes.len());
 
@@ -118,6 +120,7 @@ impl InformationBuilder {
     }
 
     /// Constructs the bytes for a valid Multiboot2 information with the given properties.
+    #[must_use]
     pub fn build(self) -> BootInformationBytes {
         const ALIGN: usize = 8;
 
@@ -202,92 +205,109 @@ impl InformationBuilder {
     }
 
     /// Adds a 'basic memory information' tag (represented by [`BasicMemoryInfoTag`]) to the builder.
+    #[must_use]
     pub fn basic_memory_info_tag(self, tag: BasicMemoryInfoTag) -> Self {
         self.add_tag(&tag).unwrap()
     }
 
     /// Adds a 'bootloader name' tag (represented by [`BootLoaderNameTag`]) to the builder.
+    #[must_use]
     pub fn bootloader_name_tag(self, tag: BoxedDst<BootLoaderNameTag>) -> Self {
         self.add_tag(&*tag).unwrap()
     }
 
     /// Adds a 'command line' tag (represented by [`CommandLineTag`]) to the builder.
+    #[must_use]
     pub fn command_line_tag(self, tag: BoxedDst<CommandLineTag>) -> Self {
         self.add_tag(&*tag).unwrap()
     }
 
     /// Adds a 'EFI 32-bit system table pointer' tag (represented by [`EFISdt32Tag`]) to the builder.
+    #[must_use]
     pub fn efisdt32_tag(self, tag: EFISdt32Tag) -> Self {
         self.add_tag(&tag).unwrap()
     }
 
     /// Adds a 'EFI 64-bit system table pointer' tag (represented by [`EFISdt64Tag`]) to the builder.
+    #[must_use]
     pub fn efisdt64_tag(self, tag: EFISdt64Tag) -> Self {
         self.add_tag(&tag).unwrap()
     }
 
     /// Adds a 'EFI boot services not terminated' tag (represented by [`EFIBootServicesNotExitedTag`]) to the builder.
+    #[must_use]
     pub fn efi_boot_services_not_exited_tag(self) -> Self {
         self.add_tag(&EFIBootServicesNotExitedTag::new()).unwrap()
     }
 
     /// Adds a 'EFI 32-bit image handle pointer' tag (represented by [`EFIImageHandle32Tag`]) to the builder.
+    #[must_use]
     pub fn efi_image_handle32(self, tag: EFIImageHandle32Tag) -> Self {
         self.add_tag(&tag).unwrap()
     }
 
     /// Adds a 'EFI 64-bit image handle pointer' tag (represented by [`EFIImageHandle64Tag`]) to the builder.
+    #[must_use]
     pub fn efi_image_handle64(self, tag: EFIImageHandle64Tag) -> Self {
         self.add_tag(&tag).unwrap()
     }
 
     /// Adds a 'EFI Memory map' tag (represented by [`EFIMemoryMapTag`]) to the builder.
+    #[must_use]
     pub fn efi_memory_map_tag(self, tag: BoxedDst<EFIMemoryMapTag>) -> Self {
         self.add_tag(&*tag).unwrap()
     }
 
     /// Adds a 'ELF-Symbols' tag (represented by [`ElfSectionsTag`]) to the builder.
+    #[must_use]
     pub fn elf_sections_tag(self, tag: BoxedDst<ElfSectionsTag>) -> Self {
         self.add_tag(&*tag).unwrap()
     }
 
     /// Adds a 'Framebuffer info' tag (represented by [`FramebufferTag`]) to the builder.
+    #[must_use]
     pub fn framebuffer_tag(self, tag: BoxedDst<FramebufferTag>) -> Self {
         self.add_tag(&*tag).unwrap()
     }
 
     /// Adds a 'Image load base physical address' tag (represented by [`ImageLoadPhysAddrTag`]) to the builder.
+    #[must_use]
     pub fn image_load_addr(self, tag: ImageLoadPhysAddrTag) -> Self {
         self.add_tag(&tag).unwrap()
     }
 
     /// Adds a (*none EFI*) 'memory map' tag (represented by [`MemoryMapTag`]) to the builder.
+    #[must_use]
     pub fn memory_map_tag(self, tag: BoxedDst<MemoryMapTag>) -> Self {
         self.add_tag(&*tag).unwrap()
     }
 
     /// Adds a 'Modules' tag (represented by [`ModuleTag`]) to the builder.
     /// This tag can occur multiple times in boot information.
+    #[must_use]
     pub fn add_module_tag(self, tag: BoxedDst<ModuleTag>) -> Self {
         self.add_tag(&*tag).unwrap()
     }
 
     /// Adds a 'ACPI old RSDP' tag (represented by [`RsdpV1Tag`]) to the builder.
+    #[must_use]
     pub fn rsdp_v1_tag(self, tag: RsdpV1Tag) -> Self {
         self.add_tag(&tag).unwrap()
     }
 
     /// Adds a 'ACPI new RSDP' tag (represented by [`RsdpV2Tag`]) to the builder.
+    #[must_use]
     pub fn rsdp_v2_tag(self, tag: RsdpV2Tag) -> Self {
         self.add_tag(&tag).unwrap()
     }
 
     /// Adds a 'SMBIOS tables' tag (represented by [`SmbiosTag`]) to the builder.
+    #[must_use]
     pub fn smbios_tag(self, tag: BoxedDst<SmbiosTag>) -> Self {
         self.add_tag(&*tag).unwrap()
     }
 
-    fn tag_is_allowed_multiple_times(tag_type: TagType) -> bool {
+    const fn tag_is_allowed_multiple_times(tag_type: TagType) -> bool {
         matches!(
             tag_type,
             TagType::Module | TagType::Smbios | TagType::Custom(_)

+ 3 - 2
multiboot2/src/command_line.rs

@@ -8,7 +8,7 @@ use core::str;
 #[cfg(feature = "builder")]
 use {crate::builder::BoxedDst, alloc::vec::Vec};
 
-pub(crate) const METADATA_SIZE: usize = mem::size_of::<TagTypeId>() + mem::size_of::<u32>();
+pub const METADATA_SIZE: usize = mem::size_of::<TagTypeId>() + mem::size_of::<u32>();
 
 /// This tag contains the command line string.
 ///
@@ -25,6 +25,7 @@ pub struct CommandLineTag {
 impl CommandLineTag {
     /// Create a new command line tag from the given string.
     #[cfg(feature = "builder")]
+    #[must_use]
     pub fn new(command_line: &str) -> BoxedDst<Self> {
         let mut bytes: Vec<_> = command_line.bytes().collect();
         if !bytes.ends_with(&[0]) {
@@ -107,7 +108,7 @@ mod tests {
         let tag = get_bytes();
         let tag = unsafe { &*tag.as_ptr().cast::<Tag>() };
         let tag = tag.cast_tag::<CommandLineTag>();
-        assert_eq!({ tag.typ }, TagType::Cmdline);
+        assert_eq!(tag.header.typ, TagType::Cmdline);
         assert_eq!(tag.cmdline().expect("must be valid UTF-8"), MSG);
     }
 

+ 13 - 4
multiboot2/src/efi.rs

@@ -20,6 +20,7 @@ pub struct EFISdt32Tag {
 
 impl EFISdt32Tag {
     /// Create a new tag to pass the EFI32 System Table pointer.
+    #[must_use]
     pub fn new(pointer: u32) -> Self {
         Self {
             header: TagHeader::new(Self::ID, size_of::<Self>().try_into().unwrap()),
@@ -28,7 +29,8 @@ impl EFISdt32Tag {
     }
 
     /// The physical address of a i386 EFI system table.
-    pub fn sdt_address(&self) -> usize {
+    #[must_use]
+    pub const fn sdt_address(&self) -> usize {
         self.pointer as usize
     }
 }
@@ -49,6 +51,7 @@ pub struct EFISdt64Tag {
 
 impl EFISdt64Tag {
     /// Create a new tag to pass the EFI64 System Table pointer.
+    #[must_use]
     pub fn new(pointer: u64) -> Self {
         Self {
             header: TagHeader::new(Self::ID, size_of::<Self>().try_into().unwrap()),
@@ -57,7 +60,8 @@ impl EFISdt64Tag {
     }
 
     /// The physical address of a x86_64 EFI system table.
-    pub fn sdt_address(&self) -> usize {
+    #[must_use]
+    pub const fn sdt_address(&self) -> usize {
         self.pointer as usize
     }
 }
@@ -79,6 +83,7 @@ pub struct EFIImageHandle32Tag {
 
 impl EFIImageHandle32Tag {
     #[cfg(feature = "builder")]
+    #[must_use]
     pub fn new(pointer: u32) -> Self {
         Self {
             header: TagHeader::new(Self::ID, size_of::<Self>().try_into().unwrap()),
@@ -87,7 +92,8 @@ impl EFIImageHandle32Tag {
     }
 
     /// Returns the physical address of the EFI image handle.
-    pub fn image_handle(&self) -> usize {
+    #[must_use]
+    pub const fn image_handle(&self) -> usize {
         self.pointer as usize
     }
 }
@@ -109,6 +115,7 @@ pub struct EFIImageHandle64Tag {
 
 impl EFIImageHandle64Tag {
     #[cfg(feature = "builder")]
+    #[must_use]
     pub fn new(pointer: u64) -> Self {
         Self {
             header: TagHeader::new(Self::ID, size_of::<Self>().try_into().unwrap()),
@@ -117,7 +124,8 @@ impl EFIImageHandle64Tag {
     }
 
     /// Returns the physical address of the EFI image handle.
-    pub fn image_handle(&self) -> usize {
+    #[must_use]
+    pub const fn image_handle(&self) -> usize {
         self.pointer as usize
     }
 }
@@ -137,6 +145,7 @@ pub struct EFIBootServicesNotExitedTag {
 
 impl EFIBootServicesNotExitedTag {
     #[cfg(feature = "builder")]
+    #[must_use]
     pub fn new() -> Self {
         Self::default()
     }

+ 11 - 2
multiboot2/src/elf_sections.rs

@@ -26,6 +26,7 @@ pub struct ElfSectionsTag {
 impl ElfSectionsTag {
     /// Create a new ElfSectionsTag with the given data.
     #[cfg(feature = "builder")]
+    #[must_use]
     pub fn new(
         number_of_sections: u32,
         entry_size: u32,
@@ -43,7 +44,7 @@ impl ElfSectionsTag {
     }
 
     /// Get an iterator of loaded ELF sections.
-    pub(crate) fn sections(&self) -> ElfSectionIter {
+    pub(crate) const fn sections(&self) -> ElfSectionIter {
         let string_section_offset = (self.shndx * self.entry_size) as isize;
         let string_section_ptr =
             unsafe { self.first_section().offset(string_section_offset) as *const _ };
@@ -55,7 +56,7 @@ impl ElfSectionsTag {
         }
     }
 
-    fn first_section(&self) -> *const u8 {
+    const fn first_section(&self) -> *const u8 {
         &(self.sections[0]) as *const _
     }
 }
@@ -174,6 +175,7 @@ struct ElfSectionInner64 {
 
 impl ElfSection {
     /// Get the section type as a `ElfSectionType` enum variant.
+    #[must_use]
     pub fn section_type(&self) -> ElfSectionType {
         match self.get().typ() {
             0 => ElfSectionType::Unused,
@@ -201,6 +203,7 @@ impl ElfSection {
     }
 
     /// Get the "raw" section type as a `u32`
+    #[must_use]
     pub fn section_type_raw(&self) -> u32 {
         self.get().typ()
     }
@@ -224,6 +227,7 @@ impl ElfSection {
     }
 
     /// Get the physical start address of the section.
+    #[must_use]
     pub fn start_address(&self) -> u64 {
         self.get().addr()
     }
@@ -231,11 +235,13 @@ impl ElfSection {
     /// Get the physical end address of the section.
     ///
     /// This is the same as doing `section.start_address() + section.size()`
+    #[must_use]
     pub fn end_address(&self) -> u64 {
         self.get().addr() + self.get().size()
     }
 
     /// Get the section's size in bytes.
+    #[must_use]
     pub fn size(&self) -> u64 {
         self.get().size()
     }
@@ -246,16 +252,19 @@ impl ElfSection {
     /// modulo the value of `addrlign`. Currently, only 0 and positive
     /// integral powers of two are allowed. Values 0 and 1 mean the section has no
     /// alignment constraints.
+    #[must_use]
     pub fn addralign(&self) -> u64 {
         self.get().addralign()
     }
 
     /// Get the section's flags.
+    #[must_use]
     pub fn flags(&self) -> ElfSectionFlags {
         ElfSectionFlags::from_bits_truncate(self.get().flags())
     }
 
     /// Check if the `ALLOCATED` flag is set in the section flags.
+    #[must_use]
     pub fn is_allocated(&self) -> bool {
         self.flags().contains(ElfSectionFlags::ALLOCATED)
     }

+ 13 - 7
multiboot2/src/framebuffer.rs

@@ -17,8 +17,8 @@ struct Reader {
 }
 
 impl Reader {
-    fn new<T>(ptr: *const T) -> Reader {
-        Reader {
+    const fn new<T>(ptr: *const T) -> Self {
+        Self {
             ptr: ptr as *const u8,
             off: 0,
         }
@@ -85,6 +85,7 @@ pub struct FramebufferTag {
 
 impl FramebufferTag {
     #[cfg(feature = "builder")]
+    #[must_use]
     pub fn new(
         address: u64,
         pitch: u32,
@@ -107,27 +108,32 @@ impl FramebufferTag {
     /// This field is 64-bit wide but bootloader should set it under 4GiB if
     /// possible for compatibility with payloads which aren’t aware of PAE or
     /// amd64.
-    pub fn address(&self) -> u64 {
+    #[must_use]
+    pub const fn address(&self) -> u64 {
         self.address
     }
 
     /// Contains the pitch in bytes.
-    pub fn pitch(&self) -> u32 {
+    #[must_use]
+    pub const fn pitch(&self) -> u32 {
         self.pitch
     }
 
     /// Contains framebuffer width in pixels.
-    pub fn width(&self) -> u32 {
+    #[must_use]
+    pub const fn width(&self) -> u32 {
         self.width
     }
 
     /// Contains framebuffer height in pixels.
-    pub fn height(&self) -> u32 {
+    #[must_use]
+    pub const fn height(&self) -> u32 {
         self.height
     }
 
     /// Contains number of bits per pixel.
-    pub fn bpp(&self) -> u8 {
+    #[must_use]
+    pub const fn bpp(&self) -> u8 {
         self.bpp
     }
 

+ 3 - 1
multiboot2/src/image_load_addr.rs

@@ -17,6 +17,7 @@ pub struct ImageLoadPhysAddrTag {
 
 impl ImageLoadPhysAddrTag {
     #[cfg(feature = "builder")]
+    #[must_use]
     pub fn new(load_base_addr: u32) -> Self {
         Self {
             header: TagHeader::new(Self::ID, size_of::<Self>().try_into().unwrap()),
@@ -25,7 +26,8 @@ impl ImageLoadPhysAddrTag {
     }
 
     /// Returns the load base address.
-    pub fn load_base_addr(&self) -> u32 {
+    #[must_use]
+    pub const fn load_base_addr(&self) -> u32 {
         self.load_base_addr
     }
 }

+ 15 - 5
multiboot2/src/lib.rs

@@ -1,12 +1,20 @@
 #![no_std]
 #![cfg_attr(feature = "unstable", feature(error_in_core))]
-#![deny(missing_debug_implementations)]
 // --- BEGIN STYLE CHECKS ---
-// These checks are optional in CI for PRs, as discussed in
-// https://github.com/rust-osdev/multiboot2/pull/92
-#![deny(clippy::all)]
+#![deny(
+    clippy::all,
+    clippy::cargo,
+    clippy::nursery,
+    clippy::must_use_candidate,
+    // clippy::restriction,
+    // clippy::pedantic
+)]
+// now allow a few rules which are denied by the above statement
+// --> They are either ridiculous, not necessary, or we can't fix them.
+#![allow(clippy::multiple_crate_versions)]
+// #![deny(missing_docs)]
+#![deny(missing_debug_implementations)]
 #![deny(rustdoc::all)]
-#![allow(rustdoc::private_doc_tests)]
 // --- END STYLE CHECKS ---
 
 //! Library that assists parsing the Multiboot2 Information Structure (MBI) from
@@ -372,6 +380,7 @@ mod tests {
 
     #[test]
     #[cfg_attr(miri, ignore)]
+    #[allow(clippy::cognitive_complexity)]
     fn vbe_info_tag() {
         //Taken from GRUB2 running in QEMU.
         #[repr(C, align(8))]
@@ -813,6 +822,7 @@ mod tests {
     }
 
     /// Helper for [`grub2`].
+    #[allow(clippy::cognitive_complexity)]
     fn test_grub2_boot_info(
         bi: &BootInformation,
         addr: usize,

+ 23 - 9
multiboot2/src/memory_map.rs

@@ -36,6 +36,7 @@ pub struct MemoryMapTag {
 
 impl MemoryMapTag {
     #[cfg(feature = "builder")]
+    #[must_use]
     pub fn new(areas: &[MemoryArea]) -> BoxedDst<Self> {
         let entry_size: u32 = mem::size_of::<MemoryArea>().try_into().unwrap();
         let entry_version: u32 = 0;
@@ -47,12 +48,14 @@ impl MemoryMapTag {
     }
 
     /// Returns the entry size.
-    pub fn entry_size(&self) -> u32 {
+    #[must_use]
+    pub const fn entry_size(&self) -> u32 {
         self.entry_size
     }
 
     /// Returns the entry version.
-    pub fn entry_version(&self) -> u32 {
+    #[must_use]
+    pub const fn entry_version(&self) -> u32 {
         self.entry_version
     }
 
@@ -60,6 +63,7 @@ impl MemoryMapTag {
     ///
     /// Usually, this should already reflect the memory consumed by the
     /// code running this.
+    #[must_use]
     pub fn memory_areas(&self) -> &[MemoryArea] {
         // If this ever fails, we need to model this differently in this crate.
         assert_eq!(self.entry_size as usize, mem::size_of::<MemoryArea>());
@@ -100,22 +104,26 @@ impl MemoryArea {
     }
 
     /// The start address of the memory region.
-    pub fn start_address(&self) -> u64 {
+    #[must_use]
+    pub const fn start_address(&self) -> u64 {
         self.base_addr
     }
 
     /// The end address of the memory region.
-    pub fn end_address(&self) -> u64 {
+    #[must_use]
+    pub const fn end_address(&self) -> u64 {
         self.base_addr + self.length
     }
 
     /// The size, in bytes, of the memory region.
-    pub fn size(&self) -> u64 {
+    #[must_use]
+    pub const fn size(&self) -> u64 {
         self.length
     }
 
     /// The type of the memory region.
-    pub fn typ(&self) -> MemoryAreaTypeId {
+    #[must_use]
+    pub const fn typ(&self) -> MemoryAreaTypeId {
         self.typ
     }
 }
@@ -215,7 +223,7 @@ impl From<MemoryAreaType> for MemoryAreaTypeId {
 
 impl PartialEq<MemoryAreaType> for MemoryAreaTypeId {
     fn eq(&self, other: &MemoryAreaType) -> bool {
-        let val: MemoryAreaTypeId = (*other).into();
+        let val: Self = (*other).into();
         let val: u32 = val.0;
         self.0.eq(&val)
     }
@@ -252,6 +260,7 @@ pub struct BasicMemoryInfoTag {
 }
 
 impl BasicMemoryInfoTag {
+    #[must_use]
     pub fn new(memory_lower: u32, memory_upper: u32) -> Self {
         Self {
             header: TagHeader::new(Self::ID, size_of::<Self>().try_into().unwrap()),
@@ -260,11 +269,13 @@ impl BasicMemoryInfoTag {
         }
     }
 
-    pub fn memory_lower(&self) -> u32 {
+    #[must_use]
+    pub const fn memory_lower(&self) -> u32 {
         self.memory_lower
     }
 
-    pub fn memory_upper(&self) -> u32 {
+    #[must_use]
+    pub const fn memory_upper(&self) -> u32 {
         self.memory_upper
     }
 }
@@ -310,6 +321,7 @@ impl EFIMemoryMapTag {
     /// Create a new EFI memory map tag with the given memory descriptors.
     /// Version and size can't be set because you're passing a slice of
     /// EFIMemoryDescs, not the ones you might have gotten from the firmware.
+    #[must_use]
     pub fn new_from_descs(descs: &[EFIMemoryDesc]) -> BoxedDst<Self> {
         // TODO replace this EfiMemorydesc::uefi_desc_size() in the next uefi_raw
         // release.
@@ -337,6 +349,7 @@ impl EFIMemoryMapTag {
 
     #[cfg(feature = "builder")]
     /// Create a new EFI memory map tag from the given EFI memory map.
+    #[must_use]
     pub fn new_from_map(desc_size: u32, desc_version: u32, efi_mmap: &[u8]) -> BoxedDst<Self> {
         assert!(desc_size > 0);
         assert_eq!(efi_mmap.len() % desc_size as usize, 0);
@@ -359,6 +372,7 @@ impl EFIMemoryMapTag {
     ///
     /// Usually, this should already reflect the memory consumed by the
     /// code running this.
+    #[must_use]
     pub fn memory_areas(&self) -> EFIMemoryAreaIter {
         // If this ever fails, this needs to be refactored in a joint-effort
         // with the uefi-rs project to have all corresponding typings.

+ 9 - 5
multiboot2/src/module.rs

@@ -24,6 +24,7 @@ pub struct ModuleTag {
 
 impl ModuleTag {
     #[cfg(feature = "builder")]
+    #[must_use]
     pub fn new(start: u32, end: u32, cmdline: &str) -> BoxedDst<Self> {
         assert!(end > start, "must have a size");
 
@@ -53,17 +54,20 @@ impl ModuleTag {
     }
 
     /// Start address of the module.
-    pub fn start_address(&self) -> u32 {
+    #[must_use]
+    pub const fn start_address(&self) -> u32 {
         self.mod_start
     }
 
     /// End address of the module
-    pub fn end_address(&self) -> u32 {
+    #[must_use]
+    pub const fn end_address(&self) -> u32 {
         self.mod_end
     }
 
     /// The size of the module/the BLOB in memory.
-    pub fn module_size(&self) -> u32 {
+    #[must_use]
+    pub const fn module_size(&self) -> u32 {
         self.mod_end - self.mod_start
     }
 }
@@ -91,7 +95,7 @@ impl Debug for ModuleTag {
     }
 }
 
-pub fn module_iter(iter: TagIter) -> ModuleIter {
+pub const fn module_iter(iter: TagIter) -> ModuleIter {
     ModuleIter { iter }
 }
 
@@ -154,7 +158,7 @@ mod tests {
         let tag = get_bytes();
         let tag = unsafe { &*tag.as_ptr().cast::<Tag>() };
         let tag = tag.cast_tag::<ModuleTag>();
-        assert_eq!({ tag.typ }, TagType::Module);
+        assert_eq!(tag.header.typ, TagType::Module);
         assert_eq!(tag.cmdline().expect("must be valid UTF-8"), MSG);
     }
 

+ 18 - 9
multiboot2/src/rsdp.rs

@@ -36,6 +36,7 @@ pub struct RsdpV1Tag {
 
 impl RsdpV1Tag {
     #[cfg(feature = "builder")]
+    #[must_use]
     pub fn new(
         signature: [u8; 8],
         checksum: u8,
@@ -56,11 +57,12 @@ impl RsdpV1Tag {
     /// The "RSD PTR " marker signature.
     ///
     /// This is originally a 8-byte C string (not null terminated!) that must contain "RSD PTR "
-    pub fn signature(&self) -> Result<&str, Utf8Error> {
+    pub const fn signature(&self) -> Result<&str, Utf8Error> {
         str::from_utf8(&self.signature)
     }
 
     /// Validation of the RSDPv1 checksum
+    #[must_use]
     pub fn checksum_is_valid(&self) -> bool {
         let bytes =
             unsafe { slice::from_raw_parts(self as *const _ as *const u8, RSDPV1_LENGTH + 8) };
@@ -71,17 +73,19 @@ impl RsdpV1Tag {
     }
 
     /// An OEM-supplied string that identifies the OEM.
-    pub fn oem_id(&self) -> Result<&str, Utf8Error> {
+    pub const fn oem_id(&self) -> Result<&str, Utf8Error> {
         str::from_utf8(&self.oem_id)
     }
 
     /// The revision of the ACPI.
-    pub fn revision(&self) -> u8 {
+    #[must_use]
+    pub const fn revision(&self) -> u8 {
         self.revision
     }
 
     /// The physical (I repeat: physical) address of the RSDT table.
-    pub fn rsdt_address(&self) -> usize {
+    #[must_use]
+    pub const fn rsdt_address(&self) -> usize {
         self.rsdt_address as usize
     }
 }
@@ -112,6 +116,7 @@ pub struct RsdpV2Tag {
 impl RsdpV2Tag {
     #[cfg(feature = "builder")]
     #[allow(clippy::too_many_arguments)]
+    #[must_use]
     pub fn new(
         signature: [u8; 8],
         checksum: u8,
@@ -139,11 +144,12 @@ impl RsdpV2Tag {
     /// The "RSD PTR " marker signature.
     ///
     /// This is originally a 8-byte C string (not null terminated!) that must contain "RSD PTR ".
-    pub fn signature(&self) -> Result<&str, Utf8Error> {
+    pub const fn signature(&self) -> Result<&str, Utf8Error> {
         str::from_utf8(&self.signature)
     }
 
     /// Validation of the RSDPv2 extended checksum
+    #[must_use]
     pub fn checksum_is_valid(&self) -> bool {
         let bytes = unsafe {
             slice::from_raw_parts(self as *const _ as *const u8, self.length as usize + 8)
@@ -155,24 +161,27 @@ impl RsdpV2Tag {
     }
 
     /// An OEM-supplied string that identifies the OEM.
-    pub fn oem_id(&self) -> Result<&str, Utf8Error> {
+    pub const fn oem_id(&self) -> Result<&str, Utf8Error> {
         str::from_utf8(&self.oem_id)
     }
 
     /// The revision of the ACPI.
-    pub fn revision(&self) -> u8 {
+    #[must_use]
+    pub const fn revision(&self) -> u8 {
         self.revision
     }
 
     /// Physical address of the XSDT table.
     ///
     /// On x86, this is truncated from 64-bit to 32-bit.
-    pub fn xsdt_address(&self) -> usize {
+    #[must_use]
+    pub const fn xsdt_address(&self) -> usize {
         self.xsdt_address as usize
     }
 
     /// This field is used to calculate the checksum of the entire table, including both checksum fields.
-    pub fn ext_checksum(&self) -> u8 {
+    #[must_use]
+    pub const fn ext_checksum(&self) -> u8 {
         self.ext_checksum
     }
 }

+ 1 - 0
multiboot2/src/smbios.rs

@@ -23,6 +23,7 @@ pub struct SmbiosTag {
 
 impl SmbiosTag {
     #[cfg(feature = "builder")]
+    #[must_use]
     pub fn new(major: u8, minor: u8, tables: &[u8]) -> BoxedDst<Self> {
         let mut bytes = [major, minor, 0, 0, 0, 0, 0, 0].to_vec();
         bytes.extend(tables);

+ 4 - 2
multiboot2/src/tag.rs

@@ -28,8 +28,8 @@ impl Display for StringError {
 impl core::error::Error for StringError {
     fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
         match self {
-            StringError::MissingNul(e) => Some(e),
-            StringError::Utf8(e) => Some(e),
+            Self::MissingNul(e) => Some(e),
+            Self::Utf8(e) => Some(e),
         }
     }
 }
@@ -75,11 +75,13 @@ pub struct Tag {
 
 impl Tag {
     /// Returns the underlying type of the tag.
+    #[must_use]
     pub fn typ(&self) -> TagType {
         self.typ.into()
     }
 
     /// Casts the base tag to the specific tag type.
+    #[must_use]
     pub fn cast_tag<'a, T: TagTrait + ?Sized + 'a>(&'a self) -> &'a T {
         assert_eq!(self.typ, T::ID);
         // Safety: At this point, we trust that "self.size" and the size hint

+ 1 - 0
multiboot2/src/tag_trait.rs

@@ -51,6 +51,7 @@ pub trait TagTrait: Pointee {
     /// Callers must be sure that the "size" field of the provided [`Tag`] is
     /// sane and the underlying memory valid. The implementation of this trait
     /// **must have** a correct [`Self::dst_size`] implementation.
+    #[must_use]
     unsafe fn from_base_tag(tag: &Tag) -> &Self {
         let ptr = core::ptr::addr_of!(*tag);
         let ptr = ptr_meta::from_raw_parts(ptr.cast(), Self::dst_size(tag));

+ 28 - 26
multiboot2/src/tag_type.rs

@@ -17,7 +17,8 @@ pub struct TagTypeId(u32);
 
 impl TagTypeId {
     /// Constructor.
-    pub fn new(val: u32) -> Self {
+    #[must_use]
+    pub const fn new(val: u32) -> Self {
         Self(val)
     }
 }
@@ -135,6 +136,7 @@ pub enum TagType {
 
 impl TagType {
     /// Convenient wrapper to get the underlying `u32` representation of the tag.
+    #[must_use]
     pub fn val(&self) -> u32 {
         u32::from(*self)
     }
@@ -161,29 +163,29 @@ mod primitive_conversion_impls {
     impl From<u32> for TagType {
         fn from(value: u32) -> Self {
             match value {
-                0 => TagType::End,
-                1 => TagType::Cmdline,
-                2 => TagType::BootLoaderName,
-                3 => TagType::Module,
-                4 => TagType::BasicMeminfo,
-                5 => TagType::Bootdev,
-                6 => TagType::Mmap,
-                7 => TagType::Vbe,
-                8 => TagType::Framebuffer,
-                9 => TagType::ElfSections,
-                10 => TagType::Apm,
-                11 => TagType::Efi32,
-                12 => TagType::Efi64,
-                13 => TagType::Smbios,
-                14 => TagType::AcpiV1,
-                15 => TagType::AcpiV2,
-                16 => TagType::Network,
-                17 => TagType::EfiMmap,
-                18 => TagType::EfiBs,
-                19 => TagType::Efi32Ih,
-                20 => TagType::Efi64Ih,
-                21 => TagType::LoadBaseAddr,
-                c => TagType::Custom(c),
+                0 => Self::End,
+                1 => Self::Cmdline,
+                2 => Self::BootLoaderName,
+                3 => Self::Module,
+                4 => Self::BasicMeminfo,
+                5 => Self::Bootdev,
+                6 => Self::Mmap,
+                7 => Self::Vbe,
+                8 => Self::Framebuffer,
+                9 => Self::ElfSections,
+                10 => Self::Apm,
+                11 => Self::Efi32,
+                12 => Self::Efi64,
+                13 => Self::Smbios,
+                14 => Self::AcpiV1,
+                15 => Self::AcpiV2,
+                16 => Self::Network,
+                17 => Self::EfiMmap,
+                18 => Self::EfiBs,
+                19 => Self::Efi32Ih,
+                20 => Self::Efi64Ih,
+                21 => Self::LoadBaseAddr,
+                c => Self::Custom(c),
             }
         }
     }
@@ -226,14 +228,14 @@ mod intermediate_conversion_impls {
     impl From<TagTypeId> for TagType {
         fn from(value: TagTypeId) -> Self {
             let value = u32::from(value);
-            TagType::from(value)
+            Self::from(value)
         }
     }
 
     impl From<TagType> for TagTypeId {
         fn from(value: TagType) -> Self {
             let value = u32::from(value);
-            TagTypeId::from(value)
+            Self::from(value)
         }
     }
 }