-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New lint [absolute_paths
]
#11003
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
New lint [absolute_paths
]
#11003
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
use clippy_utils::diagnostics::span_lint; | ||
use clippy_utils::source::snippet_opt; | ||
use rustc_data_structures::fx::FxHashSet; | ||
use rustc_hir::def::{DefKind, Res}; | ||
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX}; | ||
use rustc_hir::{HirId, ItemKind, Node, Path}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_tool_lint, impl_lint_pass}; | ||
use rustc_span::symbol::kw; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for usage of items through absolute paths, like `std::env::current_dir`. | ||
/// | ||
/// ### Why is this bad? | ||
/// Many codebases have their own style when it comes to importing, but one that is seldom used | ||
/// is using absolute paths *everywhere*. This is generally considered unidiomatic, and you | ||
/// should add a `use` statement. | ||
/// | ||
/// The default maximum segments (2) is pretty strict, you may want to increase this in | ||
/// `clippy.toml`. | ||
/// | ||
/// Note: One exception to this is code from macro expansion - this does not lint such cases, as | ||
/// using absolute paths is the proper way of referencing items in one. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// let x = std::f64::consts::PI; | ||
/// ``` | ||
/// Use any of the below instead, or anything else: | ||
/// ```rust | ||
/// use std::f64; | ||
/// use std::f64::consts; | ||
/// use std::f64::consts::PI; | ||
/// let x = f64::consts::PI; | ||
/// let x = consts::PI; | ||
/// let x = PI; | ||
/// use std::f64::consts as f64_consts; | ||
/// let x = f64_consts::PI; | ||
/// ``` | ||
#[clippy::version = "1.73.0"] | ||
pub ABSOLUTE_PATHS, | ||
restriction, | ||
"checks for usage of an item without a `use` statement" | ||
} | ||
impl_lint_pass!(AbsolutePaths => [ABSOLUTE_PATHS]); | ||
|
||
pub struct AbsolutePaths { | ||
pub absolute_paths_max_segments: u64, | ||
pub absolute_paths_allowed_crates: FxHashSet<String>, | ||
} | ||
|
||
impl LateLintPass<'_> for AbsolutePaths { | ||
// We should only lint `QPath::Resolved`s, but since `Path` is only used in `Resolved` and `UsePath` | ||
// we don't need to use a visitor or anything as we can just check if the `Node` for `hir_id` isn't | ||
// a `Use` | ||
#[expect(clippy::cast_possible_truncation)] | ||
fn check_path(&mut self, cx: &LateContext<'_>, path: &Path<'_>, hir_id: HirId) { | ||
let Self { | ||
absolute_paths_max_segments, | ||
absolute_paths_allowed_crates, | ||
} = self; | ||
|
||
if !path.span.from_expansion() | ||
&& let Some(node) = cx.tcx.hir().find(hir_id) | ||
&& !matches!(node, Node::Item(item) if matches!(item.kind, ItemKind::Use(_, _))) | ||
&& let [first, rest @ ..] = path.segments | ||
// Handle `::std` | ||
&& let (segment, len) = if first.ident.name == kw::PathRoot { | ||
// Indexing is fine as `PathRoot` must be followed by another segment. `len() - 1` | ||
// is fine here for the same reason | ||
(&rest[0], path.segments.len() - 1) | ||
} else { | ||
(first, path.segments.len()) | ||
} | ||
&& len > *absolute_paths_max_segments as usize | ||
&& let Some(segment_snippet) = snippet_opt(cx, segment.ident.span) | ||
&& segment_snippet == segment.ident.as_str() | ||
{ | ||
let is_abs_external = | ||
matches!(segment.res, Res::Def(DefKind::Mod, DefId { index, .. }) if index == CRATE_DEF_INDEX); | ||
let is_abs_crate = segment.ident.name == kw::Crate; | ||
|
||
if is_abs_external && absolute_paths_allowed_crates.contains(segment.ident.name.as_str()) | ||
|| is_abs_crate && absolute_paths_allowed_crates.contains("crate") | ||
{ | ||
return; | ||
} | ||
|
||
if is_abs_external || is_abs_crate { | ||
span_lint( | ||
cx, | ||
ABSOLUTE_PATHS, | ||
path.span, | ||
"consider bringing this path into scope with the `use` keyword", | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
tests/ui-toml/absolute_paths/absolute_paths.allow_crates.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
error: consider bringing this path into scope with the `use` keyword | ||
--> $DIR/absolute_paths.rs:40:5 | ||
| | ||
LL | std::f32::MAX; | ||
| ^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::absolute-paths` implied by `-D warnings` | ||
|
||
error: consider bringing this path into scope with the `use` keyword | ||
--> $DIR/absolute_paths.rs:41:5 | ||
| | ||
LL | core::f32::MAX; | ||
| ^^^^^^^^^^^^^^ | ||
|
||
error: consider bringing this path into scope with the `use` keyword | ||
--> $DIR/absolute_paths.rs:42:5 | ||
| | ||
LL | ::core::f32::MAX; | ||
| ^^^^^^^^^^^^^^^^ | ||
|
||
error: consider bringing this path into scope with the `use` keyword | ||
--> $DIR/absolute_paths.rs:58:5 | ||
| | ||
LL | ::std::f32::MAX; | ||
| ^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 4 previous errors | ||
|
70 changes: 70 additions & 0 deletions
70
tests/ui-toml/absolute_paths/absolute_paths.disallow_crates.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
error: consider bringing this path into scope with the `use` keyword | ||
--> $DIR/absolute_paths.rs:40:5 | ||
| | ||
LL | std::f32::MAX; | ||
| ^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::absolute-paths` implied by `-D warnings` | ||
|
||
error: consider bringing this path into scope with the `use` keyword | ||
--> $DIR/absolute_paths.rs:41:5 | ||
| | ||
LL | core::f32::MAX; | ||
| ^^^^^^^^^^^^^^ | ||
|
||
error: consider bringing this path into scope with the `use` keyword | ||
--> $DIR/absolute_paths.rs:42:5 | ||
| | ||
LL | ::core::f32::MAX; | ||
| ^^^^^^^^^^^^^^^^ | ||
|
||
error: consider bringing this path into scope with the `use` keyword | ||
--> $DIR/absolute_paths.rs:43:5 | ||
| | ||
LL | crate::a::b::c::C; | ||
| ^^^^^^^^^^^^^^^^^ | ||
|
||
error: consider bringing this path into scope with the `use` keyword | ||
--> $DIR/absolute_paths.rs:44:5 | ||
| | ||
LL | crate::a::b::c::d::e::f::F; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: consider bringing this path into scope with the `use` keyword | ||
--> $DIR/absolute_paths.rs:45:5 | ||
| | ||
LL | crate::a::A; | ||
| ^^^^^^^^^^^ | ||
|
||
error: consider bringing this path into scope with the `use` keyword | ||
--> $DIR/absolute_paths.rs:46:5 | ||
| | ||
LL | crate::a::b::B; | ||
| ^^^^^^^^^^^^^^ | ||
|
||
error: consider bringing this path into scope with the `use` keyword | ||
--> $DIR/absolute_paths.rs:47:5 | ||
| | ||
LL | crate::a::b::c::C::ZERO; | ||
| ^^^^^^^^^^^^^^^^^ | ||
|
||
error: consider bringing this path into scope with the `use` keyword | ||
--> $DIR/absolute_paths.rs:48:5 | ||
| | ||
LL | helper::b::c::d::e::f(); | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: consider bringing this path into scope with the `use` keyword | ||
--> $DIR/absolute_paths.rs:49:5 | ||
| | ||
LL | ::helper::b::c::d::e::f(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: consider bringing this path into scope with the `use` keyword | ||
--> $DIR/absolute_paths.rs:58:5 | ||
| | ||
LL | ::std::f32::MAX; | ||
| ^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 11 previous errors | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.