rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

`// PANIC` comment on panic-able calls

Open blyxyas opened this issue 1 year ago • 4 comments

What it does

Reports on functions marked as panic-able (for example, .expect()) in the config file or with a special #[clippy::may_panic].

Advantage

  • Ensures that specially critical code is handled with the requirement that it won't panic
  • Allows for more flexibility than unwrap_used or disallowed_methods

Drawbacks

No response

Example

#[clippy::may_panic]
fn dangerous(v: Vec<usize>) -> usize {
    v[1]
}

fn foo()  {
    let v = vec![];
    let _ = dangerous(v);
}

Comparison with existing lints

Unlike unwrap_used, this allows for any function marked such as that. Compared disallowed_methods, this allows for any function call and gives a specific reason. Unlike indexing_slicing, this works for any function marked as clippy::may_panic Unlike unreachable/unimplemented, this works for any functions marked with the attribute.

Additional Context

This was requested by the Rust-for-Linux project, giving it priority. I will mentor anyone trying to implement it.

blyxyas avatar Oct 10 '25 22:10 blyxyas

@rustbot claim

I'd love to work on this, and would certainly appreciate some mentorship :)

hcbarker avatar Oct 11 '25 04:10 hcbarker

Hi @hcbarker! Thanks for taking the time to contribute to Clippy. I'll guide you through this process (and obviously respond any question that you might have)

First of all, some useful resources:


I suggest that you start by reading the Clippy book, it teaches you how to add a lint. For examples on how current lints are written, I'd say that this specific lint could be similar to how undocumented_unsafe_blocks works.

Note that, in the end, a Clippy lint is just a struct that implements a special trait that goes token-by-token, expr-by-expr, item-by-item, calling a set of functions to check that item and emit a warn if necessary.

blyxyas avatar Oct 13 '25 20:10 blyxyas

Once you have a pull request, write "r? blyxyas" somewhere on the body of your pull request. Also note that I might be slow to respond on weekends, I'm under direct threat from my partner that if I review anything on weekends, she'll assassinate me hit-man style.

blyxyas avatar Oct 13 '25 21:10 blyxyas

@blyxyas Thanks a bunch! I'll reach out on Zulip as I have questions :)

hcbarker avatar Oct 14 '25 18:10 hcbarker