Skip to content

Commit f8a4ec2

Browse files
committed
Make it only lint crate/<crate>
1 parent c709790 commit f8a4ec2

File tree

14 files changed

+167
-174
lines changed

14 files changed

+167
-174
lines changed
Lines changed: 36 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
use clippy_utils::diagnostics::span_lint;
22
use clippy_utils::is_from_proc_macro;
3+
use clippy_utils::source::snippet_opt;
4+
use rustc_data_structures::fx::FxHashSet;
5+
use rustc_hir::def::{DefKind, Res};
6+
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX};
37
use rustc_hir::intravisit::{walk_qpath, Visitor};
4-
use rustc_hir::{def_id::LOCAL_CRATE, HirId, QPath};
8+
use rustc_hir::{HirId, Path};
59
use rustc_lint::{LateContext, LateLintPass};
6-
use rustc_middle::hir::nested_filter::OnlyBodies;
710
use rustc_session::{declare_tool_lint, impl_lint_pass};
8-
use rustc_span::Span;
9-
use std::iter::once;
11+
use rustc_span::symbol::kw;
1012

1113
declare_clippy_lint! {
1214
/// ### What it does
@@ -37,83 +39,50 @@ declare_clippy_lint! {
3739
/// ```
3840
#[clippy::version = "1.72.0"]
3941
pub ABSOLUTE_SYMBOL_PATHS,
40-
style,
42+
restriction,
4143
"checks for usage of a symbol without a `use` statement"
4244
}
4345
impl_lint_pass!(AbsoluteSymbolPaths => [ABSOLUTE_SYMBOL_PATHS]);
4446

4547
pub struct AbsoluteSymbolPaths {
4648
pub absolute_symbol_paths_max_segments: u64,
47-
pub absolute_symbol_paths_allow_std: bool,
49+
pub absolute_symbol_paths_allowed_crates: FxHashSet<String>,
4850
}
4951

5052
impl LateLintPass<'_> for AbsoluteSymbolPaths {
51-
fn check_crate(&mut self, cx: &LateContext<'_>) {
52-
let Self {
53-
absolute_symbol_paths_max_segments,
54-
absolute_symbol_paths_allow_std,
55-
} = *self;
56-
57-
cx.tcx.hir().visit_all_item_likes_in_crate(&mut V {
58-
cx,
59-
absolute_symbol_paths_max_segments,
60-
absolute_symbol_paths_allow_std,
61-
});
62-
}
63-
}
64-
65-
struct V<'a, 'tcx> {
66-
cx: &'a LateContext<'tcx>,
67-
absolute_symbol_paths_max_segments: u64,
68-
absolute_symbol_paths_allow_std: bool,
69-
}
70-
71-
impl<'a, 'tcx> Visitor<'tcx> for V<'a, 'tcx> {
72-
type NestedFilter = OnlyBodies;
73-
74-
fn nested_visit_map(&mut self) -> Self::Map {
75-
self.cx.tcx.hir()
76-
}
77-
53+
// We should only lint `QPath::Resolved`s, but since `Path` is only used in `Resolved` (Please
54+
// confirm this), we don't need to use a visitor or anything
7855
#[expect(clippy::cast_possible_truncation)]
79-
fn visit_qpath(&mut self, qpath: &'tcx QPath<'tcx>, hir_id: HirId, span: Span) {
56+
fn check_path(&mut self, cx: &LateContext<'_>, path: &Path<'_>, _: HirId) {
8057
let Self {
81-
cx,
8258
absolute_symbol_paths_max_segments,
83-
absolute_symbol_paths_allow_std,
84-
} = *self;
59+
absolute_symbol_paths_allowed_crates,
60+
} = self;
8561

86-
if !span.from_expansion()
87-
&& let QPath::Resolved(_, path) = qpath
88-
&& let Some(def_id) = path.res.opt_def_id()
89-
&& let def_path = cx.tcx.def_path(def_id)
90-
&& let crate_name = cx.tcx.crate_name(def_path.krate)
91-
&& let segments = once(crate_name)
92-
.chain(def_path.data.iter().filter_map(|segment| segment.data.get_opt_name()))
93-
&& path.segments.len() > absolute_symbol_paths_max_segments as usize
94-
&& let is_std = matches!(crate_name.as_str(), "alloc" | "core" | "std")
95-
&& !(absolute_symbol_paths_allow_std && is_std)
96-
// If this is local, keep the crate name, if it is not, remove it, as we don't need to
97-
// check that (and results in FNs on reexports from one crate to another)
98-
//
99-
// `def_path_str` handles this for us, but unfortunately requires converting every
100-
// `Symbol` to a `&str`, which is a very slow operation.
101-
&& segments.skip(1).eq(
102-
path.segments.iter()
103-
// I love clippy :)
104-
.skip(usize::from(def_path.krate != LOCAL_CRATE))
105-
.map(|segment| segment.ident.name)
106-
)
107-
&& !is_from_proc_macro(cx, qpath)
62+
if !path.span.from_expansion()
63+
&& path.segments.len() > *absolute_symbol_paths_max_segments as usize
64+
&& let [first, ..] = path.segments
65+
&& let Some(first_snippet) = snippet_opt(cx, first.ident.span)
66+
&& first_snippet == first.ident.as_str()
10867
{
109-
span_lint(
110-
cx,
111-
ABSOLUTE_SYMBOL_PATHS,
112-
span,
113-
"consider referring to this symbol by adding a `use` statement for consistent formatting",
114-
);
115-
}
68+
let is_abs_external =
69+
matches!(first.res, Res::Def(DefKind::Mod, DefId { index, .. }) if index == CRATE_DEF_INDEX);
70+
let is_abs_crate = first.ident.name == kw::Crate;
71+
72+
if is_abs_external && absolute_symbol_paths_allowed_crates.contains(first.ident.name.as_str())
73+
|| is_abs_crate && absolute_symbol_paths_allowed_crates.contains("crate")
74+
{
75+
return;
76+
}
11677

117-
walk_qpath(self, qpath, hir_id);
78+
if is_abs_external || is_abs_crate {
79+
span_lint(
80+
cx,
81+
ABSOLUTE_SYMBOL_PATHS,
82+
path.span,
83+
"consider referring to this symbol by adding a `use` statement for consistent formatting",
84+
);
85+
}
86+
}
11887
}
11988
}

