Skip to content

Commit a9b5c8c

Browse files
committed
Auto merge of rust-lang#12790 - c410-f3r:blah, r=dswij
Fix rust-lang#12760 Fix rust-lang#12760 ``` changelog: [missing_panics_doc]: Ignore panics in compile time ```
2 parents a7f3265 + 12ec009 commit a9b5c8c

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

clippy_lints/src/doc/missing_headers.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use super::{DocHeaders, MISSING_ERRORS_DOC, MISSING_PANICS_DOC, MISSING_SAFETY_DOC, UNNECESSARY_SAFETY_DOC};
12
use clippy_utils::diagnostics::{span_lint, span_lint_and_note};
23
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
34
use clippy_utils::{is_doc_hidden, return_ty};
@@ -6,15 +7,13 @@ use rustc_lint::LateContext;
67
use rustc_middle::ty;
78
use rustc_span::{sym, Span};
89

9-
use super::{DocHeaders, MISSING_ERRORS_DOC, MISSING_PANICS_DOC, MISSING_SAFETY_DOC, UNNECESSARY_SAFETY_DOC};
10-
1110
pub fn check(
1211
cx: &LateContext<'_>,
1312
owner_id: OwnerId,
1413
sig: FnSig<'_>,
1514
headers: DocHeaders,
1615
body_id: Option<BodyId>,
17-
panic_span: Option<Span>,
16+
panic_info: Option<(Span, bool)>,
1817
check_private_items: bool,
1918
) {
2019
if !check_private_items && !cx.effective_visibilities.is_exported(owner_id.def_id) {
@@ -48,13 +47,13 @@ pub fn check(
4847
),
4948
_ => (),
5049
}
51-
if !headers.panics && panic_span.is_some() {
50+
if !headers.panics && panic_info.map_or(false, |el| !el.1) {
5251
span_lint_and_note(
5352
cx,
5453
MISSING_PANICS_DOC,
5554
span,
5655
"docs for function which may panic missing `# Panics` section",
57-
panic_span,
56+
panic_info.map(|el| el.0),
5857
"first possible panic found here",
5958
);
6059
}

clippy_lints/src/doc/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
44
use clippy_utils::macros::{is_panic, root_macro_call_first_node};
55
use clippy_utils::ty::is_type_diagnostic_item;
66
use clippy_utils::visitors::Visitable;
7-
use clippy_utils::{is_entrypoint_fn, is_trait_impl_item, method_chain_args};
7+
use clippy_utils::{in_constant, is_entrypoint_fn, is_trait_impl_item, method_chain_args};
88
use pulldown_cmark::Event::{
99
Code, End, FootnoteReference, HardBreak, Html, Rule, SoftBreak, Start, TaskListMarker, Text,
1010
};
@@ -461,14 +461,14 @@ impl<'tcx> LateLintPass<'tcx> for Documentation {
461461
if !(is_entrypoint_fn(cx, item.owner_id.to_def_id()) || in_external_macro(cx.tcx.sess, item.span)) {
462462
let body = cx.tcx.hir().body(body_id);
463463

464-
let panic_span = FindPanicUnwrap::find_span(cx, cx.tcx.typeck(item.owner_id), body.value);
464+
let panic_info = FindPanicUnwrap::find_span(cx, cx.tcx.typeck(item.owner_id), body.value);
465465
missing_headers::check(
466466
cx,
467467
item.owner_id,
468468
sig,
469469
headers,
470470
Some(body_id),
471-
panic_span,
471+
panic_info,
472472
self.check_private_items,
473473
);
474474
}
@@ -806,6 +806,7 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
806806

807807
struct FindPanicUnwrap<'a, 'tcx> {
808808
cx: &'a LateContext<'tcx>,
809+
is_const: bool,
809810
panic_span: Option<Span>,
810811
typeck_results: &'tcx ty::TypeckResults<'tcx>,
811812
}
@@ -815,14 +816,15 @@ impl<'a, 'tcx> FindPanicUnwrap<'a, 'tcx> {
815816
cx: &'a LateContext<'tcx>,
816817
typeck_results: &'tcx ty::TypeckResults<'tcx>,
817818
body: impl Visitable<'tcx>,
818-
) -> Option<Span> {
819+
) -> Option<(Span, bool)> {
819820
let mut vis = Self {
820821
cx,
822+
is_const: false,
821823
panic_span: None,
822824
typeck_results,
823825
};
824826
body.visit(&mut vis);
825-
vis.panic_span
827+
vis.panic_span.map(|el| (el, vis.is_const))
826828
}
827829
}
828830

@@ -841,6 +843,7 @@ impl<'a, 'tcx> Visitor<'tcx> for FindPanicUnwrap<'a, 'tcx> {
841843
"assert" | "assert_eq" | "assert_ne"
842844
)
843845
{
846+
self.is_const = in_constant(self.cx, expr.hir_id);
844847
self.panic_span = Some(macro_call.span);
845848
}
846849
}

tests/ui/missing_panics_doc.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,11 @@ fn from_declared_macro_should_lint_at_macrosite() {
191191
// Not here.
192192
some_macro_that_panics!()
193193
}
194+
195+
pub fn issue_12760<const N: usize>() {
196+
const {
197+
if N == 0 {
198+
panic!();
199+
}
200+
}
201+
}

0 commit comments

Comments
 (0)