Skip to content

Lint unused #[non_exhaustive] - #160918

Draft
mu001999 wants to merge 3 commits into
rust-lang:mainfrom
mu001999-contrib:lint-unused-non-exhaustive
Draft

Lint unused #[non_exhaustive]#160918
mu001999 wants to merge 3 commits into
rust-lang:mainfrom
mu001999-contrib:lint-unused-non-exhaustive

Conversation

@mu001999

@mu001999 mu001999 commented Aug 11, 2026

Copy link
Copy Markdown
Member

Fixes #159713

One difference from the original issue is that this PR will lint unreachable items rather than non-pub items.

I think the second commit could be extracted into a separate PR, and I will do it later.

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 11, 2026
@rust-log-analyzer

This comment has been minimized.

@mu001999
mu001999 force-pushed the lint-unused-non-exhaustive branch from 71d91c2 to 6269943 Compare August 11, 2026 15:49
@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job aarch64-gnu-llvm-21-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
+    | ^^^^^^^^^^^^^^^^^^^^^
+    |
+    = note: requested on the command line with `-W unused-attributes`
+ 
855 error: valid forms for the attribute are `doc = "string"`, `doc(alias)`, `doc(attribute)`, `doc(auto_cfg)`, `doc(cfg)`, `doc(fake_variadic)`, `doc(hidden)`, `doc(html_favicon_url)`, `doc(html_logo_url)`, `doc(html_no_source)`, `doc(html_playground_url)`, `doc(html_root_url)`, `doc(include)`, `doc(inline)`, `doc(issue_tracker_base_url)`, `doc(keyword)`, `doc(masked)`, `doc(no_default_passes)`, `doc(no_inline)`, `doc(notable_trait)`, `doc(passes)`, `doc(plugins)`, `doc(rust_logo)`, `doc(search_unbox)`, `doc(spotlight)`, and `doc(test)`
856   --> $DIR/malformed-attrs.rs:41:3
857    |

888 ...  |
889 LL | | }
890    | |_^
-    = note: requested on the command line with `-W unused-attributes`
892 
893 error: valid forms for the attribute are `doc = "string"`, `doc(alias)`, `doc(attribute)`, `doc(auto_cfg)`, `doc(cfg)`, `doc(fake_variadic)`, `doc(hidden)`, `doc(html_favicon_url)`, `doc(html_logo_url)`, `doc(html_no_source)`, `doc(html_playground_url)`, `doc(html_root_url)`, `doc(include)`, `doc(inline)`, `doc(issue_tracker_base_url)`, `doc(keyword)`, `doc(masked)`, `doc(no_default_passes)`, `doc(no_inline)`, `doc(notable_trait)`, `doc(passes)`, `doc(plugins)`, `doc(rust_logo)`, `doc(search_unbox)`, `doc(spotlight)`, and `doc(test)`
894   --> $DIR/malformed-attrs.rs:79:3

984    = note: expected unit type `()`
985               found coroutine `{coroutine@$DIR/malformed-attrs.rs:118:23: 118:25}`
986 
- error: aborting due to 74 previous errors; 8 warnings emitted
+ error: aborting due to 74 previous errors; 9 warnings emitted
988 
989 Some errors have detailed explanations: E0308, E0463, E0539, E0565, E0658, E0805.
---
To only update this specific test, also pass `--test-args attributes/malformed-attrs.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/attributes/malformed-attrs.rs" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/attributes/malformed-attrs" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "incomplete_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers"
stdout: none
--- stderr -------------------------------
error[E0539]: malformed `cfg` attribute input
##[error]  --> /checkout/tests/ui/attributes/malformed-attrs.rs:109:3
   |
---
help: the following are the possible correct uses
   |
LL | #[forbid(lint1)]
   |         +++++++
LL | #[forbid(lint1, lint2, ...)]
   |         +++++++++++++++++++
LL | #[forbid(lint1, lint2, lint3, reason = "...")]
   |         +++++++++++++++++++++++++++++++++++++

error: the `proc_macro` attribute is only usable with crates of the `proc-macro` crate type
##[error]  --> /checkout/tests/ui/attributes/malformed-attrs.rs:106:3
   |
LL | #[proc_macro = 18]
   |   ^^^^^^^^^^

error: the `proc_macro_attribute` attribute is only usable with crates of the `proc-macro` crate type
##[error]  --> /checkout/tests/ui/attributes/malformed-attrs.rs:123:3
   |
---
   |   ^^^^^^^^^^^^^^^^^ expected this to be a list
   |
help: must be of the form
   |
LL | #[rustc_confusables("name1", "name2", ...)]
   |                    +++++++++++++++++++++++

error: the `rustc_confusables` attribute cannot be used on functions
##[error]  --> /checkout/tests/ui/attributes/malformed-attrs.rs:36:3
   |
---
help: try changing it to one of the following valid forms of the attribute
   |
LL | #[rustc_macro_transparency = "opaque"]
   |                            ++++++++++
LL | #[rustc_macro_transparency = "semiopaque"]
   |                            ++++++++++++++
LL | #[rustc_macro_transparency = "transparent"]
   |                            +++++++++++++++

error: the `rustc_macro_transparency` attribute cannot be used on functions
---
   |   ^^^^^^^^^^^ expected this to be a list
   |
help: must be of the form
   |
LL | #[rustc_align(<alignment in bytes>)]
   |              ++++++++++++++++++++++

error[E0539]: malformed `optimize` attribute input
##[error]  --> /checkout/tests/ui/attributes/malformed-attrs.rs:55:3
   |
---
LL | #[optimize(none)]
   |           ++++++
LL | #[optimize(size)]
   |           ++++++
LL | #[optimize(speed)]
   |           +++++++

error[E0805]: malformed `optimize` attribute input
##[error]  --> /checkout/tests/ui/attributes/malformed-attrs.rs:57:3
   |
---
   |
LL - #[optimize(none, none)]
LL + #[optimize(size)]
   |
LL - #[optimize(none, none)]
LL + #[optimize(speed)]
   |

error[E0805]: malformed `optimize` attribute input
##[error]  --> /checkout/tests/ui/attributes/malformed-attrs.rs:59:3
   |
---
   |
LL - #[optimize(none, speed)]
LL + #[optimize(size)]
   |
LL - #[optimize(none, speed)]
LL + #[optimize(speed)]
   |

error[E0565]: malformed `cold` attribute input
##[error]  --> /checkout/tests/ui/attributes/malformed-attrs.rs:61:3
   |
---
   |               didn't expect any arguments here
   |
help: must be of the form
   |
LL - #[unsafe(naked())]
LL + #[unsafe(naked)]
   |

error[E0565]: malformed `track_caller` attribute input
##[error]  --> /checkout/tests/ui/attributes/malformed-attrs.rs:69:3
   |
---
   |   ^^^^^^^^^^^^^^ expected this to be a list
   |
help: must be of the form
   |
LL | #[target_feature(enable = "feat1, feat2")]
   |                 +++++++++++++++++++++++++

error[E0565]: malformed `export_stable` attribute input
##[error]  --> /checkout/tests/ui/attributes/malformed-attrs.rs:83:3
   |
---

error[E0565]: malformed `proc_macro` attribute input
##[error]  --> /checkout/tests/ui/attributes/malformed-attrs.rs:106:3
   |
LL | #[proc_macro = 18]
   |   ^^^^^^^^^^^----
   |              |
   |              didn't expect any arguments here
   |
help: must be of the form
   |
LL - #[proc_macro = 18]
LL + #[proc_macro]
   |

error[E0539]: malformed `instruction_set` attribute input
##[error]  --> /checkout/tests/ui/attributes/malformed-attrs.rs:113:3
   |
---
   |   ^^^^^^^^^^^^^^^^^^^^^^^^ expected this to be a list
   |
help: must be of the form
   |
LL | #[patchable_function_entry(prefix_nops = m, entry_nops = n, section = "section")]
   |                           ++++++++++++++++++++++++++++++++++++++++++++++++++++++

error[E0565]: malformed `coroutine` attribute input
##[error]  --> /checkout/tests/ui/attributes/malformed-attrs.rs:118:7
   |
---
   |
   = note: for more information, visit <https://doc.rust-lang.org/reference/procedural-macros.html#derive-macros>
help: try changing it to one of the following valid forms of the attribute
   |
LL | #[proc_macro_derive(TraitName)]
   |                    +++++++++++
LL | #[proc_macro_derive(TraitName, attributes(name1, name2, ...))]
   |                    ++++++++++++++++++++++++++++++++++++++++++

error[E0539]: malformed `must_not_suspend` attribute input
##[error]  --> /checkout/tests/ui/attributes/malformed-attrs.rs:135:3
   |
---
   |
   = note: for more information, visit <https://doc.rust-lang.org/reference/items/external-blocks.html#the-link_ordinal-attribute>
help: must be of the form
   |
LL |     #[link_ordinal(ordinal)]
   |                   +++++++++

error[E0565]: malformed `ffi_const` attribute input
##[error]  --> /checkout/tests/ui/attributes/malformed-attrs.rs:172:7
   |
---
   |
   = note: for more information, visit <https://doc.rust-lang.org/reference/attributes/debugger.html#the-debugger_visualizer-attribute>
help: must be of the form
   |
LL | #[debugger_visualizer(natvis_file = "...", gdb_script_file = "...")]
   |                      ++++++++++++++++++++++++++++++++++++++++++++++

error[E0565]: malformed `automatically_derived` attribute input
##[error]  --> /checkout/tests/ui/attributes/malformed-attrs.rs:191:3
   |
---
   |
LL - #[macro_use = 1]
LL + #[macro_use]
   |
LL - #[macro_use = 1]
LL + #[macro_use(name1, name2, ...)]
   |

error[E0539]: malformed `macro_export` attribute input
##[error]  --> /checkout/tests/ui/attributes/malformed-attrs.rs:216:3
   |
---
   |
LL | #[allow_internal_unsafe = 1]
   |   ^^^^^^^^^^^^^^^^^^^^^
   |
   = help: add `#![feature(allow_internal_unsafe)]` to the crate attributes to enable
   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0565]: malformed `allow_internal_unsafe` attribute input
##[error]  --> /checkout/tests/ui/attributes/malformed-attrs.rs:218:3
   |
LL | #[allow_internal_unsafe = 1]
---
   | ^^^^^^^^^^^^^^^^^^^^^
   |
   = note: requested on the command line with `-W unused-attributes`

error: valid forms for the attribute are `doc = "string"`, `doc(alias)`, `doc(attribute)`, `doc(auto_cfg)`, `doc(cfg)`, `doc(fake_variadic)`, `doc(hidden)`, `doc(html_favicon_url)`, `doc(html_logo_url)`, `doc(html_no_source)`, `doc(html_playground_url)`, `doc(html_root_url)`, `doc(include)`, `doc(inline)`, `doc(issue_tracker_base_url)`, `doc(keyword)`, `doc(masked)`, `doc(no_default_passes)`, `doc(no_inline)`, `doc(notable_trait)`, `doc(passes)`, `doc(plugins)`, `doc(rust_logo)`, `doc(search_unbox)`, `doc(spotlight)`, and `doc(test)`
##[error]  --> /checkout/tests/ui/attributes/malformed-attrs.rs:41:3
   |
LL | #[doc]
   |   ^^^
   |
---
   |   ^^^^^^^^^^
   |
   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
   = note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>
   = note: `#[deny(ill_formed_attribute_input)]` (part of `#[deny(future_incompatible)]`) on by default

warning: crate-level attribute should be an inner attribute: add an exclamation mark: `#![crate_name]`
##[warning]  --> /checkout/tests/ui/attributes/malformed-attrs.rs:76:1
   |
LL | #[crate_name]
---
...  |
LL | | }
   | |_^

error: valid forms for the attribute are `doc = "string"`, `doc(alias)`, `doc(attribute)`, `doc(auto_cfg)`, `doc(cfg)`, `doc(fake_variadic)`, `doc(hidden)`, `doc(html_favicon_url)`, `doc(html_logo_url)`, `doc(html_no_source)`, `doc(html_playground_url)`, `doc(html_root_url)`, `doc(include)`, `doc(inline)`, `doc(issue_tracker_base_url)`, `doc(keyword)`, `doc(masked)`, `doc(no_default_passes)`, `doc(no_inline)`, `doc(notable_trait)`, `doc(passes)`, `doc(plugins)`, `doc(rust_logo)`, `doc(search_unbox)`, `doc(spotlight)`, and `doc(test)`
##[error]  --> /checkout/tests/ui/attributes/malformed-attrs.rs:79:3
   |
LL | #[doc]
   |   ^^^

---
LL |     #[coroutine = 63] || {}
   |                       ^^^^^ expected `()`, found coroutine
   |
   = note: expected unit type `()`
              found coroutine `{coroutine@/checkout/tests/ui/attributes/malformed-attrs.rs:118:23: 118:25}`

error: aborting due to 74 previous errors; 9 warnings emitted

Some errors have detailed explanations: E0308, E0463, E0539, E0565, E0658, E0805.
For more information about an error, try `rustc --explain E0308`.
---
   |   ^^^^^^^^^^
   |
   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
   = note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>
   = note: `#[deny(ill_formed_attribute_input)]` (part of `#[deny(future_incompatible)]`) on by default

Future breakage diagnostic:
error: valid forms for the attribute are `ignore` and `ignore = "reason"`
##[error]  --> /checkout/tests/ui/attributes/malformed-attrs.rs:99:3
   |
LL | #[ignore()]
   |   ^^^^^^^^
   |
   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
   = note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>
   = note: `#[deny(ill_formed_attribute_input)]` (part of `#[deny(future_incompatible)]`) on by default

Future breakage diagnostic:
error: valid forms for the attribute are `ignore` and `ignore = "reason"`
##[error]  --> /checkout/tests/ui/attributes/malformed-attrs.rs:225:3
   |
LL | #[ignore = 1]
   |   ^^^^^^^^^^
   |
   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
   = note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>
   = note: `#[deny(ill_formed_attribute_input)]` (part of `#[deny(future_incompatible)]`) on by default
------------------------------------------

---- [ui] tests/ui/attributes/malformed-attrs.rs stdout end ----
---- [ui] tests/ui/feature-gates/feature-gate-cfg-target-compact.rs stdout ----
Saved the actual stderr to `/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/feature-gates/feature-gate-cfg-target-compact/feature-gate-cfg-target-compact.stderr`
diff of stderr:

38    = help: add `#![feature(cfg_target_compact)]` to the crate attributes to enable
39    = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
40 
- error: aborting due to 4 previous errors
+ warning: `#[non_exhaustive]` has no effect on an unreachable item
+   --> $DIR/feature-gate-cfg-target-compact.rs:4:34
+    |
---
44 

Note: some mismatched output was normalized before being compared
-   --> /checkout/tests/ui/feature-gates/feature-gate-cfg-target-compact.rs:4:34
- LL | #[cfg_attr(target(os = "linux"), non_exhaustive)] //~ ERROR compact `cfg(target(..))` is experimental
+ warning: `#[non_exhaustive]` has no effect on an unreachable item
+   --> $DIR/feature-gate-cfg-target-compact.rs:4:34
+    |
+ LL | #[cfg_attr(target(os = "linux"), non_exhaustive)]
+    |                                  ^^^^^^^^^^^^^^
+    |
---
To only update this specific test, also pass `--test-args feature-gates/feature-gate-cfg-target-compact.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/feature-gates/feature-gate-cfg-target-compact.rs" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/feature-gates/feature-gate-cfg-target-compact" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "incomplete_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers"
stdout: none
--- stderr -------------------------------
error[E0658]: compact `cfg(target(..))` is experimental and subject to change
##[error]  --> /checkout/tests/ui/feature-gates/feature-gate-cfg-target-compact.rs:1:7
   |
LL | #[cfg(target(os = "linux"))] //~ ERROR compact `cfg(target(..))` is experimental
   |       ^^^^^^^^^^^^^^^^^^^^
   |
   = note: see issue #96901 <https://github.com/rust-lang/rust/issues/96901> for more information
   = help: add `#![feature(cfg_target_compact)]` to the crate attributes to enable
   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: compact `cfg(target(..))` is experimental and subject to change
##[error]  --> /checkout/tests/ui/feature-gates/feature-gate-cfg-target-compact.rs:4:12
   |
LL | #[cfg_attr(target(os = "linux"), non_exhaustive)] //~ ERROR compact `cfg(target(..))` is experimental
   |            ^^^^^^^^^^^^^^^^^^^^
   |
   = note: see issue #96901 <https://github.com/rust-lang/rust/issues/96901> for more information
   = help: add `#![feature(cfg_target_compact)]` to the crate attributes to enable
   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: compact `cfg(target(..))` is experimental and subject to change
##[error]  --> /checkout/tests/ui/feature-gates/feature-gate-cfg-target-compact.rs:7:19
   |
LL | #[cfg(not(any(all(target(os = "linux")))))] //~ ERROR compact `cfg(target(..))` is experimental
   |                   ^^^^^^^^^^^^^^^^^^^^
   |
   = note: see issue #96901 <https://github.com/rust-lang/rust/issues/96901> for more information
   = help: add `#![feature(cfg_target_compact)]` to the crate attributes to enable
   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: compact `cfg(target(..))` is experimental and subject to change
##[error]  --> /checkout/tests/ui/feature-gates/feature-gate-cfg-target-compact.rs:11:10
   |
LL |     cfg!(target(os = "linux"));
   |          ^^^^^^^^^^^^^^^^^^^^
   |
   = note: see issue #96901 <https://github.com/rust-lang/rust/issues/96901> for more information
   = help: add `#![feature(cfg_target_compact)]` to the crate attributes to enable
   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

warning: `#[non_exhaustive]` has no effect on an unreachable item
##[warning]  --> /checkout/tests/ui/feature-gates/feature-gate-cfg-target-compact.rs:4:34
   |
LL | #[cfg_attr(target(os = "linux"), non_exhaustive)] //~ ERROR compact `cfg(target(..))` is experimental
   |                                  ^^^^^^^^^^^^^^
   |
   = note: requested on the command line with `-W unused-attributes`

error: aborting due to 4 previous errors; 1 warning emitted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Request: lint on #[non_exhaustive] that do nothing

3 participants