-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
new lint: use_crate_prefix_for_self_imports #13662
Open
lengyijun
wants to merge
4
commits into
rust-lang:master
Choose a base branch
from
lengyijun:last_use_crate_prefix_for_self_imports
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+143
−7
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::snippet_opt; | ||
use def_id::LOCAL_CRATE; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::def::Res; | ||
use rustc_hir::{Item, ItemKind, def_id}; | ||
use rustc_lint::{LateContext, LateLintPass, LintContext}; | ||
use rustc_session::declare_lint_pass; | ||
use rustc_span::{FileName, RealFileName}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// This lint checks for imports from the current crate that do not use the `crate::` prefix. | ||
/// It suggests using `crate::` to make it clear that the item is from the same crate. | ||
/// | ||
/// ### Why is this bad? | ||
/// When imports from the current crate lack the `crate::` prefix, it can make the code less readable | ||
/// because it’s not immediately clear if the imported item is from the current crate or an external dependency. | ||
/// Using `crate::` for self-imports provides a consistent style, making the origin of each import clear. | ||
/// This helps reduce confusion and maintain a uniform codebase. | ||
/// | ||
/// ### Example | ||
/// ```rust,ignore | ||
/// // lib.rs | ||
/// mod foo; | ||
/// use foo::bar; | ||
/// ``` | ||
/// | ||
/// ```rust,ignore | ||
/// // foo.rs | ||
/// #[path = "./foo.rs"] | ||
/// pub fn bar() {} | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```rust,ignore | ||
/// // lib.rs | ||
/// mod foo; | ||
/// use crate::foo::bar; | ||
/// ``` | ||
/// | ||
/// ```rust,ignore | ||
/// // foo.rs | ||
/// #[path = "./foo.rs"] | ||
/// pub fn bar() {} | ||
/// ``` | ||
#[clippy::version = "1.84.0"] | ||
pub USE_CRATE_PREFIX_FOR_SELF_IMPORTS, | ||
style, | ||
"checks that imports from the current crate use the `crate::` prefix" | ||
} | ||
|
||
declare_lint_pass!(UseCratePrefixForSelfImports => [USE_CRATE_PREFIX_FOR_SELF_IMPORTS]); | ||
|
||
impl LateLintPass<'_> for UseCratePrefixForSelfImports { | ||
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) { | ||
let FileName::Real(RealFileName::LocalPath(p)) = cx.sess().source_map().span_to_filename(item.span) else { | ||
return; | ||
}; | ||
let Some(file_name) = p.file_name() else { | ||
return; | ||
}; | ||
// only check `main.rs` and `lib.rs` | ||
if !(file_name == "main.rs" || file_name == "lib.rs") { | ||
return; | ||
} | ||
|
||
if let ItemKind::Use(use_path, _) = &item.kind { | ||
if let Some(segment) = use_path.segments.first() | ||
&& let Res::Def(_, def_id) = segment.res | ||
&& def_id.krate == LOCAL_CRATE | ||
{ | ||
let root = segment.ident.name; | ||
if root != rustc_span::symbol::kw::Crate && root != rustc_span::symbol::kw::Super { | ||
span_lint_and_sugg( | ||
cx, | ||
USE_CRATE_PREFIX_FOR_SELF_IMPORTS, | ||
segment.ident.span, | ||
"this import is not clear", | ||
"prefix with `crate::`", | ||
format!("crate::{}", snippet_opt(cx, segment.ident.span).unwrap()), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
tests/ui-cargo/use_crate_prefix_for_self_imports/fail/Cargo.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
error: this import is not clear | ||
--> src/main.rs:1:5 | ||
| | ||
1 | use foo::Foo; | ||
| ^^^ help: prefix with `crate::`: `crate::foo` | ||
| | ||
= note: `-D clippy::use-crate-prefix-for-self-imports` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::use_crate_prefix_for_self_imports)]` | ||
|
||
error: could not compile `fail` (bin "fail") due to 1 previous error |
7 changes: 7 additions & 0 deletions
7
tests/ui-cargo/use_crate_prefix_for_self_imports/fail/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "fail" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] |
1 change: 1 addition & 0 deletions
1
tests/ui-cargo/use_crate_prefix_for_self_imports/fail/src/foo.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub struct Foo; |
7 changes: 7 additions & 0 deletions
7
tests/ui-cargo/use_crate_prefix_for_self_imports/fail/src/main.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use foo::Foo; | ||
|
||
mod foo; | ||
|
||
fn main() { | ||
let _foo = Foo; | ||
} |
7 changes: 7 additions & 0 deletions
7
tests/ui-cargo/use_crate_prefix_for_self_imports/pass/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "pass" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] |
1 change: 1 addition & 0 deletions
1
tests/ui-cargo/use_crate_prefix_for_self_imports/pass/src/foo.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub struct Foo; |
7 changes: 7 additions & 0 deletions
7
tests/ui-cargo/use_crate_prefix_for_self_imports/pass/src/main.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use crate::foo::Foo; | ||
|
||
mod foo; | ||
|
||
fn main() { | ||
let _foo = Foo; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#![warn(clippy::use_crate_prefix_for_self_imports)] | ||
|
||
fn main() { | ||
// test code goes here | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I understand that clearing up possible path confusion is a worthy cause, I'm afraid that introducing the lint as warn-by-default will lead to a lot of noise and broken CI jobs.
I'll discuss this on zulip.