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

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

Open
KmolYuan opened this issue Nov 29, 2024 · 0 comments
Open

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

KmolYuan opened this issue Nov 29, 2024 · 0 comments
Labels
A-lint Area: New lints

Comments

@KmolYuan
Copy link

KmolYuan commented Nov 29, 2024

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();
@KmolYuan KmolYuan added the A-lint Area: New lints label Nov 29, 2024
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

1 participant