Skip to content

Fix unused_variables typo suggestions for const patterns#156904

Open
onehr wants to merge 1 commit into
rust-lang:mainfrom
onehr:fix-unused-pattern-typo-147595
Open

Fix unused_variables typo suggestions for const patterns#156904
onehr wants to merge 1 commit into
rust-lang:mainfrom
onehr:fix-unused-pattern-typo-147595

Conversation

@onehr

@onehr onehr commented May 25, 2026

Copy link
Copy Markdown
Contributor

Closes #147595

This fixes unused_variables typo suggestions that could print replacement
patterns which are not valid from the binding site.

The fix keeps the existing def_path_str output when it is already valid, but
adjusts two local-item cases:

  • items outside the binding module are printed with crate::...;
  • items nested inside the current body are printed relative to that body.

It also suppresses const typo suggestions for bare let x = ... bindings,
because replacing the binding with a const path creates a refutable pattern.
Unit ADT typo suggestions are left unchanged.

The regression test covers 2015 and 2021 editions, cross-module consts,
same-module consts, function-local consts, cross-module variants, and bare
let const initializers.

@rustbot rustbot added 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. labels May 25, 2026
@onehr onehr marked this pull request as ready for review May 25, 2026 09:59
@rustbot

rustbot commented May 25, 2026

Copy link
Copy Markdown
Collaborator

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. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 25, 2026
@rustbot

rustbot commented May 25, 2026

Copy link
Copy Markdown
Collaborator

r? @mejrs

rustbot has assigned @mejrs.
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 73 candidates
  • Random selection from 18 candidates

@mejrs mejrs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks!

  • Please add a regression test for not suggesting path::_, like reported in the issue.
  • Please add explicit HELP annotations in the new tests.

It looks good otherwise but I'm not familiar enough here to tell whether or not corner cases are being missed, so

r? @fmease or @estebank

View changes since this review

@rustbot rustbot assigned fmease and unassigned mejrs May 26, 2026
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 26, 2026
@rustbot

rustbot commented May 26, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@onehr onehr force-pushed the fix-unused-pattern-typo-147595 branch from b6a1b9e to 518355f Compare May 26, 2026 17:15
@onehr

onehr commented May 26, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @mejrs for your comments, resolved.

@onehr

onehr commented May 26, 2026

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 26, 2026
Comment thread compiler/rustc_mir_transform/src/liveness.rs
@onehr onehr force-pushed the fix-unused-pattern-typo-147595 branch 2 times, most recently from 2de8a14 to 537deff Compare May 31, 2026 15:42
Comment thread compiler/rustc_mir_transform/src/liveness.rs Outdated
@onehr onehr force-pushed the fix-unused-pattern-typo-147595 branch from 537deff to 189de55 Compare June 7, 2026 02:48
@rust-log-analyzer

This comment has been minimized.

When `unused_variables` finds a similarly named const or enum variant, it
suggests replacing the unused binding with a pattern matching that item. The
item was printed with a trimmed path, which is not necessarily resolvable from
the binding site, so the `MachineApplicable` suggestion could fail to compile.

Print the item with `with_crate_prefix!`: a `crate::`-rooted, re-export-aware
path that resolves from any binding site in the crate. A const declared inside
the current body has no such path, so it is printed by its bare in-scope name.

Also suppress const typo suggestions for bare `let x = ...` bindings, where a
const pattern would be refutable.
@onehr onehr force-pushed the fix-unused-pattern-typo-147595 branch from 189de55 to 5632423 Compare June 7, 2026 06:14
@rustbot

rustbot commented Jun 7, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rust-bors

rust-bors Bot commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #157894) made this pull request unmergeable. Please resolve the merge conflicts.

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

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lint unused_variables's "typoed pattern" suggestion still suggests invalid and unrelated paths

6 participants