Skip to content

Commit 65043bf

Browse files
committed
wip
1 parent 6400f00 commit 65043bf

File tree

1 file changed

+50
-44
lines changed

1 file changed

+50
-44
lines changed

clippy_lints/src/use_crate_prefix_for_self_imports.rs

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::source::SpanRangeExt;
3+
use clippy_utils::tokenize_with_text;
24
use def_id::LOCAL_CRATE;
35
use rustc_data_structures::fx::FxHashSet;
46
use rustc_errors::Applicability;
@@ -56,8 +58,7 @@ pub struct UseCratePrefixForSelfImports<'a, 'tcx> {
5658
use_block: Vec<&'a UsePath<'tcx>>,
5759
/// collect `mod` in current block
5860
mod_names: FxHashSet<Symbol>,
59-
/// spans of `mod`, `use`, and attributes
60-
spans: Vec<Span>,
61+
latest_span: Option<Span>,
6162
}
6263

6364
impl_lint_pass!(UseCratePrefixForSelfImports<'_, '_> => [USE_CRATE_PREFIX_FOR_SELF_IMPORTS]);
@@ -77,41 +78,12 @@ impl<'a, 'tcx> LateLintPass<'tcx> for UseCratePrefixForSelfImports<'a, 'tcx> {
7778
return;
7879
}
7980

80-
if self.in_same_block(item.span) {
81-
self.insert_item(item);
82-
} else {
83-
self.try_lint(cx);
84-
self.clear();
85-
self.insert_item(item);
86-
}
87-
}
88-
89-
fn check_attribute(&mut self, cx: &LateContext<'tcx>, attribute: &'a Attribute) {
90-
let FileName::Real(RealFileName::LocalPath(p)) = cx.sess().source_map().span_to_filename(attribute.span())
91-
else {
92-
self.clear();
93-
return;
94-
};
95-
let Some(file_name) = p.file_name() else {
96-
self.clear();
97-
return;
98-
};
99-
// only check `main.rs` and `lib.rs`
100-
if !(file_name == "main.rs" || file_name == "lib.rs") {
101-
return;
102-
}
103-
104-
if self.in_same_block(attribute.span()) {
105-
self.spans.push(attribute.span());
106-
} else {
107-
self.try_lint(cx);
108-
self.clear();
109-
self.spans.push(attribute.span());
110-
}
81+
self.insert_item(cx, item);
11182
}
11283
}
11384

11485
impl<'tcx> UseCratePrefixForSelfImports<'_, 'tcx> {
86+
/*
11587
fn in_same_block(&self, span: Span) -> bool {
11688
if self.spans.is_empty() {
11789
return true;
@@ -124,19 +96,53 @@ impl<'tcx> UseCratePrefixForSelfImports<'_, 'tcx> {
12496
}
12597
false
12698
}
99+
*/
127100

128-
fn insert_item(&mut self, item: &Item<'tcx>) {
129-
match item.kind {
130-
ItemKind::Mod(ident, _) => {
131-
self.spans.push(item.span);
132-
self.mod_names.insert(ident.name);
133-
},
134-
ItemKind::Use(use_tree, _) => {
135-
self.spans.push(item.span);
136-
self.use_block.push(use_tree);
101+
fn in_same_block(&self, cx: &LateContext<'tcx>, span: Span) -> bool {
102+
match self.latest_span {
103+
Some(latest_span) => {
104+
let gap_span = latest_span.between(span);
105+
let gap_snippet = gap_span.get_source_text(cx).unwrap();
106+
for (token, source, inner_span) in tokenize_with_text(&gap_snippet) {
107+
match token {
108+
rustc_lexer::TokenKind::Whitespace => return false,
109+
_ => {},
110+
}
111+
}
112+
true
137113
},
138-
_ => {},
114+
None => true,
115+
}
116+
}
117+
118+
fn insert_item(&mut self, cx: &LateContext<'tcx>, item: &Item<'tcx>) {
119+
if self.in_same_block(cx, item.span) {
120+
match item.kind {
121+
ItemKind::Mod(ident, _) => {
122+
self.mod_names.insert(ident.name);
123+
},
124+
ItemKind::Use(use_tree, _) => {
125+
self.use_block.push(use_tree);
126+
},
127+
_ => {},
128+
}
129+
} else {
130+
self.try_lint(cx);
131+
self.clear();
132+
match item.kind {
133+
ItemKind::Mod(ident, _) => {
134+
self.mod_names.insert(ident.name);
135+
},
136+
ItemKind::Use(use_tree, _) => {
137+
self.use_block.push(use_tree);
138+
},
139+
_ => {},
140+
}
139141
}
142+
self.latest_span = match self.latest_span {
143+
Some(span) => Some(span.with_hi(item.span.hi())),
144+
None => Some(item.span),
145+
};
140146
}
141147

142148
fn try_lint(&self, cx: &LateContext<'tcx>) {
@@ -164,6 +170,6 @@ impl<'tcx> UseCratePrefixForSelfImports<'_, 'tcx> {
164170
fn clear(&mut self) {
165171
self.use_block.clear();
166172
self.mod_names.clear();
167-
self.spans.clear();
173+
// self.spans.clear();
168174
}
169175
}

0 commit comments

Comments
 (0)