Skip to content

Commit fb17671

Browse files
committed
Auto merge of #7734 - Manishearth:doc-unsafe-trait, r=camsteffen
Make `doc_unsafe` warn on unsafe traits as well Fixes #7732 changelog: Make [`doc_unsafe`] warn on unsafe traits as well
2 parents 9fc4b92 + 53c534d commit fb17671

File tree

5 files changed

+43
-7
lines changed

5 files changed

+43
-7
lines changed

clippy_lints/src/doc.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,17 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
236236
hir::ItemKind::Impl(ref impl_) => {
237237
self.in_trait_impl = impl_.of_trait.is_some();
238238
},
239-
_ => {},
239+
hir::ItemKind::Trait(_, unsafety, ..) => {
240+
if !headers.safety && unsafety == hir::Unsafety::Unsafe {
241+
span_lint(
242+
cx,
243+
MISSING_SAFETY_DOC,
244+
item.span,
245+
"docs for unsafe trait missing `# Safety` section",
246+
);
247+
}
248+
},
249+
_ => (),
240250
}
241251
}
242252

tests/ui/def_id_nocore.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#![feature(no_core, lang_items, start)]
55
#![no_core]
6+
#![allow(clippy::missing_safety_doc)]
67

78
#[link(name = "c")]
89
extern "C" {}

tests/ui/def_id_nocore.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: methods called `as_*` usually take `self` by reference or `self` by mutable reference
2-
--> $DIR/def_id_nocore.rs:26:19
2+
--> $DIR/def_id_nocore.rs:27:19
33
|
44
LL | pub fn as_ref(self) -> &'static str {
55
| ^^^^

tests/ui/doc_unsafe.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,25 @@ mod private_mod {
3434

3535
pub use private_mod::republished;
3636

37-
pub trait UnsafeTrait {
37+
pub trait SafeTraitUnsafeMethods {
3838
unsafe fn woefully_underdocumented(self);
3939

4040
/// # Safety
4141
unsafe fn at_least_somewhat_documented(self);
4242
}
4343

44+
pub unsafe trait UnsafeTrait {
45+
fn method();
46+
}
47+
48+
/// # Safety
49+
pub unsafe trait DocumentedUnsafeTrait {
50+
fn method2();
51+
}
52+
4453
pub struct Struct;
4554

46-
impl UnsafeTrait for Struct {
55+
impl SafeTraitUnsafeMethods for Struct {
4756
unsafe fn woefully_underdocumented(self) {
4857
// all is well
4958
}
@@ -53,6 +62,14 @@ impl UnsafeTrait for Struct {
5362
}
5463
}
5564

65+
unsafe impl UnsafeTrait for Struct {
66+
fn method() {}
67+
}
68+
69+
unsafe impl DocumentedUnsafeTrait for Struct {
70+
fn method2() {}
71+
}
72+
5673
impl Struct {
5774
pub unsafe fn more_undocumented_unsafe() -> Self {
5875
unimplemented!();

tests/ui/doc_unsafe.stderr

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,24 @@ error: unsafe function's docs miss `# Safety` section
2222
LL | unsafe fn woefully_underdocumented(self);
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2424

25+
error: docs for unsafe trait missing `# Safety` section
26+
--> $DIR/doc_unsafe.rs:44:1
27+
|
28+
LL | / pub unsafe trait UnsafeTrait {
29+
LL | | fn method();
30+
LL | | }
31+
| |_^
32+
2533
error: unsafe function's docs miss `# Safety` section
26-
--> $DIR/doc_unsafe.rs:57:5
34+
--> $DIR/doc_unsafe.rs:74:5
2735
|
2836
LL | / pub unsafe fn more_undocumented_unsafe() -> Self {
2937
LL | | unimplemented!();
3038
LL | | }
3139
| |_____^
3240

3341
error: unsafe function's docs miss `# Safety` section
34-
--> $DIR/doc_unsafe.rs:73:9
42+
--> $DIR/doc_unsafe.rs:90:9
3543
|
3644
LL | / pub unsafe fn whee() {
3745
LL | | unimplemented!()
@@ -43,5 +51,5 @@ LL | very_unsafe!();
4351
|
4452
= note: this error originates in the macro `very_unsafe` (in Nightly builds, run with -Z macro-backtrace for more info)
4553

46-
error: aborting due to 5 previous errors
54+
error: aborting due to 6 previous errors
4755

0 commit comments

Comments
 (0)