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

Add lint against unqualified local imports #13418

Closed
RalfJung opened this issue Sep 19, 2024 · 4 comments
Closed

Add lint against unqualified local imports #13418

RalfJung opened this issue Sep 19, 2024 · 4 comments
Labels
A-lint Area: New lints

Comments

@RalfJung
Copy link
Member

What it does

This lint is intended to help with rust-lang/rustfmt#4709: when using the unstable group_imports = "StdExternalCrate" rustfmt setting (as rustc does now), imports like use local_module::name; are grouped with the "external" imports since, syntactically, they cannot be distinguished. I think it'd be useful to have a lint against that case, making us add an explicit self:: prefix. That makes it easier for rustfmt and the human reader to immediately see that this is a local import.

I implemented this in rust-lang/rust#125645 but I am stuck in "not finding any reviewer hell", so I am suggesting to make this a clippy lint instead which hopefully is less controversial. So, would this lint be acceptable for clippy?

Advantage

  • Make it syntactically clear which imports are external vs local, both for rustfmt and human readers.

Drawbacks

  • This feels a bit like going back to 2015 edition imports, maybe?

Example

mod localmod {
    pub struct S;
    pub struct T;
}

// Not a local import, so no lint.
use std::cell::Cell;

// Implicitly local import, gets lint.
use localmod::S;

Could be written as:

mod localmod {
    pub struct S;
    pub struct T;
}

use std::cell::Cell;

use self::localmod::S;
@RalfJung RalfJung added the A-lint Area: New lints label Sep 19, 2024
@lolbinarycat
Copy link

perhaps there should be an option to always use absolute crate::mod imports? that's the style i use in a lot of my projects, since it makes copy-pasting imports from one module to another easy.

@SpriteOvO
Copy link
Contributor

May I claim it to start a PR or are we waiting for a discussion to see if it is acceptable?

@RalfJung
Copy link
Member Author

rust-lang/rust#125645 has now gotten a review, so maybe we can have it in rustc after all.

perhaps there should be an option to always use absolute crate::mod imports? that's the style i use in a lot of my projects, since it makes copy-pasting imports from one module to another easy.

That's also a possible style but not the one I am looking to enforce here.

@RalfJung
Copy link
Member Author

Seems like the rustc PR will get landed after all, so I will close this. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

3 participants