Skip to content

Commit 7483281

Browse files
committed
Fix partially const bodies not linting missing_panics_doc
1 parent 6923006 commit 7483281

File tree

3 files changed

+48
-27
lines changed

3 files changed

+48
-27
lines changed

clippy_lints/src/doc/missing_headers.rs

+13-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::{DocHeaders, MISSING_ERRORS_DOC, MISSING_PANICS_DOC, MISSING_SAFETY_DOC, UNNECESSARY_SAFETY_DOC};
22
use clippy_utils::diagnostics::{span_lint, span_lint_and_note};
33
use clippy_utils::macros::{is_panic, root_macro_call_first_node};
4-
use clippy_utils::ty::{implements_trait_with_env, is_type_diagnostic_item};
4+
use clippy_utils::ty::{get_type_diagnostic_name, implements_trait_with_env, is_type_diagnostic_item};
55
use clippy_utils::visitors::Visitable;
66
use clippy_utils::{is_doc_hidden, method_chain_args, return_ty};
77
use rustc_hir::intravisit::{self, Visitor};
@@ -51,7 +51,7 @@ pub fn check(
5151
}
5252
if !headers.panics
5353
&& let Some(body_id) = body_id
54-
&& let Some((panic_span, false)) = FindPanicUnwrap::find_span(cx, body_id)
54+
&& let Some(panic_span) = FindPanicUnwrap::find_span(cx, body_id)
5555
{
5656
span_lint_and_note(
5757
cx,
@@ -98,21 +98,19 @@ pub fn check(
9898

9999
struct FindPanicUnwrap<'a, 'tcx> {
100100
cx: &'a LateContext<'tcx>,
101-
is_const: bool,
102101
panic_span: Option<Span>,
103102
typeck_results: &'tcx ty::TypeckResults<'tcx>,
104103
}
105104

106105
impl<'a, 'tcx> FindPanicUnwrap<'a, 'tcx> {
107-
pub fn find_span(cx: &'a LateContext<'tcx>, body_id: BodyId) -> Option<(Span, bool)> {
106+
pub fn find_span(cx: &'a LateContext<'tcx>, body_id: BodyId) -> Option<Span> {
108107
let mut vis = Self {
109108
cx,
110-
is_const: false,
111109
panic_span: None,
112110
typeck_results: cx.tcx.typeck_body(body_id),
113111
};
114112
cx.tcx.hir_body(body_id).visit(&mut vis);
115-
vis.panic_span.map(|el| (el, vis.is_const))
113+
vis.panic_span
116114
}
117115
}
118116

@@ -125,23 +123,24 @@ impl<'tcx> Visitor<'tcx> for FindPanicUnwrap<'_, 'tcx> {
125123
}
126124

127125
if let Some(macro_call) = root_macro_call_first_node(self.cx, expr) {
128-
if is_panic(self.cx, macro_call.def_id)
126+
if (is_panic(self.cx, macro_call.def_id)
129127
|| matches!(
130-
self.cx.tcx.item_name(macro_call.def_id).as_str(),
131-
"assert" | "assert_eq" | "assert_ne"
132-
)
128+
self.cx.tcx.get_diagnostic_name(macro_call.def_id),
129+
Some(sym::assert_macro | sym::assert_eq_macro | sym::assert_ne_macro)
130+
))
131+
&& !self.cx.tcx.hir_is_inside_const_context(expr.hir_id)
133132
{
134-
self.is_const = self.cx.tcx.hir_is_inside_const_context(expr.hir_id);
135133
self.panic_span = Some(macro_call.span);
136134
}
137135
}
138136

139137
// check for `unwrap` and `expect` for both `Option` and `Result`
140138
if let Some(arglists) = method_chain_args(expr, &["unwrap"]).or(method_chain_args(expr, &["expect"])) {
141139
let receiver_ty = self.typeck_results.expr_ty(arglists[0].0).peel_refs();
142-
if is_type_diagnostic_item(self.cx, receiver_ty, sym::Option)
143-
|| is_type_diagnostic_item(self.cx, receiver_ty, sym::Result)
144-
{
140+
if matches!(
141+
get_type_diagnostic_name(self.cx, receiver_ty),
142+
Some(sym::Option | sym::Result)
143+
) {
145144
self.panic_span = Some(expr.span);
146145
}
147146
}

tests/ui/missing_panics_doc.rs

+10
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@ pub fn debug_assertions() {
151151
debug_assert_ne!(1, 2);
152152
}
153153

154+
pub fn partially_const<const N: usize>(n: usize) {
155+
//~^ missing_panics_doc
156+
157+
const {
158+
assert!(N > 5);
159+
}
160+
161+
assert!(N > n);
162+
}
163+
154164
// all function must be triggered the lint.
155165
// `pub` is required, because the lint does not consider unreachable items
156166
pub mod issue10240 {

tests/ui/missing_panics_doc.stderr

+25-13
Original file line numberDiff line numberDiff line change
@@ -73,76 +73,88 @@ LL | assert_ne!(x, 0);
7373
| ^^^^^^^^^^^^^^^^
7474

7575
error: docs for function which may panic missing `# Panics` section
76-
--> tests/ui/missing_panics_doc.rs:157:5
76+
--> tests/ui/missing_panics_doc.rs:154:1
77+
|
78+
LL | pub fn partially_const<const N: usize>(n: usize) {
79+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
80+
|
81+
note: first possible panic found here
82+
--> tests/ui/missing_panics_doc.rs:161:5
83+
|
84+
LL | assert!(N > n);
85+
| ^^^^^^^^^^^^^^
86+
87+
error: docs for function which may panic missing `# Panics` section
88+
--> tests/ui/missing_panics_doc.rs:167:5
7789
|
7890
LL | pub fn option_unwrap<T>(v: &[T]) -> &T {
7991
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8092
|
8193
note: first possible panic found here
82-
--> tests/ui/missing_panics_doc.rs:160:9
94+
--> tests/ui/missing_panics_doc.rs:170:9
8395
|
8496
LL | o.unwrap()
8597
| ^^^^^^^^^^
8698

8799
error: docs for function which may panic missing `# Panics` section
88-
--> tests/ui/missing_panics_doc.rs:163:5
100+
--> tests/ui/missing_panics_doc.rs:173:5
89101
|
90102
LL | pub fn option_expect<T>(v: &[T]) -> &T {
91103
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
92104
|
93105
note: first possible panic found here
94-
--> tests/ui/missing_panics_doc.rs:166:9
106+
--> tests/ui/missing_panics_doc.rs:176:9
95107
|
96108
LL | o.expect("passed an empty thing")
97109
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
98110

99111
error: docs for function which may panic missing `# Panics` section
100-
--> tests/ui/missing_panics_doc.rs:169:5
112+
--> tests/ui/missing_panics_doc.rs:179:5
101113
|
102114
LL | pub fn result_unwrap<T>(v: &[T]) -> &T {
103115
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
104116
|
105117
note: first possible panic found here
106-
--> tests/ui/missing_panics_doc.rs:172:9
118+
--> tests/ui/missing_panics_doc.rs:182:9
107119
|
108120
LL | res.unwrap()
109121
| ^^^^^^^^^^^^
110122

111123
error: docs for function which may panic missing `# Panics` section
112-
--> tests/ui/missing_panics_doc.rs:175:5
124+
--> tests/ui/missing_panics_doc.rs:185:5
113125
|
114126
LL | pub fn result_expect<T>(v: &[T]) -> &T {
115127
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
116128
|
117129
note: first possible panic found here
118-
--> tests/ui/missing_panics_doc.rs:178:9
130+
--> tests/ui/missing_panics_doc.rs:188:9
119131
|
120132
LL | res.expect("passed an empty thing")
121133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
122134

123135
error: docs for function which may panic missing `# Panics` section
124-
--> tests/ui/missing_panics_doc.rs:181:5
136+
--> tests/ui/missing_panics_doc.rs:191:5
125137
|
126138
LL | pub fn last_unwrap(v: &[u32]) -> u32 {
127139
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
128140
|
129141
note: first possible panic found here
130-
--> tests/ui/missing_panics_doc.rs:183:10
142+
--> tests/ui/missing_panics_doc.rs:193:10
131143
|
132144
LL | *v.last().unwrap()
133145
| ^^^^^^^^^^^^^^^^^
134146

135147
error: docs for function which may panic missing `# Panics` section
136-
--> tests/ui/missing_panics_doc.rs:186:5
148+
--> tests/ui/missing_panics_doc.rs:196:5
137149
|
138150
LL | pub fn last_expect(v: &[u32]) -> u32 {
139151
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
140152
|
141153
note: first possible panic found here
142-
--> tests/ui/missing_panics_doc.rs:188:10
154+
--> tests/ui/missing_panics_doc.rs:198:10
143155
|
144156
LL | *v.last().expect("passed an empty thing")
145157
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
146158

147-
error: aborting due to 12 previous errors
159+
error: aborting due to 13 previous errors
148160

0 commit comments

Comments
 (0)