Skip to content

fix: Removing all unused imports removes used imports for imports used for Derive macros #19793

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Hmikihiro
Copy link
Contributor

@Hmikihiro Hmikihiro commented May 14, 2025

fix: #19119

2025-05-15.1.05.49.mov

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 14, 2025
Comment on lines -1638 to +1669
if prefer_value_ns { values().or_else(types) } else { types().or_else(values) }
.or_else(items)
.or_else(macros)
if resolve_per_ns {
PathResolutionPerNs {
type_ns: types().or_else(items),
value_ns: values(),
macro_ns: macros(),
}
} else {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I resolve type per namespace and make Struct for keep with split.

Comment on lines +1216 to +1234
pub(crate) fn resolve_hir_path_per_ns(
&self,
db: &dyn HirDatabase,
path: &ast::Path,
) -> Option<PathResolutionPerNs> {
let mut collector = ExprCollector::new(db, self.resolver.module(), self.file_id);
let hir_path =
collector.lower_path(path.clone(), &mut ExprCollector::impl_trait_error_allocator)?;
let store = collector.store.finish();
Some(resolve_hir_path_(
db,
&self.resolver,
&hir_path,
false,
name_hygiene(db, InFile::new(self.file_id, path.syntax())),
Some(&store),
true,
))
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When we check unused import, we check only tail of path.

use foo::bar;
         ^^^

So I check only by resolve_hir_path_ instead of resolve_path

Copy link
Contributor

@ChayimFriedman2 ChayimFriedman2 left a comment

Choose a reason for hiding this comment

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

The changes look good, I propose a few minor changes.

) -> Self {
PathResolutionPerNs { type_ns, value_ns, macro_ns }
}
pub fn take_path(&self) -> Option<PathResolution> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
pub fn take_path(&self) -> Option<PathResolution> {
pub fn any(&self) -> Option<PathResolution> {

take methods usually modify the object (e.g. Option::take()), I propose to call this any().

{
return Some(u);
match res {
PathResolutionPerNs { type_ns: Some(type_ns), .. } if u.star_token().is_some() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the condition needs to be put differently: if u.star_token().is_some(), then extract the module from it or return None if it isn't a module. This is because if we don't have a type ns resolution but it's a glob import, it makes little sense to handle it like value/macro ns.

{
return Some(u);
PathResolutionPerNs {
type_ns: Some(PathResolution::Def(ModuleDef::Trait(ref t))),
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's better to handle traits in is_path_unused_in_scope() specifically and not duplicate the arm here.

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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Removing all unused imports removes used imports for imports used for Derive macros
3 participants