blob: d016e099e303b68f722d34e691623d2b5a45f5e1 (
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
//@aux-build:macro_rules.rs
#![warn(clippy::missing_panics_doc)]
#![allow(clippy::option_map_unit_fn, clippy::unnecessary_literal_unwrap)]
#[macro_use]
extern crate macro_rules;
use macro_rules::macro_with_panic;
fn main() {}
/// This needs to be documented
pub fn unwrap() {
//~^ missing_panics_doc
let result = Err("Hi");
result.unwrap()
}
/// This needs to be documented
pub fn panic() {
//~^ missing_panics_doc
panic!("This function panics")
}
/// This needs to be documented
pub fn inner_body(opt: Option<u32>) {
//~^ missing_panics_doc
opt.map(|x| {
if x == 10 {
panic!()
}
});
}
/// This needs to be documented
pub fn unreachable_and_panic() {
//~^ missing_panics_doc
if true { unreachable!() } else { panic!() }
}
/// This needs to be documented
pub fn assert_eq() {
//~^ missing_panics_doc
let x = 0;
assert_eq!(x, 0);
}
/// This needs to be documented
pub fn assert_ne() {
//~^ missing_panics_doc
let x = 0;
assert_ne!(x, 0);
}
/// This is documented
///
/// # Panics
///
/// Panics if `result` if an error
pub fn unwrap_documented() {
let result = Err("Hi");
result.unwrap()
}
/// This is documented
///
/// # Panics
///
/// Panics just because
pub fn panic_documented() {
panic!("This function panics")
}
/// This is documented
///
/// # Panics
///
/// Panics if `opt` is Just(10)
pub fn inner_body_documented(opt: Option<u32>) {
opt.map(|x| {
if x == 10 {
panic!()
}
});
}
/// This is documented
///
/// # Panics
///
/// We still need to do this part
pub fn unreachable_amd_panic_documented() {
if true { unreachable!() } else { panic!() }
}
/// This is documented
///
/// # Panics
///
/// Panics if `x` is not 0.
pub fn assert_eq_documented() {
let x = 0;
assert_eq!(x, 0);
}
/// This is documented
///
/// # Panics
///
/// Panics if `x` is 0.
pub fn assert_ne_documented() {
let x = 0;
assert_ne!(x, 0);
}
/// `todo!()` is fine
pub fn todo() {
todo!()
}
/// This is okay because it is private
fn unwrap_private() {
let result = Err("Hi");
result.unwrap()
}
/// This is okay because it is private
fn panic_private() {
panic!("This function panics")
}
/// This is okay because it is private
fn inner_body_private(opt: Option<u32>) {
opt.map(|x| {
if x == 10 {
panic!()
}
});
}
/// This is okay because unreachable
pub fn unreachable() {
unreachable!("This function panics")
}
/// #6970.
/// This is okay because it is expansion of `debug_assert` family.
pub fn debug_assertions() {
debug_assert!(false);
debug_assert_eq!(1, 2);
debug_assert_ne!(1, 2);
}
pub fn partially_const<const N: usize>(n: usize) {
//~^ missing_panics_doc
const {
assert!(N > 5);
}
assert!(N > n);
}
pub fn expect_allow(i: Option<isize>) {
#[expect(clippy::missing_panics_doc)]
i.unwrap();
#[allow(clippy::missing_panics_doc)]
i.unwrap();
}
pub fn expect_allow_with_error(i: Option<isize>) {
//~^ missing_panics_doc
#[expect(clippy::missing_panics_doc)]
i.unwrap();
#[allow(clippy::missing_panics_doc)]
i.unwrap();
i.unwrap();
}
pub fn expect_after_error(x: Option<u32>, y: Option<u32>) {
//~^ missing_panics_doc
let x = x.unwrap();
#[expect(clippy::missing_panics_doc)]
let y = y.unwrap();
}
// all function must be triggered the lint.
// `pub` is required, because the lint does not consider unreachable items
pub mod issue10240 {
pub fn option_unwrap<T>(v: &[T]) -> &T {
//~^ missing_panics_doc
let o: Option<&T> = v.last();
o.unwrap()
}
pub fn option_expect<T>(v: &[T]) -> &T {
//~^ missing_panics_doc
let o: Option<&T> = v.last();
o.expect("passed an empty thing")
}
pub fn result_unwrap<T>(v: &[T]) -> &T {
//~^ missing_panics_doc
let res: Result<&T, &str> = v.last().ok_or("oh noes");
res.unwrap()
}
pub fn result_expect<T>(v: &[T]) -> &T {
//~^ missing_panics_doc
let res: Result<&T, &str> = v.last().ok_or("oh noes");
res.expect("passed an empty thing")
}
pub fn last_unwrap(v: &[u32]) -> u32 {
//~^ missing_panics_doc
*v.last().unwrap()
}
pub fn last_expect(v: &[u32]) -> u32 {
//~^ missing_panics_doc
*v.last().expect("passed an empty thing")
}
}
fn from_external_macro_should_not_lint() {
macro_with_panic!()
}
macro_rules! some_macro_that_panics {
() => {
panic!()
};
}
fn from_declared_macro_should_lint_at_macrosite() {
// Not here.
some_macro_that_panics!()
}
pub fn issue_12760<const N: usize>() {
const {
if N == 0 {
panic!();
}
}
}
/// This needs documenting
pub fn unwrap_expect_etc_in_const() {
let a = const { std::num::NonZeroUsize::new(1).unwrap() };
// This should still pass the lint even if it is guaranteed to panic at compile-time
let b = const { std::num::NonZeroUsize::new(0).unwrap() };
}
/// This needs documenting
pub const fn unwrap_expect_etc_in_const_fn_fails() {
//~^ missing_panics_doc
let a = std::num::NonZeroUsize::new(1).unwrap();
}
/// This needs documenting
pub const fn assert_in_const_fn_fails() {
//~^ missing_panics_doc
let x = 0;
if x == 0 {
panic!();
}
}
/// This needs documenting
pub const fn in_const_fn<const N: usize>(n: usize) {
//~^ missing_panics_doc
assert!(N > n);
}
|