Skip to content

Explicitly add ::/self:: on full path and import path #13750

Open
@KmolYuan

Description

@KmolYuan

What it does

For a path expression in the current module, like

mod foo;
/* ... */

// ambiguous
use foo::run_foo; // import expression
foo::run_foo(); // full path expression

// indicates an external crate
use ::foo::run_foo;
::foo::run_foo();

// indicates a local submodule
use self::foo::run_foo;
self::foo::run_foo();

Ambiguous path may mislead to an external crate (dependency) called foo. With respect to leading :: represents the root path, the root path is explicit in macros, but a leading self:: would be better then use module name directly.

Options

  • "extern" - External crate should use leading ::.
  • "self" - Submodule should use leading self::.
  • "explicit" ("extern"+"self") - Directly use foo::run_foo is not allowed. Only accept ::/self::/crate::/super:: path.

Exception

Splitted path is not limited. (clippy::absolute_paths)
This lint is affect on use statement and the starting path is not imported. (foo::bar but foo is not import)

use ::std::f64::consts;
let x = consts::PI;

Advantage

  • Explicit path to know if a submodule has similar name to the dependencies.
  • Public macros can force to use ::/$crate to avoid wrong imports.

Drawbacks

No response

Example

use foo::run_foo;
foo::run_foo();

mod bar;
use bar::run_bar;
bar::run_bar();

Could be written as:

use ::foo::run_foo;
::foo::run_foo();

mod bar;
use self::bar::run_bar;
self::bar::run_bar();

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