about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorAlex Macleod <alex@macleod.io>2024-09-04 21:22:28 +0000
committerAlex Macleod <alex@macleod.io>2024-09-04 21:22:28 +0000
commitf7f550561e801fa1adb74377fd95a151820d8d0e (patch)
tree277ead421c174f371d399bf8a447c70ac80af8e1 /tests
parenta81f1c8277a70a2b4f5d87926221ba5b37d4c3db (diff)
downloadrust-f7f550561e801fa1adb74377fd95a151820d8d0e.tar.gz
rust-f7f550561e801fa1adb74377fd95a151820d8d0e.zip
Only lint `manual_non_exhaustive` for exported types
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/manual_non_exhaustive_enum.rs24
-rw-r--r--tests/ui/manual_non_exhaustive_enum.stderr22
-rw-r--r--tests/ui/manual_non_exhaustive_struct.rs50
-rw-r--r--tests/ui/manual_non_exhaustive_struct.stderr64
4 files changed, 92 insertions, 68 deletions
diff --git a/tests/ui/manual_non_exhaustive_enum.rs b/tests/ui/manual_non_exhaustive_enum.rs
index 31c3cc80137..ffe2bb92467 100644
--- a/tests/ui/manual_non_exhaustive_enum.rs
+++ b/tests/ui/manual_non_exhaustive_enum.rs
@@ -1,8 +1,8 @@
 #![warn(clippy::manual_non_exhaustive)]
 #![allow(unused)]
 //@no-rustfix
