-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
57 changed files
with
480 additions
and
235 deletions.
There are no files selected for viewing
This file contains 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 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 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,88 @@ | ||
use clippy_utils::diagnostics::span_lint; | ||
use clippy_utils::is_from_proc_macro; | ||
use rustc_hir::intravisit::{walk_qpath, Visitor}; | ||
use rustc_hir::{HirId, QPath}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_middle::hir::nested_filter::OnlyBodies; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_span::Span; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for usage of symbols through absolute paths, like `std::env::current_dir`. | ||
/// | ||
/// ### Why is this bad? | ||
/// Many codebases have their own style when it comes to symbol importing, but one that is | ||
/// seldom used is using absolute paths *everywhere*. This is generally considered unidiomatic, | ||
/// and you should add a `use` statement. | ||
/// | ||
/// 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 symbols 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.72.0"] | ||
pub ABSOLUTE_SYMBOL_PATHS, | ||
style, | ||
"checks for usage of a symbol without a `use` statement" | ||
} | ||
declare_lint_pass!(AbsoluteSymbolPaths => [ABSOLUTE_SYMBOL_PATHS]); | ||
|
||
impl LateLintPass<'_> for AbsoluteSymbolPaths { | ||
fn check_crate(&mut self, cx: &LateContext<'_>) { | ||
cx.tcx.hir().visit_all_item_likes_in_crate(&mut V { cx }); | ||
} | ||
} | ||
|
||
struct V<'a, 'tcx> { | ||
cx: &'a LateContext<'tcx>, | ||
} | ||
|
||
impl<'a, 'tcx> Visitor<'tcx> for V<'a, 'tcx> { | ||
type NestedFilter = OnlyBodies; | ||
|
||
fn nested_visit_map(&mut self) -> Self::Map { | ||
self.cx.tcx.hir() | ||
} | ||
|
||
fn visit_qpath(&mut self, qpath: &'tcx QPath<'tcx>, hir_id: HirId, span: Span) { | ||
let Self { cx } = *self; | ||
|
||
if !span.from_expansion() | ||
&& let QPath::Resolved(_, path) = qpath | ||
&& let Some(def_id) = path.res.opt_def_id() | ||
{ | ||
let def_path = cx.tcx.def_path_str(def_id); | ||
let segments = def_path.split("::"); | ||
|
||
// FIXME: I only allowed 3 segment paths because there's tons of them in clippy :D | ||
// To my humble reviewer: thoughts? | ||
if path.segments.len() >= 4 // A 2 segment path seems okay, like `std::println!` | ||
&& segments.eq(path.segments.iter().map(|segment| segment.ident.name.as_str())) | ||
&& !is_from_proc_macro(cx, qpath) | ||
{ | ||
span_lint( | ||
cx, | ||
ABSOLUTE_SYMBOL_PATHS, | ||
span, | ||
"consider referring to this symbol by adding a `use` statement for consistent formatting", | ||
); | ||
} | ||
} | ||
|
||
walk_qpath(self, qpath, hir_id); | ||
} | ||
} |
This file contains 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 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 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 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 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 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 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#![allow(clippy::absolute_symbol_paths)] | ||
#![warn(clippy::self_named_module_files)] | ||
|
||
mod bad; | ||
|
This file contains 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 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 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 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 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
Oops, something went wrong.