clippy_lints/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,11 +1086,11 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10861086
store.register_late_pass(move |_| Box::new(tuple_array_conversions::TupleArrayConversions { msrv: msrv() }));
10871087
store.register_late_pass(|_| Box::new(manual_float_methods::ManualFloatMethods));
10881088
let absolute_symbol_paths_max_segments = conf.absolute_symbol_paths_max_segments;
1089-
let absolute_symbol_paths_allow_std = conf.absolute_symbol_paths_allow_std;
1089+
let absolute_symbol_paths_allowed_crates = conf.absolute_symbol_paths_allowed_crates.clone();
10901090
store.register_late_pass(move |_| {
10911091
Box::new(absolute_symbol_paths::AbsoluteSymbolPaths {
10921092
absolute_symbol_paths_max_segments,
1093-
absolute_symbol_paths_allow_std,
1093+
absolute_symbol_paths_allowed_crates: absolute_symbol_paths_allowed_crates.clone(),
10941094
})
10951095
});
10961096
// add lints here, do not remove this comment, it's used in `new_lint`

clippy_lints/src/utils/conf.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,8 +557,9 @@ define_Conf! {
557557
(absolute_symbol_paths_max_segments: u64 = 3),
558558
/// Lint: ABSOLUTE_SYMBOL_PATHS.
559559
///
560-
/// Whether to allow paths originating from `core`/`std`/`alloc`
561-
(absolute_symbol_paths_allow_std: bool = false),
560+
/// Which crates to allow absolute symbols, `crate` will allow the local crate
561+
(absolute_symbol_paths_allowed_crates: rustc_data_structures::fx::FxHashSet<String> =
562+
rustc_data_structures::fx::FxHashSet::default()),
562563
}
563564

564565
/// Search for the configuration file.

