Skip to content

Lint: enable empty_loop for no_std crates #6161

Closed
@josephlr

Description

@josephlr

Right now, we have a style lint against the following code:

loop {}

Note that this is no longer undefined behavior (since rust-lang/rust#77972).

Currently, there is no way to have a no_std binary or library use the empty_loop lint.
Even adding #![deny(clippy::empty_loop)] to a crate does nothing. This is because #5086 (fixing #3746), made the lint essentially not exist for no_std crates.

Furthermore, even in no_std crates, using a "hot" deadloop is almost always not what you want (as it burns a bunch of CPU, and can cause crashes). Even on a no_std target, a user should either:

  • Panic
  • Call a platform-specific halt or pause intrinsic.

Note, core::sync::atomic::spin_loop_hint is not a good recommendation here, as pause isn't meant for dead-loops (it's designed for spin-locks). See rust-lang/rust#77924

In my opinion, we should:

  • Reenable the empty_loop lint for no_std crates.
  • Have a no_std specific help message.
  • Disable this lint automatically in #[panic_handler]s
    • As an alternative, we could just disable this lint for no_std binaries (while keeping it on for no_std libraries).
    • The only uses of deadloops in the Rustonomicon are inside of #[panic_handler].

Our documentation for fixing this (on std and no_std) is part of #6162

CC (people involved with this change last time): @therealprof, @oli-obk, @eddyp, @Areredify, @phansch

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingC-enhancementCategory: Enhancement of lints, like adding more cases or adding help messagesS-needs-discussionStatus: Needs further discussion before merging or work can be started

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions