From 81bfb0c181298259215fc0f77c11f6d7c0dbfb85 Mon Sep 17 00:00:00 2001 From: lengyijun Date: Thu, 7 Nov 2024 17:16:40 +0800 Subject: [PATCH] only check `main.rs` and `lib.rs` --- .../src/use_crate_prefix_for_self_imports.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/use_crate_prefix_for_self_imports.rs b/clippy_lints/src/use_crate_prefix_for_self_imports.rs index 5cefd0abbb37..d525a1cdc903 100644 --- a/clippy_lints/src/use_crate_prefix_for_self_imports.rs +++ b/clippy_lints/src/use_crate_prefix_for_self_imports.rs @@ -4,8 +4,9 @@ use def_id::LOCAL_CRATE; use rustc_errors::Applicability; use rustc_hir::def::Res; use rustc_hir::*; -use rustc_lint::{LateContext, LateLintPass}; +use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_session::declare_lint_pass; +use rustc_span::{FileName, RealFileName}; declare_clippy_lint! { /// ### What it does @@ -53,6 +54,17 @@ declare_lint_pass!(UseCratePrefixForSelfImports => [USE_CRATE_PREFIX_FOR_SELF_IM 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