Skip to content

Commit 5ef46eb

Browse files
committed
wip
doctest
1 parent f712eb5 commit 5ef46eb

File tree

12 files changed

+125
-0
lines changed

12 files changed

+125
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6114,6 +6114,7 @@ Released 2018-09-13
61146114
[`unwrap_or_else_default`]: https://rust-lang.github.io/rust-clippy/master/index.html#unwrap_or_else_default
61156115
[`unwrap_used`]: https://rust-lang.github.io/rust-clippy/master/index.html#unwrap_used
61166116
[`upper_case_acronyms`]: https://rust-lang.github.io/rust-clippy/master/index.html#upper_case_acronyms
6117+
[`use_crate_prefix_for_self_imports`]: https://rust-lang.github.io/rust-clippy/master/index.html#use_crate_prefix_for_self_imports
61176118
[`use_debug`]: https://rust-lang.github.io/rust-clippy/master/index.html#use_debug
61186119
[`use_self`]: https://rust-lang.github.io/rust-clippy/master/index.html#use_self
61196120
[`used_underscore_binding`]: https://rust-lang.github.io/rust-clippy/master/index.html#used_underscore_binding

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,7 @@ pub static LINTS: &[&crate::LintInfo] = &[
764764
crate::unwrap::UNNECESSARY_UNWRAP_INFO,
765765
crate::unwrap_in_result::UNWRAP_IN_RESULT_INFO,
766766
crate::upper_case_acronyms::UPPER_CASE_ACRONYMS_INFO,
767+
crate::use_crate_prefix_for_self_imports::USE_CRATE_PREFIX_FOR_SELF_IMPORTS_INFO,
767768
crate::use_self::USE_SELF_INFO,
768769
crate::useless_conversion::USELESS_CONVERSION_INFO,
769770
crate::vec::USELESS_VEC_INFO,

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ mod unused_unit;
384384
mod unwrap;
385385
mod unwrap_in_result;
386386
mod upper_case_acronyms;
387+
mod use_crate_prefix_for_self_imports;
387388
mod use_self;
388389
mod useless_conversion;
389390
mod vec;
@@ -963,5 +964,6 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
963964
store.register_late_pass(|_| Box::new(manual_ignore_case_cmp::ManualIgnoreCaseCmp));
964965
store.register_late_pass(|_| Box::new(unnecessary_literal_bound::UnnecessaryLiteralBound));
965966
store.register_late_pass(move |_| Box::new(arbitrary_source_item_ordering::ArbitrarySourceItemOrdering::new(conf)));
967+
store.register_late_pass(|_| Box::new(use_crate_prefix_for_self_imports::UseCratePrefixForSelfImports));
966968
// add lints here, do not remove this comment, it's used in `new_lint`
967969
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::source::snippet_opt;
3+
use def_id::LOCAL_CRATE;
4+
use rustc_errors::Applicability;
5+
use rustc_hir::def::Res;
6+
use rustc_hir::*;
7+
use rustc_lint::{LateContext, LateLintPass};
8+
use rustc_session::declare_lint_pass;
9+
10+
declare_clippy_lint! {
11+
/// ### What it does
12+
/// This lint checks for imports from the current crate that do not use the `crate::` prefix.
13+
/// It suggests using `crate::` to make it clear that the item is from the same crate.
14+
///
15+
/// ### Why is this bad?
16+
/// When imports from the current crate lack the `crate::` prefix, it can make the code less readable
17+
/// because it’s not immediately clear if the imported item is from the current crate or an external dependency.
18+
/// Using `crate::` for self-imports provides a consistent style, making the origin of each import clear.
19+
/// This helps reduce confusion and maintain a uniform codebase.
20+
///
21+
/// ### Example
22+
/// ```rust,ignore
23+
/// // lib.rs
24+
/// mod foo;
25+
/// use foo::bar;
26+
/// ```
27+
///
28+
/// ```rust,ignore
29+
/// // foo.rs
30+
/// #[path = "./foo.rs"]
31+
/// pub fn bar() {}
32+
/// ```
33+
///
34+
/// Use instead:
35+
/// ```rust,ignore
36+
/// // lib.rs
37+
/// mod foo;
38+
/// use crate::foo::bar;
39+
/// ```
40+
///
41+
/// ```rust,ignore
42+
/// // foo.rs
43+
/// #[path = "./foo.rs"]
44+
/// pub fn bar() {}
45+
/// ```
46+
#[clippy::version = "1.84.0"]
47+
pub USE_CRATE_PREFIX_FOR_SELF_IMPORTS,
48+
style,
49+
"checks that imports from the current crate use the `crate::` prefix"
50+
}
51+
52+
declare_lint_pass!(UseCratePrefixForSelfImports => [USE_CRATE_PREFIX_FOR_SELF_IMPORTS]);
53+
54+
impl LateLintPass<'_> for UseCratePrefixForSelfImports {
55+
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
56+
if let ItemKind::Use(use_path, _) = &item.kind {
57+
if let Some(segment) = use_path.segments.first()
58+
&& let Res::Def(_, def_id) = segment.res
59+
&& def_id.krate == LOCAL_CRATE
60+
{
61+
let root = segment.ident.name;
62+
if root != rustc_span::symbol::kw::Crate && root != rustc_span::symbol::kw::Super {
63+
span_lint_and_sugg(
64+
cx,
65+
USE_CRATE_PREFIX_FOR_SELF_IMPORTS,
66+
use_path.span,
67+
"this import is not clear",
68+
"prefix with `crate::`",
69+
format!("crate::{}", snippet_opt(cx, use_path.span).unwrap()),
70+
Applicability::MachineApplicable,
71+
);
72+
}
73+
}
74+
}
75+
}
76+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: this import is not clear
2+
--> src/main.rs:1:5
3+
|
4+
1 | use foo::Foo;
5+
| ^^^^^^^^ help: prefix with `crate::`: `crate::foo::Foo`
6+
|
7+
= note: `-D clippy::use-crate-prefix-for-self-imports` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::use_crate_prefix_for_self_imports)]`
9+
10+
error: could not compile `fail` (bin "fail") due to 1 previous error
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "fail"
3+
version = "0.1.0"
4+
edition = "2021"
5+
publish = false
6+
7+
[dependencies]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub struct Foo;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use foo::Foo;
2+
3+
mod foo;
4+
5+
fn main() {
6+
let _foo = Foo;
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "pass"
3+
version = "0.1.0"
4+
edition = "2021"
5+
publish = false
6+
7+
[dependencies]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub struct Foo;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use crate::foo::Foo;
2+
3+
mod foo;
4+
5+
fn main() {
6+
let _foo = Foo;
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#![warn(clippy::use_crate_prefix_for_self_imports)]
2+
3+
fn main() {
4+
// test code goes here
5+
}

0 commit comments

Comments
 (0)