Skip to content

Commit 00b7c63

Browse files
committed
wip
doctest
1 parent ac805d4 commit 00b7c63

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
@@ -6212,6 +6212,7 @@ Released 2018-09-13
62126212
[`unwrap_or_else_default`]: https://rust-lang.github.io/rust-clippy/master/index.html#unwrap_or_else_default
62136213
[`unwrap_used`]: https://rust-lang.github.io/rust-clippy/master/index.html#unwrap_used
62146214
[`upper_case_acronyms`]: https://rust-lang.github.io/rust-clippy/master/index.html#upper_case_acronyms
6215+
[`use_crate_prefix_for_self_imports`]: https://rust-lang.github.io/rust-clippy/master/index.html#use_crate_prefix_for_self_imports
62156216
[`use_debug`]: https://rust-lang.github.io/rust-clippy/master/index.html#use_debug
62166217
[`use_self`]: https://rust-lang.github.io/rust-clippy/master/index.html#use_self
62176218
[`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
@@ -776,6 +776,7 @@ pub static LINTS: &[&crate::LintInfo] = &[
776776
crate::unwrap::UNNECESSARY_UNWRAP_INFO,
777777
crate::unwrap_in_result::UNWRAP_IN_RESULT_INFO,
778778
crate::upper_case_acronyms::UPPER_CASE_ACRONYMS_INFO,
779+
crate::use_crate_prefix_for_self_imports::USE_CRATE_PREFIX_FOR_SELF_IMPORTS_INFO,
779780
crate::use_self::USE_SELF_INFO,
780781
crate::useless_conversion::USELESS_CONVERSION_INFO,
781782
crate::vec::USELESS_VEC_INFO,

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ mod unused_unit;
390390
mod unwrap;
391391
mod unwrap_in_result;
392392
mod upper_case_acronyms;
393+
mod use_crate_prefix_for_self_imports;
393394
mod use_self;
394395
mod useless_conversion;
395396
mod vec;
@@ -976,5 +977,6 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
976977
store.register_late_pass(|_| Box::new(unneeded_struct_pattern::UnneededStructPattern));
977978
store.register_late_pass(|_| Box::<unnecessary_semicolon::UnnecessarySemicolon>::default());
978979
store.register_late_pass(move |_| Box::new(non_std_lazy_statics::NonStdLazyStatic::new(conf)));
980+
store.register_late_pass(|_| Box::new(use_crate_prefix_for_self_imports::UseCratePrefixForSelfImports));
979981
// add lints here, do not remove this comment, it's used in `new_lint`
980982
}
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;

0 commit comments

Comments
 (0)