Recently I made a typo in my code, and I have been a bit disturbed by the behaviour of the compiler, which was either a bit too clever, or a bit too quiet about the code I wrote.
Here is an example of the problem:
// The following function compiles, without even a warning:
fn my_func() -> Result<String, String> {
Err(()).unwrap()
}
// As expected, the following function does not compile:
fn my_func2(n: i32) -> Result<String, String> {
let e = match n {
0 => Ok(()),
_ => Err(()),
};
e.unwrap() // error[E0308]: mismatched types
}
fn main() {
println!("{:?}", my_func());
println!("{:?}", my_func2(0));
}
As far as I understand, in the function my_func()
the compiler is smart enough to detect that Err(()).unwrap()
will always return !
by panicking, so the code, indeed, is valid.
But maybe at least a warning is missing in this case? I mean: who would write Err(()).unwrap()
with the aim of raising a panick? For my part I discovered my typo a long time after having written it, whereas the compiler could have warned me about a potential problem.