-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Disagreement with missing_panics_doc changes regarding expect
#11436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I have similar reservations about this change so I figure I'd chime in. Recently saw it show up in CI and was a bit surprised. While my line of thought on this matter may be potentially unidiomatic, I consider usages of In this case, the To illustrate with a contrived function: /// Return a `String` pulled out of the byte stream.
pub fn string_from_byte_stream() -> String {
let bytes = get_valid_utf8();
String::from_utf8(bytes).except("`get_valid_utf8()` always returns valid UTF-8")
}
fn get_valid_utf8() -> Vec<u8> {
/* Gets some bytes that the dev knows is always valid UTF-8 */
} To appease the lint now: /// Return a `String` pulled out of the byte stream.
/// # Panics
/// Panics if the byte stream is not valid UTF-8.
pub fn string_from_byte_stream() -> String {
/* ... */
} This doesn't exactly help the caller prevent or avoid a panic in anyway. Perhaps adding a |
Running into this recently. I use cc the author of that change @KisaragiEffective |
A list of methods/macro names to ignore seems like a good config option to add. There have been several different incompatible conventions mentioned for what |
What should the config be? How should it handle paths, e.g. Initial idea, though one could also consider an allowlist instead: panics_requiring_doc = ["Option::unwrap", "Option::expect", "Result::unwrap", "Result::expect", "panic!", "assert!", "assert_eq!", "assert_ne!"] One issue is that the detection is implemented quite differently: rust-clippy/clippy_utils/src/macros.rs Lines 194 to 207 in 789bc73
rust-clippy/clippy_lints/src/doc.rs Lines 843 to 846 in 789bc73
and rust-clippy/clippy_lints/src/doc.rs Lines 852 to 856 in 789bc73
|
Personally I would go with a list of paths to ignore as that's more common for clippy configuration, you could take inspiration from |
my idea was that one could add their own functions/macros to the lint, but personally I don't have a usecase for that, so just having an ignore list is fine by me |
My deepest wish is that I could flag individual lines/blocks as not requiring panic docs. I haven't looked at the lint code and fully expect that what I want might be difficult to impossible, but in the world of dreams, I'd really like to be able to (reusing some above examples), /// Do something
pub fn string_from_byte_stream() -> String {
let bytes = get_valid_utf8();
#[expect(clippy::missing_panics_doc_ok, reason="caller can't do anything about this")]
String::from_utf8(bytes).expect("`get_valid_utf8()` always returns valid UTF-8")
} Time passes, function modified, /// Do something
pub fn string_from_byte_stream(input: i64) -> String {
let input = u64::try_from(input).expect("non-negative");// Oops, added something that needs "# Panics"
// ... stuff
let bytes = get_valid_utf8();
#[expect(clippy::missing_panics_doc_ok, reason="caller can't do anything about this")]
String::from_utf8(bytes).expect("`get_valid_utf8()` always returns valid UTF-8")
} In my dreams the first fn wouldn't lint, but the second one would since it has an untagged panic. |
you can use the _unchecked variant to eliminate both warning and unnecessary control flow jump, aren't you? |
Aside that, can we rename this as |
I started using a crate (https://docs.rs/intentional/latest/intentional/trait.Assert.html) that provides some helper methods that do the same as expect & unwrap so I can differentiate between cases I want to document and cases I don't. |
Another example not involving pub fn do_something(input: &[i64]) {
if handle_small_vec(input) { return; }
#[expect(clippy::missing_panics_doc_ok, reason="small vecs handled above")]
assert!(input.len() > 3, "small vecs handled above, this assert satisfies clippy::missing_asserts_for_indexing");
do_something_else((input[0] - input[1]) * (input[2] - input[3]));
} |
Opened #14407 for For pub fn do_something(input: &[i64]) {
if handle_small_vec(input) {
return;
}
#[expect(clippy::missing_panics_doc_ok)]
{
assert!(
input.len() > 3,
"small vecs handled above, this assert satisfies clippy::missing_asserts_for_indexing"
);
}
do_something_else((input[0] - input[1]) * (input[2] - input[3]));
} |
…panics_doc` (#14407) Implements #11436 (comment) > [...] I'd really like to be able to (reusing some above examples), > > ``` rust > /// Do something > pub fn string_from_byte_stream() -> String { > let bytes = get_valid_utf8(); > #[expect(clippy::missing_panics_doc_ok, reason="caller can't do anything about this")] > String::from_utf8(bytes).expect("`get_valid_utf8()` always returns valid UTF-8") > } > ``` Also fixes an issue where if the first panic is in a `const` context disables the lint for the rest of the function The first commit is just moving code around changelog: [`missing_panics_doc`]: `#[allow]` and `#[expect]` can now be used within the function body to ignore individual panics
Summary
In #10953,
missing_panics_doc
was updated to include reporting onOption::expect
andResult::expect
. This has caused a large number of warnings across my projects, and I wanted to start a discussion over the changes.To me, these two approaches to asserting that an option shouldn't be
None
are identical from a programmer's intent:Despite the first
match
statement possibly panicking, no warning is given. Yet, with the new behavior,opt.expect()
now warns about possible panics. To me, my intention as a programmer between these two approaches is identical.The net result of this change is that I'm going to be forced into an ugly pattern in my code to avoid this false positive. Could we split the warnings for panicking on
expect
into its own lint so that we can keep the previous behavior?Lint Name
missing_panics_doc
Reproducer
I tried this code:
I saw this happen:
I expected to see this happen: No warnings
Version
Additional Labels
No response
The text was updated successfully, but these errors were encountered: