-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2837 from fanzier/panicking_unwrap
Implement lint checking for `unwrap`s that will always panic.
- Loading branch information
Showing
4 changed files
with
308 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,101 @@ | ||
#![deny(unnecessary_unwrap)] | ||
#![deny(panicking_unwrap, unnecessary_unwrap)] | ||
#![allow(if_same_then_else)] | ||
|
||
fn main() { | ||
let x = Some(()); | ||
if x.is_some() { | ||
x.unwrap(); | ||
x.unwrap(); // unnecessary | ||
} else { | ||
x.unwrap(); // will panic | ||
} | ||
if x.is_none() { | ||
// nothing to do here | ||
x.unwrap(); // will panic | ||
} else { | ||
x.unwrap(); | ||
x.unwrap(); // unnecessary | ||
} | ||
let mut x: Result<(), ()> = Ok(()); | ||
if x.is_ok() { | ||
x.unwrap(); | ||
x.unwrap(); // unnecessary | ||
x.unwrap_err(); // will panic | ||
} else { | ||
x.unwrap_err(); | ||
x.unwrap(); // will panic | ||
x.unwrap_err(); // unnecessary | ||
} | ||
if x.is_err() { | ||
x.unwrap_err(); | ||
x.unwrap(); // will panic | ||
x.unwrap_err(); // unnecessary | ||
} else { | ||
x.unwrap(); | ||
x.unwrap(); // unnecessary | ||
x.unwrap_err(); // will panic | ||
} | ||
if x.is_ok() { | ||
x = Err(()); | ||
x.unwrap(); | ||
x.unwrap(); // not unnecessary because of mutation of x | ||
// it will always panic but the lint is not smart enoguh to see this (it only checks if conditions). | ||
} else { | ||
x = Ok(()); | ||
x.unwrap_err(); | ||
x.unwrap_err(); // not unnecessary because of mutation of x | ||
// it will always panic but the lint is not smart enoguh to see this (it only checks if conditions). | ||
} | ||
} | ||
|
||
fn test_complex_conditions() { | ||
let x: Result<(), ()> = Ok(()); | ||
let y: Result<(), ()> = Ok(()); | ||
if x.is_ok() && y.is_err() { | ||
x.unwrap(); | ||
y.unwrap_err(); | ||
x.unwrap(); // unnecessary | ||
x.unwrap_err(); // will panic | ||
y.unwrap(); // will panic | ||
y.unwrap_err(); // unnecessary | ||
} else { | ||
// not clear whether unwrappable: | ||
// not statically determinable whether any of the following will always succeed or always fail: | ||
x.unwrap(); | ||
x.unwrap_err(); | ||
y.unwrap(); | ||
y.unwrap_err(); | ||
} | ||
|
||
if x.is_ok() || y.is_ok() { | ||
// not clear whether unwrappable: | ||
// not statically determinable whether any of the following will always succeed or always fail: | ||
x.unwrap(); | ||
y.unwrap(); | ||
} else { | ||
x.unwrap_err(); | ||
y.unwrap_err(); | ||
x.unwrap(); // will panic | ||
x.unwrap_err(); // unnecessary | ||
y.unwrap(); // will panic | ||
y.unwrap_err(); // unnecessary | ||
} | ||
let z: Result<(), ()> = Ok(()); | ||
if x.is_ok() && !(y.is_ok() || z.is_err()) { | ||
x.unwrap(); | ||
y.unwrap_err(); | ||
z.unwrap(); | ||
x.unwrap(); // unnecessary | ||
x.unwrap_err(); // will panic | ||
y.unwrap(); // will panic | ||
y.unwrap_err(); // unnecessary | ||
z.unwrap(); // unnecessary | ||
z.unwrap_err(); // will panic | ||
} | ||
if x.is_ok() || !(y.is_ok() && z.is_err()) { | ||
// not clear what's unwrappable | ||
} else { | ||
x.unwrap_err(); | ||
// not statically determinable whether any of the following will always succeed or always fail: | ||
x.unwrap(); | ||
y.unwrap(); | ||
z.unwrap_err(); | ||
z.unwrap(); | ||
} else { | ||
x.unwrap(); // will panic | ||
x.unwrap_err(); // unnecessary | ||
y.unwrap(); // unnecessary | ||
y.unwrap_err(); // will panic | ||
z.unwrap(); // will panic | ||
z.unwrap_err(); // unnecessary | ||
} | ||
} | ||
|
||
fn test_nested() { | ||
fn nested() { | ||
let x = Some(()); | ||
if x.is_some() { | ||
x.unwrap(); | ||
x.unwrap(); // unnecessary | ||
} else { | ||
x.unwrap(); // will panic | ||
} | ||
} | ||
} |
Oops, something went wrong.