|
| 1 | +use clippy_utils::diagnostics::span_lint; |
| 2 | +use clippy_utils::is_from_proc_macro; |
| 3 | +use rustc_hir::intravisit::{walk_qpath, Visitor}; |
| 4 | +use rustc_hir::{HirId, QPath}; |
| 5 | +use rustc_lint::{LateContext, LateLintPass}; |
| 6 | +use rustc_middle::hir::nested_filter::OnlyBodies; |
| 7 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 8 | +use rustc_span::Span; |
| 9 | + |
| 10 | +declare_clippy_lint! { |
| 11 | + /// ### What it does |
| 12 | + /// Checks for usage of symbols through absolute paths, like `std::env::current_dir`. |
| 13 | + /// |
| 14 | + /// ### Why is this bad? |
| 15 | + /// Many codebases have their own style when it comes to symbol importing, but one that is |
| 16 | + /// seldom used is using absolute paths *everywhere*. This is generally considered unidiomatic, |
| 17 | + /// and you should add a `use` statement. |
| 18 | + /// |
| 19 | + /// Note: One exception to this is code from macro expansion - this does not lint such cases, as |
| 20 | + /// using absolute paths is the proper way of referencing symbols in one. |
| 21 | + /// |
| 22 | + /// ### Example |
| 23 | + /// ```rust |
| 24 | + /// let x = std::f64::consts::PI; |
| 25 | + /// ``` |
| 26 | + /// Use any of the below instead, or anything else: |
| 27 | + /// ```rust |
| 28 | + /// # use std::f64; |
| 29 | + /// # use std::f64::consts; |
| 30 | + /// # use std::f64::consts::PI; |
| 31 | + /// let x = f64::consts::PI; |
| 32 | + /// let x = consts::PI; |
| 33 | + /// let x = PI; |
| 34 | + /// use std::f64::consts as f64_consts; |
| 35 | + /// let x = f64_consts::PI; |
| 36 | + /// ``` |
| 37 | + #[clippy::version = "1.72.0"] |
| 38 | + pub ABSOLUTE_SYMBOL_PATHS, |
| 39 | + style, |
| 40 | + "checks for usage of a symbol without a `use` statement" |
| 41 | +} |
| 42 | +declare_lint_pass!(AbsoluteSymbolPaths => [ABSOLUTE_SYMBOL_PATHS]); |
| 43 | + |
| 44 | +impl LateLintPass<'_> for AbsoluteSymbolPaths { |
| 45 | + fn check_crate(&mut self, cx: &LateContext<'_>) { |
| 46 | + cx.tcx.hir().visit_all_item_likes_in_crate(&mut V { cx }); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +struct V<'a, 'tcx> { |
| 51 | + cx: &'a LateContext<'tcx>, |
| 52 | +} |
| 53 | + |
| 54 | +impl<'a, 'tcx> Visitor<'tcx> for V<'a, 'tcx> { |
| 55 | + type NestedFilter = OnlyBodies; |
| 56 | + |
| 57 | + fn nested_visit_map(&mut self) -> Self::Map { |
| 58 | + self.cx.tcx.hir() |
| 59 | + } |
| 60 | + |
| 61 | + fn visit_qpath(&mut self, qpath: &'tcx QPath<'tcx>, hir_id: HirId, span: Span) { |
| 62 | + let Self { cx } = *self; |
| 63 | + |
| 64 | + if !span.from_expansion() |
| 65 | + && let QPath::Resolved(_, path) = qpath |
| 66 | + && let Some(def_id) = path.res.opt_def_id() |
| 67 | + { |
| 68 | + let def_path = cx.tcx.def_path_str(def_id); |
| 69 | + let segments = def_path.split("::"); |
| 70 | + |
| 71 | + // FIXME: I only allowed 3 segment paths because there's tons of them in clippy :D |
| 72 | + // To my humble reviewer: thoughts? |
| 73 | + if path.segments.len() >= 4 // A 2 segment path seems okay, like `std::println!` |
| 74 | + && segments.eq(path.segments.iter().map(|segment| segment.ident.name.as_str())) |
| 75 | + && !is_from_proc_macro(cx, qpath) |
| 76 | + { |
| 77 | + span_lint( |
| 78 | + cx, |
| 79 | + ABSOLUTE_SYMBOL_PATHS, |
| 80 | + span, |
| 81 | + "consider referring to this symbol by adding a `use` statement for consistent formatting", |
| 82 | + ); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + walk_qpath(self, qpath, hir_id); |
| 87 | + } |
| 88 | +} |
0 commit comments