clippy_utils/src/check_proc_macro.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ fn attr_search_pat(attr: &Attribute) -> (Pat, Pat) {
304304
},
305305
AttrKind::DocComment(_kind @ CommentKind::Line, ..) => {
306306
if matches!(attr.style, AttrStyle::Outer) {
307-
(Pat::Str("///*"), Pat::Str(""))
307+
(Pat::Str("///"), Pat::Str(""))
308308
} else {
309309
(Pat::Str("//!"), Pat::Str(""))
310310
}

clippy_utils/src/usage.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ pub fn contains_return_break_continue_macro(expression: &Expr<'_>) -> bool {
155155
}
156156

157157
pub fn local_used_after_expr(cx: &LateContext<'_>, local_id: HirId, after: &Expr<'_>) -> bool {
158-
let Some(block) = utils::get_enclosing_block(cx, local_id) else { return false };
158+
let Some(block) = utils::get_enclosing_block(cx, local_id) else {
159+
return false;
160+
};
159161

160162
// for _ in 1..3 {
161163
// local
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: consider referring to this symbol by adding a `use` statement for consistent formatting
2+
--> $DIR/absolute_symbol_paths.rs:40:5
3+
|
4+
LL | std::f32::MAX;
5+
| ^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::absolute-symbol-paths` implied by `-D warnings`
8+
9+
error: consider referring to this symbol by adding a `use` statement for consistent formatting
10+
--> $DIR/absolute_symbol_paths.rs:41:5
11+
|
12+
LL | core::f32::MAX;
13+
| ^^^^^^^^^^^^^^
14+
15+
error: consider referring to this symbol by adding a `use` statement for consistent formatting
16+
--> $DIR/absolute_symbol_paths.rs:58:9
17+
|
18+
LL | use std::f32::MAX;
19+
| ^^^^^^^^^^^^^
20+
21+
error: aborting due to 3 previous errors
22+

tests/ui-toml/absolute_symbol_paths/absolute_symbol_paths.allow_std.stderr

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
error: consider referring to this symbol by adding a `use` statement for consistent formatting
2+
--> $DIR/absolute_symbol_paths.rs:40:5
3+
|
4+
LL | std::f32::MAX;
5+
| ^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::absolute-symbol-paths` implied by `-D warnings`
8+
9+
error: consider referring to this symbol by adding a `use` statement for consistent formatting
10+
--> $DIR/absolute_symbol_paths.rs:41:5
11+
|
12+
LL | core::f32::MAX;
13+
| ^^^^^^^^^^^^^^
14+
15+
error: consider referring to this symbol by adding a `use` statement for consistent formatting
16+
--> $DIR/absolute_symbol_paths.rs:42:5
17+
|
18+
LL | crate::a::b::c::C;
19+
| ^^^^^^^^^^^^^^^^^
20+
21+
error: consider referring to this symbol by adding a `use` statement for consistent formatting
22+
--> $DIR/absolute_symbol_paths.rs:43:5
23+
|
24+
LL | crate::a::b::c::d::e::f::F;
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
26+
27+
error: consider referring to this symbol by adding a `use` statement for consistent formatting
28+
--> $DIR/absolute_symbol_paths.rs:44:5
29+
|
30+
LL | crate::a::A;
31+
| ^^^^^^^^^^^
32+
33+
error: consider referring to this symbol by adding a `use` statement for consistent formatting
34+
--> $DIR/absolute_symbol_paths.rs:45:5
35+
|
36+
LL | crate::a::b::B;
37+
| ^^^^^^^^^^^^^^
38+
39+
error: consider referring to this symbol by adding a `use` statement for consistent formatting
40+
--> $DIR/absolute_symbol_paths.rs:46:5
41+
|
42+
LL | crate::a::b::c::C::ZERO;
43+
| ^^^^^^^^^^^^^^^^^
44+
45+
error: consider referring to this symbol by adding a `use` statement for consistent formatting
46+
--> $DIR/absolute_symbol_paths.rs:47:5
47+
|
48+
LL | helper::b::c::d::e::f();
49+
| ^^^^^^^^^^^^^^^^^^^^^
50+
51+
error: consider referring to this symbol by adding a `use` statement for consistent formatting
52+
--> $DIR/absolute_symbol_paths.rs:57:9
53+
|
54+
LL | use crate::a::b::c::C;
55+
| ^^^^^^^^^^^^^^^^^
56+
57+
error: consider referring to this symbol by adding a `use` statement for consistent formatting
58+
--> $DIR/absolute_symbol_paths.rs:58:9
59+
|
60+
LL | use std::f32::MAX;
61+
| ^^^^^^^^^^^^^
62+
63+
error: aborting due to 10 previous errors
64+

tests/ui-toml/absolute_symbol_paths/absolute_symbol_paths.disallow_std.stderr

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)