-enum E {
-    //~^ ERROR: this seems like a manual implementation of the non-exhaustive pattern
+pub enum E {
+    //~^ manual_non_exhaustive
     A,
     B,
     #[doc(hidden)]
@@ -11,7 +11,7 @@ enum E {
 
 // if the user explicitly marks as nonexhaustive we shouldn't warn them
 #[non_exhaustive]
-enum Ep {
+pub enum Ep {
     A,
     B,
     #[doc(hidden)]
@@ -19,14 +19,14 @@ enum Ep {
 }
 
 // marker variant does not have doc hidden attribute, should be ignored
-enum NoDocHidden {
+pub enum NoDocHidden {
     A,
     B,
     _C,
 }
 
 // name of variant with doc hidden does not start with underscore
-enum NoUnderscore {
+pub enum NoUnderscore {
     A,
     B,
     #[doc(hidden)]
@@ -34,7 +34,7 @@ enum NoUnderscore {
 }
 
 // variant with doc hidden is not unit, should be ignored
-enum NotUnit {
+pub enum NotUnit {
     A,
     B,
     #[doc(hidden)]
@@ -42,13 +42,13 @@ enum NotUnit {
 }
 
 // variant with doc hidden is the only one, should be ignored
-enum OnlyMarker {
+pub enum OnlyMarker {
     #[doc(hidden)]
     _A,
 }
 
 // variant with multiple markers, should be ignored
-enum MultipleMarkers {
+pub enum MultipleMarkers {
     A,
     #[doc(hidden)]
     _B,
@@ -58,13 +58,13 @@ enum MultipleMarkers {
 
 // already non_exhaustive and no markers, should be ignored
 #[non_exhaustive]
-enum NonExhaustive {
+pub enum NonExhaustive {
     A,
     B,
 }
 
 // marked is used, don't lint
-enum UsedHidden {
+pub enum UsedHidden {
     #[doc(hidden)]
     _A,
     B,
@@ -77,11 +77,9 @@ fn foo(x: &mut UsedHidden) {
 }
 
 #[expect(clippy::manual_non_exhaustive)]
-enum ExpectLint {
+pub enum ExpectLint {
     A,
     B,
     #[doc(hidden)]
     _C,
 }
-
-fn main() {}
diff --git a/tests/ui/manual_non_exhaustive_enum.stderr b/tests/ui/manual_non_exhaustive_enum.stderr
index dc669568dd2..0a9ac157f85 100644
--- a/tests/ui/manual_non_exhaustive_enum.stderr
+++ b/tests/ui/manual_non_exhaustive_enum.stderr
@@ -1,11 +1,7 @@
 error: this seems like a manual implementation of the non-exhaustive pattern
   --> tests/ui/manual_non_exhaustive_enum.rs:4:1
    |
-LL |   enum E {
-   |   ^-----
-   |   |
-   |  _help: add the attribute: `#[non_exhaustive] enum E`
-   | |
+LL | / pub enum E {
 LL | |
 LL | |     A,
 LL | |     B,
@@ -21,15 +17,16 @@ LL |     _C,
    |     ^^
    = note: `-D clippy::manual-non-exhaustive` implied by `-D warnings`
    = help: to override `-D warnings` add `#[allow(clippy::manual_non_exhaustive)]`
+help: use the `#[non_exhaustive]` attribute instead
+   |
+LL + #[non_exhaustive]
+LL | pub enum E {
+   |
 
 error: this seems like a manual implementation of the non-exhaustive pattern
   --> tests/ui/manual_non_exhaustive_enum.rs:29:1
    |
-LL |   enum NoUnderscore {
-   |   ^----------------
-   |   |
-   |  _help: add the attribute: `#[non_exhaustive] enum NoUnderscore`
-   | |
+LL | / pub enum NoUnderscore {
 LL | |     A,
 LL | |     B,
 LL | |     #[doc(hidden)]
@@ -42,6 +39,11 @@ help: remove this variant
    |
 LL |     C,
    |     ^
+help: use the `#[non_exhaustive]` attribute instead
+   |
+LL + #[non_exhaustive]
+LL | pub enum NoUnderscore {
+   |
 
 error: aborting due to 2 previous errors
 
diff --git a/tests/ui/manual_non_exhaustive_struct.rs b/tests/ui/manual_non_exhaustive_struct.rs
index 4b2803ccc4a..31dd50281fa 100644
--- a/tests/ui/manual_non_exhaustive_struct.rs
+++ b/tests/ui/manual_non_exhaustive_struct.rs
@@ -1,9 +1,9 @@
 #![warn(clippy::manual_non_exhaustive)]
 #![allow(unused)]
 //@no-rustfix
-mod structs {
-    struct S {
-        //~^ ERROR: this seems like a manual implementation of the non-exhaustive pattern
+pub mod structs {
+    pub struct S {
+        //~^ manual_non_exhaustive
         pub a: i32,
         pub b: i32,
         _c: (),
@@ -11,68 +11,76 @@ mod structs {
 
     // user forgot to remove the private field
     #[non_exhaustive]
-    struct Sp {
-        //~^ ERROR: this seems like a manual implementation of the non-exhaustive pattern
+    pub struct Sp {
+        //~^ manual_non_exhaustive
         pub a: i32,
         pub b: i32,
         _c: (),
     }
 
     // some other fields are private, should be ignored
-    struct PrivateFields {
+    pub struct PrivateFields {
         a: i32,
         pub b: i32,
         _c: (),
     }
 
-    // private field name does not start with underscore, should be ignored
-    struct NoUnderscore {
+    pub struct NoUnderscore {
+        //~^ manual_non_exhaustive
         pub a: i32,
         pub b: i32,
         c: (),
     }
 
     // private field is not unit type, should be ignored
-    struct NotUnit {
+    pub struct NotUnit {
         pub a: i32,
         pub b: i32,
         _c: i32,
     }
 
     // private field is the only field, should be ignored
-    struct OnlyMarker {
+    pub struct OnlyMarker {
         _a: (),
     }
 
     // already non exhaustive and no private fields, should be ignored
     #[non_exhaustive]
-    struct NonExhaustive {
+    pub struct NonExhaustive {
         pub a: i32,
         pub b: i32,
     }
 }
 
-mod tuple_structs {
-    struct T(pub i32, pub i32, ());
-    //~^ ERROR: this seems like a manual implementation of the non-exhaustive pattern
+pub mod tuple_structs {
+    pub struct T(pub i32, pub i32, ());
+    //~^ manual_non_exhaustive
 
     // user forgot to remove the private field
     #[non_exhaustive]
-    struct Tp(pub i32, pub i32, ());
-    //~^ ERROR: this seems like a manual implementation of the non-exhaustive pattern
+    pub struct Tp(pub i32, pub i32, ());
+    //~^ manual_non_exhaustive
 
     // some other fields are private, should be ignored
-    struct PrivateFields(pub i32, i32, ());
+    pub struct PrivateFields(pub i32, i32, ());
 
     // private field is not unit type, should be ignored
-    struct NotUnit(pub i32, pub i32, i32);
+    pub struct NotUnit(pub i32, pub i32, i32);
 
     // private field is the only field, should be ignored
-    struct OnlyMarker(());
+    pub struct OnlyMarker(());
 
     // already non exhaustive and no private fields, should be ignored
     #[non_exhaustive]
-    struct NonExhaustive(pub i32, pub i32);
+    pub struct NonExhaustive(pub i32, pub i32);
 }
 
-fn main() {}
+mod private {
+    // Don't lint structs that are not actually public as `#[non_exhaustive]` only applies to
+    // external crates. The manual pattern can still be used to get module local non exhaustiveness
+    pub struct NotPublic {
+        pub a: i32,
+        pub b: i32,
+        _c: (),
+    }
+}
diff --git a/tests/ui/manual_non_exhaustive_struct.stderr b/tests/ui/manual_non_exhaustive_struct.stderr
index 1cab812988a..81de1e4f271 100644
--- a/tests/ui/manual_non_exhaustive_struct.stderr
+++ b/tests/ui/manual_non_exhaustive_struct.stderr
@@ -1,11 +1,7 @@
 error: this seems like a manual implementation of the non-exhaustive pattern
   --> tests/ui/manual_non_exhaustive_struct.rs:5:5
    |
-LL |       struct S {
-   |       ^-------
-   |       |
-   |  _____help: add the attribute: `#[non_exhaustive] struct S`
-   | |
+LL | /     pub struct S {
 LL | |
 LL | |         pub a: i32,
 LL | |         pub b: i32,
@@ -20,11 +16,16 @@ LL |         _c: (),
    |         ^^^^^^
    = note: `-D clippy::manual-non-exhaustive` implied by `-D warnings`
    = help: to override `-D warnings` add `#[allow(clippy::manual_non_exhaustive)]`
+help: use the `#[non_exhaustive]` attribute instead
+   |
+LL ~     #[non_exhaustive]
+LL ~     pub struct S {
+   |
 
 error: this seems like a manual implementation of the non-exhaustive pattern
   --> tests/ui/manual_non_exhaustive_struct.rs:14:5
    |
-LL | /     struct Sp {
+LL | /     pub struct Sp {
 LL | |
 LL | |         pub a: i32,
 LL | |         pub b: i32,
@@ -32,6 +33,11 @@ LL | |         _c: (),
 LL | |     }
    | |_____^
    |
+note: the struct is already non-exhaustive
+  --> tests/ui/manual_non_exhaustive_struct.rs:13:5
+   |
+LL |     #[non_exhaustive]
+   |     ^^^^^^^^^^^^^^^^^
 help: remove this field
   --> tests/ui/manual_non_exhaustive_struct.rs:18:9
    |
@@ -39,13 +45,10 @@ LL |         _c: (),
    |         ^^^^^^
 
 error: this seems like a manual implementation of the non-exhaustive pattern
-  --> tests/ui/manual_non_exhaustive_struct.rs:29:5
+  --> tests/ui/manual_non_exhaustive_struct.rs:28:5
    |
-LL |       struct NoUnderscore {
-   |       ^------------------
-   |       |
-   |  _____help: add the attribute: `#[non_exhaustive] struct NoUnderscore`
-   | |
+LL | /     pub struct NoUnderscore {
+LL | |
 LL | |         pub a: i32,
 LL | |         pub b: i32,
 LL | |         c: (),
@@ -57,32 +60,45 @@ help: remove this field
    |
 LL |         c: (),
    |         ^^^^^
+help: use the `#[non_exhaustive]` attribute instead
+   |
+LL ~     #[non_exhaustive]
+LL ~     pub struct NoUnderscore {
+   |
 
 error: this seems like a manual implementation of the non-exhaustive pattern
   --> tests/ui/manual_non_exhaustive_struct.rs:56:5
    |
-LL |     struct T(pub i32, pub i32, ());
-   |     --------^^^^^^^^^^^^^^^^^^^^^^^
-   |     |
-   |     help: add the attribute: `#[non_exhaustive] struct T`
+LL |     pub struct T(pub i32, pub i32, ());
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
 help: remove this field
-  --> tests/ui/manual_non_exhaustive_struct.rs:56:32
+  --> tests/ui/manual_non_exhaustive_struct.rs:56:36
+   |
+LL |     pub struct T(pub i32, pub i32, ());
+   |                                    ^^
+help: use the `#[non_exhaustive]` attribute instead
+   |
+LL ~     #[non_exhaustive]
+LL ~     pub struct T(pub i32, pub i32, ());
    |
-LL |     struct T(pub i32, pub i32, ());
-   |                                ^^
 
 error: this seems like a manual implementation of the non-exhaustive pattern
   --> tests/ui/manual_non_exhaustive_struct.rs:61:5
    |
-LL |     struct Tp(pub i32, pub i32, ());
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+LL |     pub struct Tp(pub i32, pub i32, ());
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+note: the struct is already non-exhaustive
+  --> tests/ui/manual_non_exhaustive_struct.rs:60:5
    |
+LL |     #[non_exhaustive]
+   |     ^^^^^^^^^^^^^^^^^
 help: remove this field
-  --> tests/ui/manual_non_exhaustive_struct.rs:61:33
+  --> tests/ui/manual_non_exhaustive_struct.rs:61:37
    |
-LL |     struct Tp(pub i32, pub i32, ());
-   |                                 ^^
+LL |     pub struct Tp(pub i32, pub i32, ());
+   |                                     ^^
 
 error: aborting due to 5 previous errors