Skip to content

Commit 1f76a23

Browse files
missing_panics_doc: Allow unwrap() and expect() inside const-only contexts (#15170)
changelog: [`missing_panics_doc`]: Allow unwrap() and expect()s in const-only contexts Fixes #15169. As far as I can tell, this change keeps this lint in line with similar issues and their fixes such as #13382 and #10240, so I feel pretty confident that the behavior it exhibits after this PR is correct.
2 parents 428208e + ff3c9c1 commit 1f76a23

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

clippy_lints/src/doc/missing_headers.rs

Lines changed: 5 additions & 2 deletions
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::for_each_expr;
6-
use clippy_utils::{fulfill_or_allowed, is_doc_hidden, method_chain_args, return_ty};
6+
use clippy_utils::{fulfill_or_allowed, is_doc_hidden, is_inside_always_const_context, method_chain_args, return_ty};
77
use rustc_hir::{BodyId, FnSig, OwnerId, Safety};
88
use rustc_lint::LateContext;
99
use rustc_middle::ty;
@@ -99,13 +99,16 @@ fn find_panic(cx: &LateContext<'_>, body_id: BodyId) -> Option<Span> {
9999
let mut panic_span = None;
100100
let typeck = cx.tcx.typeck_body(body_id);
101101
for_each_expr(cx, cx.tcx.hir_body(body_id), |expr| {
102+
if is_inside_always_const_context(cx.tcx, expr.hir_id) {
103+
return ControlFlow::<!>::Continue(());
104+
}
105+
102106
if let Some(macro_call) = root_macro_call_first_node(cx, expr)
103107
&& (is_panic(cx, macro_call.def_id)
104108
|| matches!(
105109
cx.tcx.get_diagnostic_name(macro_call.def_id),
106110
Some(sym::assert_macro | sym::assert_eq_macro | sym::assert_ne_macro)
107111
))
108-
&& !cx.tcx.hir_is_inside_const_context(expr.hir_id)
109112
&& !fulfill_or_allowed(cx, MISSING_PANICS_DOC, [expr.hir_id])
110113
&& panic_span.is_none()
111114
{

tests/ui/missing_panics_doc.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,31 @@ pub fn issue_12760<const N: usize>() {
250250
}
251251
}
252252
}
253+
254+
/// This needs documenting
255+
pub fn unwrap_expect_etc_in_const() {
256+
let a = const { std::num::NonZeroUsize::new(1).unwrap() };
257+
// This should still pass the lint even if it is guaranteed to panic at compile-time
258+
let b = const { std::num::NonZeroUsize::new(0).unwrap() };
259+
}
260+
261+
/// This needs documenting
262+
pub const fn unwrap_expect_etc_in_const_fn_fails() {
263+
//~^ missing_panics_doc
264+
let a = std::num::NonZeroUsize::new(1).unwrap();
265+
}
266+
267+
/// This needs documenting
268+
pub const fn assert_in_const_fn_fails() {
269+
//~^ missing_panics_doc
270+
let x = 0;
271+
if x == 0 {
272+
panic!();
273+
}
274+
}
275+
276+
/// This needs documenting
277+
pub const fn in_const_fn<const N: usize>(n: usize) {
278+
//~^ missing_panics_doc
279+
assert!(N > n);
280+
}

tests/ui/missing_panics_doc.stderr

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,5 +180,41 @@ note: first possible panic found here
180180
LL | *v.last().expect("passed an empty thing")
181181
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
182182

183-
error: aborting due to 15 previous errors
183+
error: docs for function which may panic missing `# Panics` section
184+
--> tests/ui/missing_panics_doc.rs:262:1
185+
|
186+
LL | pub const fn unwrap_expect_etc_in_const_fn_fails() {
187+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
188+
|
189+
note: first possible panic found here
190+
--> tests/ui/missing_panics_doc.rs:264:13
191+
|
192+
LL | let a = std::num::NonZeroUsize::new(1).unwrap();
193+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
194+
195+
error: docs for function which may panic missing `# Panics` section
196+
--> tests/ui/missing_panics_doc.rs:268:1
197+
|
198+
LL | pub const fn assert_in_const_fn_fails() {
199+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
200+
|
201+
note: first possible panic found here
202+
--> tests/ui/missing_panics_doc.rs:272:9
203+
|
204+
LL | panic!();
205+
| ^^^^^^^^
206+
207+
error: docs for function which may panic missing `# Panics` section
208+
--> tests/ui/missing_panics_doc.rs:277:1
209+
|
210+
LL | pub const fn in_const_fn<const N: usize>(n: usize) {
211+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
212+
|
213+
note: first possible panic found here
214+
--> tests/ui/missing_panics_doc.rs:279:5
215+
|
216+
LL | assert!(N > n);
217+
| ^^^^^^^^^^^^^^
218+
219+
error: aborting due to 18 previous errors
184220

0 commit comments

Comments
 (0)