Skip to content
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

Optional lint for functions that panic without mentioning panics in rustdoc #1974

Closed
joshtriplett opened this issue Aug 19, 2017 · 5 comments · Fixed by #6523
Closed

Optional lint for functions that panic without mentioning panics in rustdoc #1974

joshtriplett opened this issue Aug 19, 2017 · 5 comments · Fixed by #6523
Assignees
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy

Comments

@joshtriplett
Copy link
Member

joshtriplett commented Aug 19, 2017

@mgattozzi's excellent talk at RustConf just now made a really good point: functions that panic should document that panic in their documentation.

While we can't detect this in the completely general case, we could detect explicit calls to panic!(), and RFC 2091 would give the compiler enough information that it could detect calls to functions that (may) panic.

I'd love to see an optional lint for functions that call panic, indirectly or directly, without documentation of that. (Having such documentation would require fairly fuzzy detection, unfortunately, but I still think this is worth considering..)

@killercup
Copy link
Member

Casual drive-by comment:

@joshtriplett
Copy link
Member Author

@killercup First: awesome, thank you!

Can you give an example of the kind of "indirect panics" that you don't think need documentation?

I do, for instance, want documentation that using an invalid array index would panic rather than generating an error.

@killercup
Copy link
Member

@joshtriplett sure. It may be that I have a different assumption on what I want to document than you do.

Consider this function:

fn foo(x: i32) {
    assert!(x > 1, "Can only foo-ify things great than 1");
    let y = vec![1, 2, 3,];
    let z = helper_thing(&y, x);
    // ...
}

What I'm saying is this: The assert explicitly communicates a possible programmer error, where the API expects its user to check they supply a valid input. Requiring documentation for this assert is very valuable (IMHO) and hopefully not that hard. Analyzing that helper_thing cannot (indirectly) cause a panic may be quite hard (if at all possible!) and in contrast to the explicit panic conditions may only result in a "yep, that'll panic" boolean. (Which is totally fine but a different goal.)

@joshtriplett
Copy link
Member Author

@killercup I actually do want that function to provide documentation that it panics if passed an index too large. As opposed to, for instance, returning an error.

That said, there are occasional cases where a call to .unwrap() or similar has a panic that is in practice unreachable, and in those cases, sure, no need to document that. But I think I'd be OK with having to tag those somehow to ignore the apparent issue.

@brightly-salty
Copy link
Contributor

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants