Skip to content

Function only used in const assert is treated as dead code #142104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
clarfonthey opened this issue Jun 6, 2025 · 3 comments · Fixed by #142208
Closed

Function only used in const assert is treated as dead code #142104

clarfonthey opened this issue Jun 6, 2025 · 3 comments · Fixed by #142208
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. L-dead_code Lint: dead_code T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@clarfonthey
Copy link
Contributor

clarfonthey commented Jun 6, 2025

I tried this code: playground link

const fn is_nonzero(x: u8) -> bool {
    x != 0
}
const _: () = {
    assert!(is_nonzero(2));
};

I expected to compile without any warnings.

Instead, it indicates that is_nonzero is dead code:

warning: function `is_nonzero` is never used
 --> src/lib.rs:1:10
  |
1 | const fn is_nonzero(x: u8) -> bool {
  |          ^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

Meta

rustc --version --verbose:

rustc 1.89.0-nightly (5d707b07e 2025-06-02)
binary: rustc
commit-hash: 5d707b07e42766c080c5012869c9988a18dcbb83
commit-date: 2025-06-02
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5
@clarfonthey clarfonthey added the C-bug Category: This is a bug. label Jun 6, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jun 6, 2025
@Noratrieb Noratrieb added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. L-dead_code Lint: dead_code A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jun 6, 2025
@Noratrieb
Copy link
Member

Noratrieb commented Jun 6, 2025

Constants with the name _ should be seen as live for the dead code analysis.

@hanna-kruppe
Copy link
Contributor

Some previous discussion in #142075

@DoubleHyphen
Copy link

I swear, myself and @clarfonthey are completely different people.

@bors bors closed this as completed in facc5da Jun 9, 2025
rust-timer added a commit that referenced this issue Jun 9, 2025
Rollup merge of #142208 - Urgau:dead_code-const_, r=petrochenkov

Always consider `const _` items as live for dead code analysis

This PR alters dead code analysis to always consider `const _: () = { ... };` to be live.

This doesn't address the `_name` pattern from #142075.

Fixes #142104
tgross35 added a commit to tgross35/compiler-builtins that referenced this issue Jun 9, 2025
Always consider `const _` items as live for dead code analysis

This PR alters dead code analysis to always consider `const _: () = { ... };` to be live.

This doesn't address the `_name` pattern from rust-lang/rust#142075.

Fixes rust-lang/rust#142104
github-actions bot pushed a commit to rust-lang/miri that referenced this issue Jun 10, 2025
Always consider `const _` items as live for dead code analysis

This PR alters dead code analysis to always consider `const _: () = { ... };` to be live.

This doesn't address the `_name` pattern from rust-lang/rust#142075.

Fixes rust-lang/rust#142104
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. L-dead_code Lint: dead_code T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants