Skip to content

Commit e1c74cc

Browse files
committed
Abide by allow/expect in bodies for missing_panics_doc
1 parent 7483281 commit e1c74cc

File tree

4 files changed

+62
-15
lines changed

4 files changed

+62
-15
lines changed

clippy_lints/src/doc/missing_headers.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_note};
33
use clippy_utils::macros::{is_panic, root_macro_call_first_node};
44
use clippy_utils::ty::{get_type_diagnostic_name, implements_trait_with_env, is_type_diagnostic_item};
55
use clippy_utils::visitors::Visitable;
6-
use clippy_utils::{is_doc_hidden, method_chain_args, return_ty};
6+
use clippy_utils::{fulfill_or_allowed, is_doc_hidden, method_chain_args, return_ty};
77
use rustc_hir::intravisit::{self, Visitor};
88
use rustc_hir::{AnonConst, BodyId, Expr, FnSig, OwnerId, Safety};
99
use rustc_lint::LateContext;
@@ -129,6 +129,7 @@ impl<'tcx> Visitor<'tcx> for FindPanicUnwrap<'_, 'tcx> {
129129
Some(sym::assert_macro | sym::assert_eq_macro | sym::assert_ne_macro)
130130
))
131131
&& !self.cx.tcx.hir_is_inside_const_context(expr.hir_id)
132+
&& !fulfill_or_allowed(self.cx, MISSING_PANICS_DOC, [expr.hir_id])
132133
{
133134
self.panic_span = Some(macro_call.span);
134135
}
@@ -140,7 +141,8 @@ impl<'tcx> Visitor<'tcx> for FindPanicUnwrap<'_, 'tcx> {
140141
if matches!(
141142
get_type_diagnostic_name(self.cx, receiver_ty),
142143
Some(sym::Option | sym::Result)
143-
) {
144+
) && !fulfill_or_allowed(self.cx, MISSING_PANICS_DOC, [expr.hir_id])
145+
{
144146
self.panic_span = Some(expr.span);
145147
}
146148
}

