Skip to content

Commit

Permalink
Make doc_unsafe lint on unsafe traits as well
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Sep 28, 2021
1 parent 13834e6 commit 09e2130
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
12 changes: 11 additions & 1 deletion clippy_lints/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,17 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
hir::ItemKind::Impl(ref impl_) => {
self.in_trait_impl = impl_.of_trait.is_some();
},
_ => {},
hir::ItemKind::Trait(_, unsafety, ..) => {
if !headers.safety && unsafety == hir::Unsafety::Unsafe {
span_lint(
cx,
MISSING_SAFETY_DOC,
item.span,
"unsafe trait's docs miss `# Safety` section",
);
}
}
_ => ()
}
}

Expand Down
21 changes: 19 additions & 2 deletions tests/ui/doc_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,25 @@ mod private_mod {

pub use private_mod::republished;

pub trait UnsafeTrait {
pub trait SafeTraitUnsafeMethods {
unsafe fn woefully_underdocumented(self);

/// # Safety
unsafe fn at_least_somewhat_documented(self);
}

pub unsafe trait UnsafeTrait {
fn method();
}

/// # Safety
pub unsafe trait DocumentedUnsafeTrait {
fn method2();
}

pub struct Struct;

impl UnsafeTrait for Struct {
impl SafeTraitUnsafeMethods for Struct {
unsafe fn woefully_underdocumented(self) {
// all is well
}
Expand All @@ -53,6 +62,14 @@ impl UnsafeTrait for Struct {
}
}

unsafe impl UnsafeTrait for Struct {
fn method() {}
}

unsafe impl DocumentedUnsafeTrait for Struct {
fn method2() {}
}

impl Struct {
pub unsafe fn more_undocumented_unsafe() -> Self {
unimplemented!();
Expand Down
14 changes: 11 additions & 3 deletions tests/ui/doc_unsafe.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,24 @@ error: unsafe function's docs miss `# Safety` section
LL | unsafe fn woefully_underdocumented(self);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: unsafe trait's docs miss `# Safety` section
--> $DIR/doc_unsafe.rs:44:1
|
LL | / pub unsafe trait UnsafeTrait {
LL | | fn method();
LL | | }
| |_^

error: unsafe function's docs miss `# Safety` section
--> $DIR/doc_unsafe.rs:57:5
--> $DIR/doc_unsafe.rs:74:5
|
LL | / pub unsafe fn more_undocumented_unsafe() -> Self {
LL | | unimplemented!();
LL | | }
| |_____^

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

error: aborting due to 5 previous errors
error: aborting due to 6 previous errors

0 comments on commit 09e2130

Please sign in to comment.