about summary refs log tree commit diff
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2021-09-28 08:33:58 -0700
committerManish Goregaokar <manishsmail@gmail.com>2021-09-28 08:40:59 -0700
commit25850fc264ff71fcdbfd3594e8d5a299d7dbe2c0 (patch)
tree1409213095472bcbad7f7b53529cc97ff16d64c3
parent13834e6ad2294a95c664aca2acaedb1a9619773f (diff)
downloadrust-25850fc264ff71fcdbfd3594e8d5a299d7dbe2c0.tar.gz
rust-25850fc264ff71fcdbfd3594e8d5a299d7dbe2c0.zip
Make doc_unsafe lint on unsafe traits as well
-rw-r--r--clippy_lints/src/doc.rs12
-rw-r--r--tests/ui/def_id_nocore.rs1
-rw-r--r--tests/ui/def_id_nocore.stderr2
-rw-r--r--tests/ui/doc_unsafe.rs21
-rw-r--r--tests/ui/doc_unsafe.stderr14
5 files changed, 43 insertions, 7 deletions
diff --git a/clippy_lints/src/doc.rs b/clippy_lints/src/doc.rs
index cb2b7f5be70..0a9edfd7334 100644
--- a/clippy_lints/src/doc.rs
+++ b/clippy_lints/src/doc.rs
@@ -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",
+                    );
+                }
+            },
+            _ => (),
         }
     }
 
diff --git a/tests/ui/def_id_nocore.rs b/tests/ui/def_id_nocore.rs
index cba7666c2d8..9ff3b5e5ef5 100644
--- a/tests/ui/def_id_nocore.rs
+++ b/tests/ui/def_id_nocore.rs
@@ -3,6 +3,7 @@
 
 #![feature(no_core, lang_items, start)]
 #![no_core]
+#![allow(clippy::missing_safety_doc)]
 
 #[link(name = "c")]
 extern "C" {}
diff --git a/tests/ui/def_id_nocore.stderr b/tests/ui/def_id_nocore.stderr
index 702684f6b43..6210d7c6cfd 100644
--- a/tests/ui/def_id_nocore.stderr
+++ b/tests/ui/def_id_nocore.stderr
@@ -1,5 +1,5 @@
 error: methods called `as_*` usually take `self` by reference or `self` by mutable reference
-  --> $DIR/def_id_nocore.rs:26:19
+  --> $DIR/def_id_nocore.rs:27:19
    |
 LL |     pub fn as_ref(self) -> &'static str {
    |                   ^^^^
diff --git a/tests/ui/doc_unsafe.rs b/tests/ui/doc_unsafe.rs
index 484aa72d59a..8f823f1672b 100644
--- a/tests/ui/doc_unsafe.rs
+++ b/tests/ui/doc_unsafe.rs
@@ -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
     }
@@ -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!();
diff --git a/tests/ui/doc_unsafe.stderr b/tests/ui/doc_unsafe.stderr
index 73b53f3431e..d540abd1262 100644
--- a/tests/ui/doc_unsafe.stderr
+++ b/tests/ui/doc_unsafe.stderr
@@ -22,8 +22,16 @@ 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!();
@@ -31,7 +39,7 @@ 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!()
@@ -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