Skip to content

unused_imports lints on an import from a crate with a lang item #109261

@Niki4tap

Description

@Niki4tap

When importing an item from a crate, which has a language item defined, unused_imports can lint, which makes sense if the item is actually unused, but if you remove this import (run cargo fix for example), the code stops working, because if you don't import anything from the crate, the lang item doesn't get defined.

Seems like this issue can also happen with panic handlers and other project-wide items (don't know the proper term here).

Interestingly enough, it seems like importing just the crate use lib as _ (without ::inner) doesn't lint, which made me feel like there was a similar issue already, but I wasn't able to find it.

I tried this code:

lib.rs:

#![feature(lang_items)] #![no_std] #[lang = "eh_personality"] extern "C" fn eh_personality() {} #[panic_handler] fn panic(_info: &core::panic::PanicInfo) -> ! { loop {} } pub mod inner {}

main.rs:

#![no_std] #![no_main] use lib::inner as _; #[link(name = "c", kind = "dylib")] extern {} #[no_mangle] extern "C" fn main() {}

I expected to see this happen: unused_imports doesn't fire

Instead, this happened:

warning: unused import: `lib::inner as _` --> main.rs:4:5 | 4 | use lib::inner as _; | ^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default 

After running cargo fix:

The following errors were reported: error: `#[panic_handler]` function required, but not found error: language item required, but not found: `eh_personality` | = note: this can occur when a binary crate with `#![no_std]` is compiled for a target where `eh_personality` is defined in the standard library = help: you may be able to compile for a target that doesn't need `eh_personality`, specify a target with `--target` or in `.cargo/config` error: aborting due to 2 previous errors 

Meta

rustc --version --verbose:

rustc 1.68.0-nightly (333ee6c46 2023-01-18) binary: rustc commit-hash: 333ee6c466972185973d5097f8b5fb0f9fb13fa5 commit-date: 2023-01-18 host: x86_64-unknown-linux-gnu release: 1.68.0-nightly LLVM version: 15.0.6 

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.L-unused_importsLint: unused_imports

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions