Skip to content

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

@ojeda

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.

We may introduce further linting at the source code level for some of those in the future, e.g. #[expect(...), possibly with reason = ... or require // PANIC: comments.

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    easyExpected to be an easy issue to resolve.good first issueGood for newcomers• 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