Skip to content

Commit ea0daf8

Browse files
committed
Handle paths with leading :: (root)
1 parent 0b6c37a commit ea0daf8

File tree

4 files changed

+59
-14
lines changed

4 files changed

+59
-14
lines changed

clippy_lints/src/absolute_symbol_paths.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,24 @@ impl LateLintPass<'_> for AbsoluteSymbolPaths {
6161
if !path.span.from_expansion()
6262
&& let Some(node) = cx.tcx.hir().find(hir_id)
6363
&& !matches!(node, Node::Item(item) if matches!(item.kind, ItemKind::Use(_, _)))
64-
&& path.segments.len() > *absolute_symbol_paths_max_segments as usize
65-
&& let [first, ..] = path.segments
66-
&& let Some(first_snippet) = snippet_opt(cx, first.ident.span)
67-
&& first_snippet == first.ident.as_str()
64+
&& let [first, rest @ ..] = path.segments
65+
// Handle `::std`
66+
&& let (segment, len) = if first.ident.name == kw::PathRoot {
67+
// Indexing is fine as `PathRoot` must be followed by another segment. Similarly,
68+
// `len() - 1` is fine here for the same reason
69+
(&rest[0], path.segments.len() - 1)
70+
} else {
71+
(first, path.segments.len())
72+
}
73+
&& len > *absolute_symbol_paths_max_segments as usize
74+
&& let Some(segment_snippet) = snippet_opt(cx, segment.ident.span)
75+
&& segment_snippet == segment.ident.as_str()
6876
{
6977
let is_abs_external =
70-
matches!(first.res, Res::Def(DefKind::Mod, DefId { index, .. }) if index == CRATE_DEF_INDEX);
71-
let is_abs_crate = first.ident.name == kw::Crate;
78+
matches!(segment.res, Res::Def(DefKind::Mod, DefId { index, .. }) if index == CRATE_DEF_INDEX);
79+
let is_abs_crate = segment.ident.name == kw::Crate;
7280

73-
if is_abs_external && absolute_symbol_paths_allowed_crates.contains(first.ident.name.as_str())
81+
if is_abs_external && absolute_symbol_paths_allowed_crates.contains(segment.ident.name.as_str())
7482
|| is_abs_crate && absolute_symbol_paths_allowed_crates.contains("crate")
7583
{
7684
return;

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,17 @@ error: consider referring to this symbol by adding a `use` statement for consist
1212
LL | core::f32::MAX;
1313
| ^^^^^^^^^^^^^^
1414

15-
error: aborting due to 2 previous errors
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 | ::core::f32::MAX;
19+
| ^^^^^^^^^^^^^^^^
20+
21+
error: consider referring to this symbol by adding a `use` statement for consistent formatting
22+
--> $DIR/absolute_symbol_paths.rs:58:5
23+
|
24+
LL | ::std::f32::MAX;
25+
| ^^^^^^^^^^^^^^^
26+
27+
error: aborting due to 4 previous errors
1628

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,56 @@ LL | core::f32::MAX;
1515
error: consider referring to this symbol by adding a `use` statement for consistent formatting
1616
--> $DIR/absolute_symbol_paths.rs:42:5
1717
|
18+
LL | ::core::f32::MAX;
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+
|
1824
LL | crate::a::b::c::C;
1925
| ^^^^^^^^^^^^^^^^^
2026

2127
error: consider referring to this symbol by adding a `use` statement for consistent formatting
22-
--> $DIR/absolute_symbol_paths.rs:43:5
28+
--> $DIR/absolute_symbol_paths.rs:44:5
2329
|
2430
LL | crate::a::b::c::d::e::f::F;
2531
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
2632

2733
error: consider referring to this symbol by adding a `use` statement for consistent formatting
28-
--> $DIR/absolute_symbol_paths.rs:44:5
34+
--> $DIR/absolute_symbol_paths.rs:45:5
2935
|
3036
LL | crate::a::A;
3137
| ^^^^^^^^^^^
3238

3339
error: consider referring to this symbol by adding a `use` statement for consistent formatting
34-
--> $DIR/absolute_symbol_paths.rs:45:5
40+
--> $DIR/absolute_symbol_paths.rs:46:5
3541
|
3642
LL | crate::a::b::B;
3743
| ^^^^^^^^^^^^^^
3844

3945
error: consider referring to this symbol by adding a `use` statement for consistent formatting
40-
--> $DIR/absolute_symbol_paths.rs:46:5
46+
--> $DIR/absolute_symbol_paths.rs:47:5
4147
|
4248
LL | crate::a::b::c::C::ZERO;
4349
| ^^^^^^^^^^^^^^^^^
4450

4551
error: consider referring to this symbol by adding a `use` statement for consistent formatting
46-
--> $DIR/absolute_symbol_paths.rs:47:5
52+
--> $DIR/absolute_symbol_paths.rs:48:5
4753
|
4854
LL | helper::b::c::d::e::f();
4955
| ^^^^^^^^^^^^^^^^^^^^^
5056

51-
error: aborting due to 8 previous errors
57+
error: consider referring to this symbol by adding a `use` statement for consistent formatting
58+
--> $DIR/absolute_symbol_paths.rs:49:5
59+
|
60+
LL | ::helper::b::c::d::e::f();
61+
| ^^^^^^^^^^^^^^^^^^^^^^^
62+
63+
error: consider referring to this symbol by adding a `use` statement for consistent formatting
64+
--> $DIR/absolute_symbol_paths.rs:58:5
65+
|
66+
LL | ::std::f32::MAX;
67+
| ^^^^^^^^^^^^^^^
68+
69+
error: aborting due to 11 previous errors
5270

tests/ui-toml/absolute_symbol_paths/absolute_symbol_paths.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,25 @@ fn main() {
3939
f32::max(1.0, 2.0);
4040
std::f32::MAX;
4141
core::f32::MAX;
42+
::core::f32::MAX;
4243
crate::a::b::c::C;
4344
crate::a::b::c::d::e::f::F;
4445
crate::a::A;
4546
crate::a::b::B;
4647
crate::a::b::c::C::ZERO;
4748
helper::b::c::d::e::f();
49+
::helper::b::c::d::e::f();
4850
fn b() -> a::b::B {
4951
todo!()
5052
}
5153
std::println!("a");
5254
let x = 1;
5355
std::ptr::addr_of!(x);
56+
// Test we handle max segments with `PathRoot` properly; this has 4 segments but we should say it
57+
// has 3
58+
::std::f32::MAX;
59+
// Do not lint due to the above
60+
::helper::a();
5461
// Do not lint
5562
helper::a();
5663
use crate::a::b::c::C;

0 commit comments

Comments
 (0)