Skip to content

Commit a0fdca5

Browse files
committed
Auto merge of #4993 - rust-lang:doc-unsafe-macro-check, r=flip1995
External macro check for missing_*_doc This fixes #4949 changelog: none
2 parents d9d2013 + 539cd25 commit a0fdca5

File tree

4 files changed

+58
-9
lines changed

4 files changed

+58
-9
lines changed

clippy_lints/src/doc.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::utils::{match_type, paths, return_ty, span_lint};
22
use itertools::Itertools;
33
use rustc::hir;
44
use rustc::impl_lint_pass;
5-
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5+
use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintPass};
66
use rustc_data_structures::fx::FxHashSet;
77
use rustc_session::declare_tool_lint;
88
use rustc_span::source_map::{BytePos, MultiSpan, Span};
@@ -153,7 +153,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DocMarkdown {
153153
let headers = check_attrs(cx, &self.valid_idents, &item.attrs);
154154
match item.kind {
155155
hir::ItemKind::Fn(ref sig, ..) => {
156-
lint_for_missing_headers(cx, item.hir_id, item.span, sig, headers);
156+
if !in_external_macro(cx.tcx.sess, item.span) {
157+
lint_for_missing_headers(cx, item.hir_id, item.span, sig, headers);
158+
}
157159
},
158160
hir::ItemKind::Impl(_, _, _, _, ref trait_ref, ..) => {
159161
self.in_trait_impl = trait_ref.is_some();
@@ -171,13 +173,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DocMarkdown {
171173
fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::TraitItem<'_>) {
172174
let headers = check_attrs(cx, &self.valid_idents, &item.attrs);
173175
if let hir::TraitItemKind::Method(ref sig, ..) = item.kind {
174-
lint_for_missing_headers(cx, item.hir_id, item.span, sig, headers);
176+
if !in_external_macro(cx.tcx.sess, item.span) {
177+
lint_for_missing_headers(cx, item.hir_id, item.span, sig, headers);
178+
}
175179
}
176180
}
177181

178182
fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::ImplItem<'_>) {
179183
let headers = check_attrs(cx, &self.valid_idents, &item.attrs);
180-
if self.in_trait_impl {
184+
if self.in_trait_impl || in_external_macro(cx.tcx.sess, item.span) {
181185
return;
182186
}
183187
if let hir::ImplItemKind::Method(ref sig, ..) = item.kind {
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#[macro_export]
2+
macro_rules! undocd_unsafe {
3+
() => {
4+
pub unsafe fn oy_vey() {
5+
unimplemented!();
6+
}
7+
};
8+
}

tests/ui/doc_unsafe.rs

+26
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// aux-build:doc_unsafe_macros.rs
2+
3+
#[macro_use]
4+
extern crate doc_unsafe_macros;
5+
16
/// This is not sufficiently documented
27
pub unsafe fn destroy_the_planet() {
38
unimplemented!();
@@ -63,6 +68,26 @@ impl Struct {
6368
}
6469
}
6570

71+
macro_rules! very_unsafe {
72+
() => {
73+
pub unsafe fn whee() {
74+
unimplemented!()
75+
}
76+
77+
/// # Safety
78+
///
79+
/// Please keep the seat belt fastened
80+
pub unsafe fn drive() {
81+
whee()
82+
}
83+
};
84+
}
85+
86+
very_unsafe!();
87+
88+
// we don't lint code from external macros
89+
undocd_unsafe!();
90+
6691
#[allow(clippy::let_unit_value)]
6792
fn main() {
6893
unsafe {
@@ -71,5 +96,6 @@ fn main() {
7196
let mut universe = ();
7297
apocalypse(&mut universe);
7398
private_mod::only_crate_wide_accessible();
99+
drive();
74100
}
75101
}

tests/ui/doc_unsafe.stderr

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: unsafe function's docs miss `# Safety` section
2-
--> $DIR/doc_unsafe.rs:2:1
2+
--> $DIR/doc_unsafe.rs:7:1
33
|
44
LL | / pub unsafe fn destroy_the_planet() {
55
LL | | unimplemented!();
@@ -9,26 +9,37 @@ LL | | }
99
= note: `-D clippy::missing-safety-doc` implied by `-D warnings`
1010

1111
error: unsafe function's docs miss `# Safety` section
12-
--> $DIR/doc_unsafe.rs:25:5
12+
--> $DIR/doc_unsafe.rs:30:5
1313
|
1414
LL | / pub unsafe fn republished() {
1515
LL | | unimplemented!();
1616
LL | | }
1717
| |_____^
1818

1919
error: unsafe function's docs miss `# Safety` section
20-
--> $DIR/doc_unsafe.rs:33:5
20+
--> $DIR/doc_unsafe.rs:38:5
2121
|
2222
LL | unsafe fn woefully_underdocumented(self);
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2424

2525
error: unsafe function's docs miss `# Safety` section
26-
--> $DIR/doc_unsafe.rs:52:5
26+
--> $DIR/doc_unsafe.rs:57:5
2727
|
2828
LL | / pub unsafe fn more_undocumented_unsafe() -> Self {
2929
LL | | unimplemented!();
3030
LL | | }
3131
| |_____^
3232

33-
error: aborting due to 4 previous errors
33+
error: unsafe function's docs miss `# Safety` section
34+
--> $DIR/doc_unsafe.rs:73:9
35+
|
36+
LL | / pub unsafe fn whee() {
37+
LL | | unimplemented!()
38+
LL | | }
39+
| |_________^
40+
...
41+
LL | very_unsafe!();
42+
| --------------- in this macro invocation
43+
44+
error: aborting due to 5 previous errors
3445

0 commit comments

Comments
 (0)