|  | 
|  | 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; | 
|  | 2 | +use clippy_utils::msrvs::{self, Msrv}; | 
|  | 3 | +use clippy_utils::{is_trait_method, match_def_path, paths, peel_hir_expr_refs}; | 
|  | 4 | +use rustc_errors::Applicability; | 
|  | 5 | +use rustc_hir::def::{DefKind, Res}; | 
|  | 6 | +use rustc_hir::*; | 
|  | 7 | +use rustc_lint::{LateContext, LateLintPass}; | 
|  | 8 | +use rustc_middle::ty; | 
|  | 9 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; | 
|  | 10 | +use rustc_span::sym; | 
|  | 11 | + | 
|  | 12 | +declare_clippy_lint! { | 
|  | 13 | +    /// ### What it does | 
|  | 14 | +    /// Checks for references on `std::path::MAIN_SEPARATOR.to_string()` used | 
|  | 15 | +    /// to build a `&str`. | 
|  | 16 | +    /// | 
|  | 17 | +    /// ### Why is this bad? | 
|  | 18 | +    /// There exists a `std::path::MAIN_SEPARATOR_STR` which does not require | 
|  | 19 | +    /// an extra memory allocation. | 
|  | 20 | +    /// | 
|  | 21 | +    /// ### Example | 
|  | 22 | +    /// ```rust | 
|  | 23 | +    /// let s: &str = &std::path::MAIN_SEPARATOR.to_string(); | 
|  | 24 | +    /// ``` | 
|  | 25 | +    /// Use instead: | 
|  | 26 | +    /// ```rust | 
|  | 27 | +    /// let s: &str = std::path::MAIN_SEPARATOR_STR; | 
|  | 28 | +    /// ``` | 
|  | 29 | +    #[clippy::version = "1.70.0"] | 
|  | 30 | +    pub MANUAL_MAIN_SEPARATOR_STR, | 
|  | 31 | +    complexity, | 
|  | 32 | +    "`&std::path::MAIN_SEPARATOR.to_string()` can be replaced by `std::path::MAIN_SEPARATOR_STR`" | 
|  | 33 | +} | 
|  | 34 | + | 
|  | 35 | +pub struct ManualMainSeparatorStr { | 
|  | 36 | +    msrv: Msrv, | 
|  | 37 | +} | 
|  | 38 | + | 
|  | 39 | +impl ManualMainSeparatorStr { | 
|  | 40 | +    #[must_use] | 
|  | 41 | +    pub fn new(msrv: Msrv) -> Self { | 
|  | 42 | +        Self { msrv } | 
|  | 43 | +    } | 
|  | 44 | +} | 
|  | 45 | + | 
|  | 46 | +impl_lint_pass!(ManualMainSeparatorStr => [MANUAL_MAIN_SEPARATOR_STR]); | 
|  | 47 | + | 
|  | 48 | +impl LateLintPass<'_> for ManualMainSeparatorStr { | 
|  | 49 | +    fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { | 
|  | 50 | +        if self.msrv.meets(msrvs::PATH_MAIN_SEPARATOR_STR) && | 
|  | 51 | +            let (target, _) = peel_hir_expr_refs(expr) && | 
|  | 52 | +            is_trait_method(cx, target, sym::ToString) && | 
|  | 53 | +            let ExprKind::MethodCall(path, receiver, &[], _) = target.kind && | 
|  | 54 | +            path.ident.name == sym::to_string && | 
|  | 55 | +            let ExprKind::Path(QPath::Resolved(None, path)) = receiver.kind && | 
|  | 56 | +            let Res::Def(DefKind::Const, receiver_def_id) = path.res && | 
|  | 57 | +            match_def_path(cx, receiver_def_id, &paths::PATH_MAIN_SEPARATOR) && | 
|  | 58 | +            let ty::Ref(_, ty, Mutability::Not) = cx.typeck_results().expr_ty_adjusted(expr).kind() && | 
|  | 59 | +            ty.is_str() | 
|  | 60 | +            { | 
|  | 61 | +                span_lint_and_sugg( | 
|  | 62 | +                    cx, | 
|  | 63 | +                    MANUAL_MAIN_SEPARATOR_STR, | 
|  | 64 | +                    expr.span, | 
|  | 65 | +                    "taking a reference on `std::path::MAIN_SEPARATOR` conversion to `String`", | 
|  | 66 | +                    "replace with", | 
|  | 67 | +                    "std::path::MAIN_SEPARATOR_STR".to_owned(), | 
|  | 68 | +                    Applicability::MachineApplicable, | 
|  | 69 | +                ); | 
|  | 70 | +            } | 
|  | 71 | +    } | 
|  | 72 | +} | 
0 commit comments