close
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
Copy link
Copy Markdown
Contributor

@onehr onehr commented May 25, 2026

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
Copy link
Copy Markdown
Collaborator

rustbot commented May 25, 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. 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
Copy link
Copy Markdown
Collaborator

rustbot commented May 25, 2026

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

Copy link
Copy Markdown
Contributor

@mejrs mejrs left a comment

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
Copy link
Copy Markdown
Collaborator

rustbot commented May 26, 2026

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
Copy link
Copy Markdown
Contributor Author

onehr commented May 26, 2026

Thanks @mejrs for your comments, resolved.

@onehr
Copy link
Copy Markdown
Contributor Author

onehr commented May 26, 2026

@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 on lines +236 to +293
fn pattern_item_path(tcx: TyCtxt<'_>, body_def_id: DefId, item_def_id: DefId) -> String {
if is_nested_in(tcx, item_def_id, body_def_id) {
return relative_item_path(tcx, body_def_id, item_def_id);
}

let body_mod = nearest_parent_module(tcx, body_def_id);
if body_mod == nearest_parent_module(tcx, item_def_id) {
relative_item_path(tcx, body_mod, item_def_id)
} else if item_def_id.is_local() && !body_mod.is_top_level_module() {
format!("{}::{}", kw::Crate, with_no_trimmed_paths!(tcx.def_path_str(item_def_id)))
} else {
with_no_trimmed_paths!(tcx.def_path_str(item_def_id))
}
}

fn is_nested_in(tcx: TyCtxt<'_>, mut def_id: DefId, parent_def_id: DefId) -> bool {
while let Some(parent) = tcx.opt_parent(def_id) {
if parent == parent_def_id {
return true;
}
def_id = parent;
}

false
}

fn relative_item_path(tcx: TyCtxt<'_>, parent_def_id: DefId, item_def_id: DefId) -> String {
let mut def_id = item_def_id;
let mut path = Vec::new();

loop {
if def_id == parent_def_id {
break;
}
if let Some(name) = tcx.opt_item_name(def_id) {
path.push(name.to_string());
}
let Some(parent) = tcx.opt_parent(def_id) else {
return with_no_trimmed_paths!(tcx.def_path_str(item_def_id));
};
def_id = parent;
}

path.reverse();
path.join("::")
}

fn nearest_parent_module(tcx: TyCtxt<'_>, mut def_id: DefId) -> DefId {
while let Some(parent) = tcx.opt_parent(def_id) {
def_id = parent;
if matches!(tcx.def_kind(def_id), DefKind::Mod) {
return def_id;
}
}

def_id
}

Copy link
Copy Markdown
Contributor

@cjgillot cjgillot May 30, 2026

Choose a reason for hiding this comment

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

Please use an existing printing mode from rustc_middle::ty::print. No need to create our own here.

View changes since the review

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

5 participants