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

Lint Idea: Call may cause panic #2942

Closed
jamesmunns opened this issue Jul 20, 2018 · 3 comments
Closed

Lint Idea: Call may cause panic #2942

jamesmunns opened this issue Jul 20, 2018 · 3 comments

Comments

@jamesmunns
Copy link
Member

It would be nice to have a lint that would inform me if a call I make could panic, and ideally if possible, recommend an alternative that uses Option or Result to handle the error directly. For example, with the following input:

fn main() {
    use std::time::Duration;
    
    let x = Duration::from_secs(2);
    let y = Duration::from_secs(1);
    
    println!("{:?}", y - x);
}

I would like a warning that looks something like this:

src/main.rs
   |
 7 | println!("{:?}", y - x);
   |                    ^ warning: Sub<Duration> for Duration may panic due to this:

src/core/time.rs:428
     | fn sub(self, rhs: Duration) -> Duration {
 428 |     self.checked_sub(rhs).expect("overflow when subtracting durations")
     | }
     |                          ^^^^^^^ call to `expect` could fail

Consider using `Duration::checked_sub()` instead

I imagine this could fall into "Halting problem" territory, but I imagine the process would go something like:

  • find every operator or function call for a given program (you know, no big deal)
  • obtain the full possible branching graph for that call/operator (also nbd)
  • look for unwrap/expect/panic
  • Check if any steps in the call graph to get to the panic have some kind of recommended alternative. Maybe possible to cover the std lib, though supporting external libraries/versions would be difficult to say the least. If no alternative, at least warn

I'm very open to hear things like "this is possible, but difficult", "this is impossible, here is where we discussed previously", etc.

@Manishearth
Copy link
Member

We basically don't do global analyses in clippy, aside from being hard (generics are tricky) and expensive, they're impossible with the current rustc lint design because we run on individual crates and don't have this kind of metadata from dependencies.

I believe https://github.com/llogiq/metacollect has the goal of doing stuff like this, though the project seems abandoned.

@jamesmunns
Copy link
Member Author

Thanks for the feedback @Manishearth, I submitted it since I didn't see any previous issues that exactly discussed this, though #959 and #2536 are somewhat related. I somewhat expected it to get rejected :)

Also I'll check out metacollect if I get some idle cycles.

@flip1995
Copy link
Member

You may also be interested in https://github.com/Technolution/rustig. This tool checks if your Rust program has any paths leading to the panic handler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants