Skip to content

fix: unfulfilled nested dead code lint - #161005

Open
dronavallipranav wants to merge 1 commit into
rust-lang:mainfrom
dronavallipranav:fix-nested-dead-code-expectations
Open

dronavallipranav wants to merge 1 commit into
rust-lang:mainfrom
dronavallipranav:fix-nested-dead-code-expectations

Conversation

@dronavallipranav

@dronavallipranav dronavallipranav commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Fixes #160942

Dead code diagnostic being covered by parent's dead diagnostic resulting in false positives on expectations not being fulfilled. fixed by checking each child of a dead item for its own expectation now and fulfilling their expectation (if valid). Follows suite for manually fulfilling expect level diagnostics (copies mechanism from LintContext::fulfill_expectation.

UI tests fail as expected on main (without fix)

Input

// This struct is only used on windows.
#[cfg_attr(not(target_os = "windows"), expect(unused))]
struct Foo {
    // This field is never used regardless of whether on windows or other.
    #[expect(unused)]
    x: u32,
}

Output
Before

warning: this lint expectation is unfulfilled
 --> src/lib.rs:5:14
  |
5 |     #[expect(unused)]
  |              ^^^^^^

After: no output

I used llm originally to get an understanding of the existing flows and context of the flow around the problem, through prompting helped locate how could reuse the dummy diagnostic lint strategy created cases and I worked backwards from these cases to generate implementation and corresponding logic. I then had it wrote UI tests for those cases for me. The UI tests felt redundant to recreate since I already had it generate the original cases the linter was meant to handle and after review they seem robust couldn't seem to come up with another case.

 expectations not fulfulling
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 12, 2026
@rustbot

rustbot commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

r? @folkertdev

rustbot has assigned @folkertdev.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 75 candidates
  • Random selection from 17 candidates

@folkertdev

Copy link
Copy Markdown
Contributor

r? JonathanBrouwer because lints?

I'm not sure we even want this though, it probably needs T-lang approval, or input at least

@JonathanBrouwer

JonathanBrouwer commented Aug 14, 2026

Copy link
Copy Markdown
Member

I feel a bit iffy about whether the original issue is really an issue, so I'll let the lang team decide.
Explanation for T-lang:

struct Foo {
    #[expect(unused)]
    x: u32,
}

Currently this example produces the following output

warning: struct `Foo` is never constructed
 --> src/main.rs:6:8
  |
6 | struct Foo {
  |        ^^^
  |
  = note: `#[warn(dead_code)]` (part of `#[warn(unused)]`) on by default

warning: this lint expectation is unfulfilled
 --> src/main.rs:7:14
  |
7 |     #[expect(unused)]
  |              ^^^^^^
  |
  = note: `#[warn(unfulfilled_lint_expectations)]` on by default

This is because if Foo is unused, we don't emit a lint that x is unused.
This PR changes that so that this example only produces the first warning.

This is implemented by recursively matching expectations of specifically the dead-code lint on children of dead items. Having such a special case for the expect attribute feels weird, since expect currently works the same way for all lints.

On the other hand, this does unlock the usecase from the original issue where expect can be used to really ensure that something is unused.

@rust-lang/lang @rust-lang/lang-advisors
How do you feel about this?

@JonathanBrouwer JonathanBrouwer added I-lang-nominated Nominated for discussion during a lang team meeting. S-waiting-on-t-lang Status: Awaiting decision from T-lang and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 14, 2026
@traviscross traviscross added the P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang label Aug 15, 2026
@traviscross

traviscross commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

Some observations: we don't allow this, today:

#![deny(unfulfilled_lint_expectations)]

#[expect(unused)]
struct Root {
    #[expect(unused)] // ERROR: this lint expectation is unfulfilled
    leaf: u8,
}

And we don't allow:

#![deny(unfulfilled_lint_expectations)]

#[expect(dead_code)]
fn root() {
    #[expect(dead_code)] // ERROR: this lint expectation is unfulfilled
    fn leaf() {}
    leaf()
}

But we do allow this:

#![deny(unfulfilled_lint_expectations)]

#[expect(dead_code)]
fn root() { leaf() }

#[expect(dead_code)]
fn leaf() {} // OK.

@traviscross traviscross added the T-lang Relevant to the language team label Aug 15, 2026
@steffahn

steffahn commented Aug 19, 2026

Copy link
Copy Markdown
Member

Unless it already exists somewhere else, I think we'd want to also have/add the test case @JonathanBrouwer mentioned of

struct Foo {
    #[expect(unused)]
    x: u32,
}

to the test suite, where Foo is completely unused to make sure that the warning: struct Foo is never constructed itself does not disappear from the expect(unused) on the field.

@traviscross

Copy link
Copy Markdown
Contributor

We talked about this in the lang call today. On the call, our expectation was that all three of the examples in #161005 (comment) should be accepted (i.e., the expectation fulfilled), in principle. That's what we'd document as the intended behavior in the Reference. That said, we're cognizant of possible performance implications, and we leave to compiler the question of whether or when to implement this intended semantic on performance grounds.

@rfcbot fcp merge lang

@rust-rfcbot

rust-rfcbot commented Aug 19, 2026

Copy link
Copy Markdown
Collaborator

@traviscross has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns.
See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Aug 19, 2026
@traviscross traviscross added the I-lang-radar Items that are on lang's radar and will need eventual work or consideration. label Aug 19, 2026
@nikomatsakis

Copy link
Copy Markdown
Contributor

@rfcbot reviewed

@rust-rfcbot rust-rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Sep 2, 2026
@rust-rfcbot

Copy link
Copy Markdown
Collaborator

🔔 This is now entering its final comment period, as per the review above. 🔔

@traviscross traviscross removed I-lang-nominated Nominated for discussion during a lang team meeting. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang labels Sep 2, 2026
@tmandry

tmandry commented Sep 2, 2026

Copy link
Copy Markdown
Member

@rfcbot reviewed

@JonathanBrouwer

Copy link
Copy Markdown
Member

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Sep 3, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Sep 3, 2026
…tions, r=<try>

fix: unfulfilled nested dead code lint
@rust-bors

rust-bors Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 4169f58 (4169f58cb9e8bef1a3fe8b5df2da0227a5253aba)
Base parent: 4fcf397 (4fcf39725a9c99bd495d8c73af83628a256ff9a9)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (4169f58): comparison URL.

Overall result: ❌✅ regressions and improvements - no action needed

Benchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up.

@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.1% [0.1%, 0.1%] 6
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.2% [-0.2%, -0.2%] 1
All ❌✅ (primary) - - 0

Max RSS (memory usage)

This perf run didn't have relevant results for this metric.

Cycles

Results (primary -0.3%, secondary -3.8%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.2% [2.2%, 2.2%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.9% [-2.9%, -2.9%] 1
Improvements ✅
(secondary)
-3.8% [-3.8%, -3.8%] 1
All ❌✅ (primary) -0.3% [-2.9%, 2.2%] 2

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 476.065s -> 473.967s (-0.44%)
Artifact size: 401.18 MiB -> 401.09 MiB (-0.02%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Sep 3, 2026
@rust-rfcbot rust-rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. to-announce Announce this issue on triage meeting and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Sep 12, 2026
@rust-rfcbot

Copy link
Copy Markdown
Collaborator

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

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

Labels

disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team to-announce Announce this issue on triage meeting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unfulfilled_lint_expectations false-positive for unused struct field inside unused struct

10 participants