Skip to content

Commit

Permalink
only check main.rs and lib.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
lengyijun committed Nov 13, 2024
1 parent 6910703 commit 81bfb0c
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion clippy_lints/src/use_crate_prefix_for_self_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 81bfb0c

Please sign in to comment.