Skip to content

Add lint against unqualified local imports #13418

@RalfJung

Description

@RalfJung

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;

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions