Skip to content

Conversation

@phip1611
Copy link
Member

@phip1611 phip1611 commented Nov 24, 2025

This is a series of similar commits to clean up obsolete extern crate statements

Since Rust 1.30, normal macros can be imported via use, and with Rust 1.31 and edition 2018 this has become the preferred approach. extern crate is only needed for alloc in no_std crates, which does not apply here.

By dropping these (often redundant or odd) extern crate lines, we
expose the actual dependencies more clearly and reduce technical debt.

Part of #7489

I actually did this mostly in September but I just discovered this patch series again. I rebased everything and IMHO it is a clear improvement to code quality.

@phip1611 phip1611 self-assigned this Nov 24, 2025
pub mod regs;

#[cfg(feature = "tdx")]
pub mod tdx;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here I also reordered statements a little: first pub modules, then private modules. Instead of mixing modules and use with each other


use crate::{GuestMemoryMmap, InitramfsConfig, RegionType};
mod smbios;
use std::arch::x86_64;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment above

@phip1611 phip1611 force-pushed the remove-extern-crate branch from 2d12550 to d196b2f Compare November 24, 2025 13:05
This commit is the first in a series of similar commits to clean up
obsolete `extern crate` statements

Since Rust 1.30, normal macros can be imported via `use`, and with Rust
1.31 and edition 2018 this has become the preferred approach.
`extern crate` is only needed for `alloc` in `no_std` crates, which does
not apply here.

By dropping these (often redundant or odd) `extern crate` lines, we
expose the actual dependencies more clearly and reduce technical debt.

## Auto-generation of the series

Most of this series was produced automatically:

1. Removed all "extern crate" references
2. Run the script [0] to add missing `use` statements
3. Run `cargo +nightly fmt --all`
4. Fix the remaining problems manually

The treewide changes were then split into per-folder commits.

[0]
```python
import os
import re

# Mapping of macro/function usage to imports
MACRO_IMPORTS = {
    "info!": "use log::info;\n",
    "debug!": "use log::debug;\n",
    "error!": "use log::error;\n",
    "trace!": "use log::trace;\n",
    "warn!": "use log::warn;\n",
    "event!": "use event_monitor::event;\n",
    "anyhow!(": "use anyhow::anyhow;\n",
    "bitflags!(": "use bitflags::bitflags;\n",
    "ioctl_ior_nr!": "use vmm_sys_util::{ioctl_ior_nr};\n",
    "ioctl_iow_nr!": "use vmm_sys_util::{ioctl_iow_nr};\n",
}

# Regex for finding the first use statement
USE_REGEX = re.compile(r"^\s*(use|pub use) .+?;")

def process_file(path):
    with open(path, "r", encoding="utf-8") as f:
        lines = f.readlines()

    content = "".join(lines)
    existing_imports = set(lines)
    needed_imports = set()

    # Check macros/functions against mapping, only add if not already present
    for key, import_stmt in MACRO_IMPORTS.items():
        if key in content and import_stmt not in existing_imports:
            needed_imports.add(import_stmt)

    if not needed_imports:
        print(f"Unmodified {path} (no new imports needed)")
        return  # Nothing to do

    # Find first use or pub use statement
    for i, line in enumerate(lines):
        if USE_REGEX.match(line):
            insertion_index = i + 1
            break
    else:
        print(f"Unmodified {path} (no use or pub use statement found)")
        return  # No use statement found, skip file

    # Insert imports
    lines[insertion_index:insertion_index] = list(needed_imports)

    # Write back file
    with open(path, "w", encoding="utf-8") as f:
        f.writelines(lines)

    print(f"Modified {path}, added imports: {''.join(needed_imports).strip()}")

for root, _, files in os.walk("."):
    for file in files:
        if file.endswith(".rs"):
            process_file(os.path.join(root, file))
```

Signed-off-by: Philipp Schuster <[email protected]>
On-behalf-of: SAP [email protected]
This commit is part of a series of similar commits.

Signed-off-by: Philipp Schuster <[email protected]>
On-behalf-of: SAP [email protected]
This commit is part of a series of similar commits.

Signed-off-by: Philipp Schuster <[email protected]>
On-behalf-of: SAP [email protected]
@phip1611 phip1611 force-pushed the remove-extern-crate branch 2 times, most recently from 04c6f88 to 797e5bc Compare November 24, 2025 13:26
@phip1611 phip1611 marked this pull request as ready for review November 24, 2025 13:26
@phip1611 phip1611 requested a review from a team as a code owner November 24, 2025 13:26
@phip1611 phip1611 force-pushed the remove-extern-crate branch from 797e5bc to 01b3bff Compare November 24, 2025 15:00
This commit is part of a series of similar commits.

Signed-off-by: Philipp Schuster <[email protected]>
On-behalf-of: SAP [email protected]
This commit is part of a series of similar commits.

Signed-off-by: Philipp Schuster <[email protected]>
On-behalf-of: SAP [email protected]
This commit is part of a series of similar commits.

Signed-off-by: Philipp Schuster <[email protected]>
On-behalf-of: SAP [email protected]
This commit is part of a series of similar commits.

Signed-off-by: Philipp Schuster <[email protected]>
On-behalf-of: SAP [email protected]
This commit is part of a series of similar commits.

Signed-off-by: Philipp Schuster <[email protected]>
On-behalf-of: SAP [email protected]
This commit is part of a series of similar commits.

Signed-off-by: Philipp Schuster <[email protected]>
On-behalf-of: SAP [email protected]
This commit is part of a series of similar commits.

Signed-off-by: Philipp Schuster <[email protected]>
On-behalf-of: SAP [email protected]
This commit is part of a series of similar commits.

Signed-off-by: Philipp Schuster <[email protected]>
On-behalf-of: SAP [email protected]
This commit is part of a series of similar commits.

Signed-off-by: Philipp Schuster <[email protected]>
On-behalf-of: SAP [email protected]
This commit is part of a series of similar commits.

Signed-off-by: Philipp Schuster <[email protected]>
On-behalf-of: SAP [email protected]
This commit is part of a series of similar commits.

Signed-off-by: Philipp Schuster <[email protected]>
On-behalf-of: SAP [email protected]
This commit is part of a series of similar commits.

Signed-off-by: Philipp Schuster <[email protected]>
On-behalf-of: SAP [email protected]
This commit is part of a series of similar commits.

Signed-off-by: Philipp Schuster <[email protected]>
On-behalf-of: SAP [email protected]
Copy link
Contributor

@snue snue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I couldn't spot any mistakes in the various transformations. I definitely prefer the clear dependencies. Thanks for taking the time to clean this up.

@rbradford rbradford added this pull request to the merge queue Nov 24, 2025
Merged via the queue into cloud-hypervisor:main with commit 67fc9d9 Nov 24, 2025
42 of 43 checks passed
@phip1611 phip1611 deleted the remove-extern-crate branch November 25, 2025 05:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants