Skip to content
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

handle {self} when removing unused imports #17140

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

Conversation

harrysarson
Copy link

Fixes #17139

On master

mod inner {
    pub struct X();
    pub struct Y();
}

mod z {
    use super::inner::{self, X}$0;

    fn f() {
        let y = inner::Y();
    }
}

becomes

mod inner {
    pub struct X();
    pub struct Y();
}

mod z {
    use super::inner:self;

    fn f() {
        let y = inner::Y();
    }
}

with this fix it instead becomes


```rs
mod inner {
    pub struct X();
    pub struct Y();
}

mod z {
    use super::inner;

    fn f() {
        let y = inner::Y();
    }
}

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 25, 2024
@Veykril
Copy link
Member

Veykril commented Apr 26, 2024

That transform is generally not valid, use foo::bar::{self} imports only the type namespace, where as use foo::bar; imports value, type and macro namespace. So transforming like that can actually cause name collisions.

Now there is still a bug here in that we produce malformed syntax but we need to keep the braces.

@@ -376,11 +376,24 @@ impl ast::UseTreeList {
.filter_map(|it| it.into_token().filter(|it| it.kind() == T![,]))
}

pub fn is_just_self(&self) -> bool {
if let Some((single_subtree,)) = self.use_trees().collect_tuple() {
fn path_is_self(path: &ast::Path) -> bool {
Copy link
Member

Choose a reason for hiding this comment

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

This can be a closure

Copy link
Author

Choose a reason for hiding this comment

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

I looked to make this into a closure, and in the process decided to check the number of use_tree children as part of the same check (so now the closure is called has_single_subtree_that_is_not_self)

@Veykril Veykril 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 Apr 29, 2024
@harrysarson
Copy link
Author

That transform is generally not valid, use foo::bar::{self} imports only the type namespace, where as use foo::bar; imports value, type and macro namespace. So transforming like that can actually cause name collisions.

Now there is still a bug here in that we produce malformed syntax but we need to keep the braces.

Yep. I have removed the second commit, the first commit (which fixes remove_unnecessary_braces) I think is what we need to fix the malformed syntax and keep the braces.

Before this commit `UseTree::remove_unnecessary_braces` removed the braces
around `{self}` in `use x::y::{self};` but `use x::y::self;` is not valid
rust.
@harrysarson
Copy link
Author

harrysarson commented Apr 30, 2024

That transform is generally not valid,

The normalize import IDE assist (https://github.com/rust-lang/rust-analyzer/blob/master/crates/ide-assists/src/handlers/normalize_import.rs) does transform {self}. I.e. the following unit test would pass if I added it to normalize_import.rs

    #[test]
    fn test_collapses_self() {
        check_assist_variations!("std::fmt::{self}", "std::fmt");
    }

Is that a bug?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

removing unused imports generates invalid use module_name::self
3 participants