-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Merged
ChayimFriedman2
merged 4 commits into
rust-lang:master
from
Hmikihiro:unused_import_conlict_derive
May 15, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,9 @@ use std::iter::{self, once}; | |
use crate::{ | ||
Adt, AssocItem, BindingMode, BuiltinAttr, BuiltinType, Callable, Const, DeriveHelper, Field, | ||
Function, GenericSubstitution, Local, Macro, ModuleDef, Static, Struct, ToolModule, Trait, | ||
TraitAlias, TupleField, Type, TypeAlias, Variant, db::HirDatabase, semantics::PathResolution, | ||
TraitAlias, TupleField, Type, TypeAlias, Variant, | ||
db::HirDatabase, | ||
semantics::{PathResolution, PathResolutionPerNs}, | ||
}; | ||
use either::Either; | ||
use hir_def::{ | ||
|
@@ -1159,7 +1161,9 @@ impl<'db> SourceAnalyzer<'db> { | |
prefer_value_ns, | ||
name_hygiene(db, InFile::new(self.file_id, path.syntax())), | ||
Some(&store), | ||
)?; | ||
false, | ||
) | ||
.any()?; | ||
let subst = (|| { | ||
let parent = parent()?; | ||
let ty = if let Some(expr) = ast::Expr::cast(parent.clone()) { | ||
|
@@ -1209,6 +1213,26 @@ impl<'db> SourceAnalyzer<'db> { | |
} | ||
} | ||
|
||
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, | ||
)) | ||
} | ||
|
||
pub(crate) fn record_literal_missing_fields( | ||
&self, | ||
db: &'db dyn HirDatabase, | ||
|
@@ -1532,7 +1556,7 @@ pub(crate) fn resolve_hir_path( | |
hygiene: HygieneId, | ||
store: Option<&ExpressionStore>, | ||
) -> Option<PathResolution> { | ||
resolve_hir_path_(db, resolver, path, false, hygiene, store) | ||
resolve_hir_path_(db, resolver, path, false, hygiene, store, false).any() | ||
} | ||
|
||
#[inline] | ||
|
@@ -1554,7 +1578,8 @@ fn resolve_hir_path_( | |
prefer_value_ns: bool, | ||
hygiene: HygieneId, | ||
store: Option<&ExpressionStore>, | ||
) -> Option<PathResolution> { | ||
resolve_per_ns: bool, | ||
) -> PathResolutionPerNs { | ||
let types = || { | ||
let (ty, unresolved) = match path.type_anchor() { | ||
Some(type_ref) => resolver.generic_def().and_then(|def| { | ||
|
@@ -1635,9 +1660,31 @@ fn resolve_hir_path_( | |
.map(|(def, _)| PathResolution::Def(ModuleDef::Macro(def.into()))) | ||
}; | ||
|
||
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 { | ||
Comment on lines
-1638
to
+1669
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I resolve type per namespace and make Struct for keep with split. |
||
let res = if prefer_value_ns { | ||
values() | ||
.map(|value_ns| PathResolutionPerNs::new(None, Some(value_ns), None)) | ||
.unwrap_or_else(|| PathResolutionPerNs::new(types(), None, None)) | ||
} else { | ||
types() | ||
.map(|type_ns| PathResolutionPerNs::new(Some(type_ns), None, None)) | ||
.unwrap_or_else(|| PathResolutionPerNs::new(None, values(), None)) | ||
}; | ||
|
||
if res.any().is_some() { | ||
res | ||
} else if let Some(type_ns) = items() { | ||
PathResolutionPerNs::new(Some(type_ns), None, None) | ||
} else { | ||
PathResolutionPerNs::new(None, None, macros()) | ||
} | ||
} | ||
} | ||
|
||
fn resolve_hir_value_path( | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
So I check only by
resolve_hir_path_
instead ofresolve_path