clippy_lints/src/doc/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,19 @@ declare_clippy_lint! {
187187
/// }
188188
/// }
189189
/// ```
190+
///
191+
/// Individual panics within a function can be ignored with `#[expect]` or
192+
/// `#[allow]`:
193+
///
194+
/// ```no_run
195+
/// # use std::num::NonZeroUsize;
196+
/// pub fn will_not_panic(x: usize) {
197+
/// #[expect(clippy::missing_panics_doc, reason = "infallible")]
198+
/// let y = NonZeroUsize::new(1).unwrap();
199+
///
200+
/// // If any panics are added in the future the lint will still catch them
201+
/// }
202+
/// ```
190203
#[clippy::version = "1.51.0"]
191204
pub MISSING_PANICS_DOC,
192205
pedantic,

tests/ui/missing_panics_doc.rs

+20
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,26 @@ pub fn partially_const<const N: usize>(n: usize) {
161161
assert!(N > n);
162162
}
163163

164+
pub fn expect_allow(i: Option<isize>) {
165+
#[expect(clippy::missing_panics_doc)]
166+
i.unwrap();
167+
168+
#[allow(clippy::missing_panics_doc)]
169+
i.unwrap();
170+
}
171+
172+
pub fn expect_allow_with_error(i: Option<isize>) {
173+
//~^ missing_panics_doc
174+
175+
#[expect(clippy::missing_panics_doc)]
176+
i.unwrap();
177+
178+
#[allow(clippy::missing_panics_doc)]
179+
i.unwrap();
180+
181+
i.unwrap();
182+
}
183+
164184
// all function must be triggered the lint.
165185
// `pub` is required, because the lint does not consider unreachable items
166186
pub mod issue10240 {

tests/ui/missing_panics_doc.stderr

+25-13
Original file line numberDiff line numberDiff line change
@@ -85,76 +85,88 @@ LL | assert!(N > n);
8585
| ^^^^^^^^^^^^^^
8686

8787
error: docs for function which may panic missing `# Panics` section
88-
--> tests/ui/missing_panics_doc.rs:167:5
88+
--> tests/ui/missing_panics_doc.rs:172:1
89+
|
90+
LL | pub fn expect_allow_with_error(i: Option<isize>) {
91+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
92+
|
93+
note: first possible panic found here
94+
--> tests/ui/missing_panics_doc.rs:181:5
95+
|
96+
LL | i.unwrap();
97+
| ^^^^^^^^^^
98+
99+
error: docs for function which may panic missing `# Panics` section
100+
--> tests/ui/missing_panics_doc.rs:187:5
89101
|
90102
LL | pub fn option_unwrap<T>(v: &[T]) -> &T {
91103
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
92104
|
93105
note: first possible panic found here
94-
--> tests/ui/missing_panics_doc.rs:170:9
106+
--> tests/ui/missing_panics_doc.rs:190:9
95107
|
96108
LL | o.unwrap()
97109
| ^^^^^^^^^^
98110

99111
error: docs for function which may panic missing `# Panics` section
100-
--> tests/ui/missing_panics_doc.rs:173:5
112+
--> tests/ui/missing_panics_doc.rs:193:5
101113
|
102114
LL | pub fn option_expect<T>(v: &[T]) -> &T {
103115
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
104116
|
105117
note: first possible panic found here
106-
--> tests/ui/missing_panics_doc.rs:176:9
118+
--> tests/ui/missing_panics_doc.rs:196:9
107119
|
108120
LL | o.expect("passed an empty thing")
109121
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
110122

111123
error: docs for function which may panic missing `# Panics` section
112-
--> tests/ui/missing_panics_doc.rs:179:5
124+
--> tests/ui/missing_panics_doc.rs:199:5
113125
|
114126
LL | pub fn result_unwrap<T>(v: &[T]) -> &T {
115127
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
116128
|
117129
note: first possible panic found here
118-
--> tests/ui/missing_panics_doc.rs:182:9
130+
--> tests/ui/missing_panics_doc.rs:202:9
119131
|
120132
LL | res.unwrap()
121133
| ^^^^^^^^^^^^
122134

123135
error: docs for function which may panic missing `# Panics` section
124-
--> tests/ui/missing_panics_doc.rs:185:5
136+
--> tests/ui/missing_panics_doc.rs:205:5
125137
|
126138
LL | pub fn result_expect<T>(v: &[T]) -> &T {
127139
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
128140
|
129141
note: first possible panic found here
130-
--> tests/ui/missing_panics_doc.rs:188:9
142+
--> tests/ui/missing_panics_doc.rs:208:9
131143
|
132144
LL | res.expect("passed an empty thing")
133145
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
134146

135147
error: docs for function which may panic missing `# Panics` section
136-
--> tests/ui/missing_panics_doc.rs:191:5
148+
--> tests/ui/missing_panics_doc.rs:211:5
137149
|
138150
LL | pub fn last_unwrap(v: &[u32]) -> u32 {
139151
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
140152
|
141153
note: first possible panic found here
142-
--> tests/ui/missing_panics_doc.rs:193:10
154+
--> tests/ui/missing_panics_doc.rs:213:10
143155
|
144156
LL | *v.last().unwrap()
145157
| ^^^^^^^^^^^^^^^^^
146158

147159
error: docs for function which may panic missing `# Panics` section
148-
--> tests/ui/missing_panics_doc.rs:196:5
160+
--> tests/ui/missing_panics_doc.rs:216:5
149161
|
150162
LL | pub fn last_expect(v: &[u32]) -> u32 {
151163
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
152164
|
153165
note: first possible panic found here
154-
--> tests/ui/missing_panics_doc.rs:198:10
166+
--> tests/ui/missing_panics_doc.rs:218:10
155167
|
156168
LL | *v.last().expect("passed an empty thing")
157169
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
158170

159-
error: aborting due to 13 previous errors
171+
error: aborting due to 14 previous errors
160172

0 commit comments

Comments
 (0)