blob: b429f3a8a0bb9968e50342435a9e2132cadb759f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
#![warn(clippy::unwrap_used, clippy::expect_used)]
#![allow(clippy::unnecessary_literal_unwrap)]
#![feature(never_type)]
use std::convert::Infallible;
trait OptionExt {
type Item;
fn unwrap_err(self) -> Self::Item;
fn expect_err(self, msg: &str) -> Self::Item;
}
impl<T> OptionExt for Option<T> {
type Item = T;
fn unwrap_err(self) -> T {
panic!();
}
fn expect_err(self, msg: &str) -> T {
panic!();
}
}
fn main() {
Some(3).unwrap();
//~^ unwrap_used
Some(3).expect("Hello world!");
//~^ expect_used
// Don't trigger on unwrap_err on an option
Some(3).unwrap_err();
Some(3).expect_err("Hello none!");
// Issue #11245: The `Err` variant can never be constructed so do not lint this.
let x: Result<(), !> = Ok(());
x.unwrap();
x.expect("is `!` (never)");
let x: Result<(), Infallible> = Ok(());
x.unwrap();
x.expect("is never-like (0 variants)");
let a: Result<i32, i32> = Ok(3);
a.unwrap();
//~^ unwrap_used
a.expect("Hello world!");
//~^ expect_used
a.unwrap_err();
//~^ unwrap_used
a.expect_err("Hello error!");
//~^ expect_used
// Don't trigger in compile time contexts by default
const SOME: Option<i32> = Some(3);
const UNWRAPPED: i32 = SOME.unwrap();
const EXPECTED: i32 = SOME.expect("Not three?");
const {
SOME.unwrap();
}
const {
SOME.expect("Still not three?");
}
}
mod with_expansion {
macro_rules! open {
($file:expr) => {
std::fs::File::open($file)
};
}
fn test(file: &str) {
use std::io::Read;
let mut s = String::new();
let _ = open!(file).unwrap(); //~ unwrap_used
let _ = open!(file).expect("can open"); //~ expect_used
let _ = open!(file).unwrap_err(); //~ unwrap_used
let _ = open!(file).expect_err("can open"); //~ expect_used
}
}
|