Skip to content

checkpatch.pl: introduce warning for unwrap() calls #1191

Open

Description

@ojeda

Just like checkpatch.pl tells developers that they should do their best to avoid BUG* calls (and if they do, they should be ready to justify them), unwrap()s (and similar panicking APis like expect()) in the Rust side are also equally damaging (at least at the moment) since a Rust panic currently calls BUGs too.

There are cases where unwrap() is justified (just like unsafe/unchecked operations), but in many cases developers may reach for unwrap() when there are better alternatives (e.g. reorganizing the code to avoid that case to start with). Even if there is no better alternative and it may be locally obvious that it is an impossible condition, using unwrap() and similar APIs should be a conscious decision.

Thus introduce a checkpatch.pl warning for common panicking methods such as unwrap() and expect() to make developers think twice about it, as well as to make it easier for maintainers to catch any such use when they check patches they receive.

Notes:

Link: https://lore.kernel.org/rust-for-linux/CANiq72nJiJ4K6jy17x-YRYnJpjqTnohYWvoFrLkYQp0X4tLL=w@mail.gmail.com/


This requires submitting a proper patch to the LKML and the Rust for Linux mailing list. Please recall to test your changes (including generating the documentation if changed, running the Rust doctests if changed, etc.), to use a proper title for the commit, to sign your commit under the Developer's Certificate of Origin and to add a Suggested-by: tag, and a Link: tag to this issue. Please see https://docs.kernel.org/process/submitting-patches.html and https://rust-for-linux.com/contributing for details.

Activity

  1. added
    • miscRelated to other topics (e.g. CI).
    easyExpected to be an easy issue to resolve.
    on Sep 27, 2025
  2. added
    mediumExpected to be an issue of medium difficulty to resolve.
    on Nov 10, 2025
  3. tot0rokr commented on Jan 13, 2026

    @tot0rokr

    Hi @ojeda .
    Are you still waiting on a fix for this issue? If so, I’d be happy to take it on and work on a patch.
    Would it be a good approach to address this by updating checkpatch.pl to parse unwrap() calls with a regex and emit a warning? Is there anything else I should look at or reference before I start?

  4. ojeda commented on Jan 13, 2026

    @ojeda
    MemberAuthor

    That would be nice from our side, but please note that there may not be much bandwidth on the checkpatch.pl side -- we have a few old, pending patches in the list for checkpatch.pl (helping to move those forward, for instance reviewing them, would be nice as well).

    Thus, if you decide to work on it, then please keep it as simple as reasonably possible, so that it is as easy to review as possible and thus to land.

    Thanks!

  5. onur-ozkan commented on Feb 3, 2026

    @onur-ozkan
    Member

    Is there a specific reason we are not using clippy for this? It already has unwrap_used and expect_used rules.

  6. ojeda commented on Feb 5, 2026

    @ojeda
    MemberAuthor

    Is there a specific reason we are not using clippy for this? It already has unwrap_used and expect_used rules.

    For reference, I gave some context at https://lore.kernel.org/rust-for-linux/CANiq72=J5LBxfawu3MtQpcukaXcw+pRyMWd9ueDE=od4X99R3w@mail.gmail.com/.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    easyExpected to be an easy issue to resolve.good first issueGood for newcomersmediumExpected to be an issue of medium difficulty to resolve.• miscRelated to other topics (e.g. CI).

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions