Skip to content

Conversation

@eggyal
Copy link
Contributor

@eggyal eggyal commented Jan 23, 2026

Unused assignments to an unused variable should trigger only the unused_variables lint and not also the unused_assignments lint. This was previously implemented by checking whether the span of the assignee was within the span of the binding pattern, however that failed to capture situations was imported from elsewhere (eg from the input tokenstream of a proc-macro that generates the binding pattern).

By comparing the span of the assignee to those of the variable introductions instead, a reported stable-to-stable regression is resolved.

This fix also impacted some other preexisting tests, which had (undesirably) been triggering both the unused_variables and unused_assignments lints on the same initializing assignment; those tests have therefore now been updated to expect only the former lint.

Fixes #151514
r? cjgillot (as author of reworked liveness testing in #142390)

Unused assignments to an unused variable should trigger only the
`unused_variables` lint and not also the `unused_assignments` lint.
This was previously implemented by checking whether the span of the
assignee was within the span of the binding pattern, however that failed
to capture situations was imported from elsewhere (eg from the input
tokenstream of a proc-macro that generates the binding pattern).

By comparing the span of the assignee to those of the variable
introductions instead, a reported stable-to-stable regression is
resolved.

This fix also impacted some other preexisting tests, which had
(undesirably) been triggering both the `unused_variables` and
`unused_assignments` lints on the same initializing assignment; those
tests have therefore now been updated to expect only the former lint.
@rustbot
Copy link
Collaborator

rustbot commented Jan 23, 2026

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@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 Jan 23, 2026
@@ -53,7 +53,6 @@ pub fn main() {
}
match (E::E { a: 10, e: C { c: 20 } }) {
mut x @ E::E{ a, e: C { mut c } } => {
//~^ WARN value assigned to `a` is never read
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line does still trigger the unused_variables lint in respect of a, but that lint is allowed by the test and so is not shown here. I'd be happy to enable it, but there are a bunch of other occasions in the test that would then trigger it (if enabled test-wide; of course I could just enable it for this scope).

Copy link

@Tuntii Tuntii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The resulting diagnostics are more precise without weakening either lint.

View changes since this review

// from the list of assignments the ones that happen at the definition site.
statements.retain(|source_info, _| {
source_info.span.find_ancestor_inside(binding.pat_span).is_none()
!binding.introductions.iter().any(|intro| intro.span == source_info.span)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This additional filter against binding.introductions makes sense, especially for proc-macro generated bindings where spans don’t line up lexically. It avoids double warnings without weakening the lint.

quote! {
impl Drop for $name {
fn drop(&mut self) {
let Self { $field } = self;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the fix, the compiler would incorrectly emit both unused_variables and unused_assignments. This test clearly asserts the intended behavior.

@cjgillot
Copy link
Contributor

r=me if perf is clean

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Jan 24, 2026
…r=<try>

Fix suppression of `unused_assignment` in binding of `unused_variable`
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jan 24, 2026
@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 24, 2026

☀️ Try build successful (CI)
Build commit: 38bad8b (38bad8be43d60c5e464300a5658806ef6ef23a6c, parent: 021fc25b7a48f6051bee1e1f06c7a277e4de1cc9)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (38bad8b): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results (primary 2.4%)

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

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

Cycles

Results (primary -1.7%)

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

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.7% [-2.9%, -0.4%] 2
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -1.7% [-2.9%, -0.4%] 2

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 472.296s -> 472.772s (0.10%)
Artifact size: 383.54 MiB -> 383.54 MiB (-0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jan 24, 2026
@cjgillot
Copy link
Contributor

@bors r+ rollup

@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 25, 2026

📌 Commit 22b3f59 has been approved by cjgillot

It is now in the queue for this repository.

@rust-bors rust-bors bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 25, 2026
@rust-bors

This comment has been minimized.

@rust-bors rust-bors bot added merged-by-bors This PR was explicitly merged by bors. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jan 25, 2026
@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 25, 2026

☀️ Test successful - CI
Approved by: cjgillot
Duration: 3h 19m 48s
Pushing 9415853 to main...

@rust-bors rust-bors bot merged commit 9415853 into rust-lang:main Jan 25, 2026
13 checks passed
@rustbot rustbot added this to the 1.95.0 milestone Jan 25, 2026
@github-actions
Copy link
Contributor

What is this? This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.

Comparing 4d38622 (parent) -> 9415853 (this PR)

Test differences

Show 5 test diffs

Stage 1

  • [ui] tests/ui/lint/unused/unused_assignment.rs: [missing] -> pass (J1)

Stage 2

  • [ui] tests/ui/lint/unused/unused_assignment.rs: [missing] -> pass (J0)

Additionally, 3 doctest diffs were found. These are ignored, as they are noisy.

Job group index

Test dashboard

Run

cargo run --manifest-path src/ci/citool/Cargo.toml -- \
    test-dashboard 9415853279dda1394c1f7d4114e1fe1e3ced76ec --output-dir test-dashboard

And then open test-dashboard/index.html in your browser to see an overview of all executed tests.

Job duration changes

  1. dist-x86_64-apple: 6657.5s -> 8340.7s (+25.3%)
  2. x86_64-gnu-llvm-20-3: 6061.4s -> 6686.7s (+10.3%)
  3. aarch64-msvc-2: 5528.7s -> 6088.0s (+10.1%)
  4. i686-gnu-nopt-1: 7254.3s -> 7949.1s (+9.6%)
  5. x86_64-mingw-1: 10261.6s -> 9283.9s (-9.5%)
  6. x86_64-gnu-llvm-21-2: 5435.7s -> 4937.2s (-9.2%)
  7. dist-loongarch64-linux: 5096.3s -> 5549.0s (+8.9%)
  8. pr-check-2: 2496.0s -> 2292.6s (-8.1%)
  9. x86_64-gnu-stable: 8043.7s -> 7392.1s (-8.1%)
  10. x86_64-gnu-llvm-21-3: 5881.4s -> 6327.7s (+7.6%)
How to interpret the job duration changes?

Job durations can vary a lot, based on the actual runner instance
that executed the job, system noise, invalidated caches, etc. The table above is provided
mostly for t-infra members, for simpler debugging of potential CI slow-downs.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (9415853): comparison URL.

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

@rustbot label: -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.2% [0.0%, 0.2%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.1% [-0.1%, -0.1%] 2
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (secondary -1.5%)

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

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
3.4% [1.6%, 6.7%] 4
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-4.8% [-5.4%, -4.2%] 6
All ❌✅ (primary) - - 0

Cycles

Results (secondary 0.4%)

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

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
4.8% [4.7%, 5.0%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.5% [-2.5%, -2.4%] 3
All ❌✅ (primary) - - 0

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 473.581s -> 470.507s (-0.65%)
Artifact size: 383.57 MiB -> 383.59 MiB (0.01%)

@eggyal eggyal deleted the unused-assignment-to-unused-variable branch January 25, 2026 17:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged-by-bors This PR was explicitly merged by bors. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unexpected warning about unused assignment in Rust 1.92 and newer

5 participants