|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::path_to_local_id; |
| 3 | +use clippy_utils::source::{snippet, snippet_opt}; |
| 4 | +use clippy_utils::ty::is_type_diagnostic_item; |
| 5 | +use rustc_errors::Applicability; |
| 6 | +use rustc_hir::def::Res; |
| 7 | +use rustc_hir::{BindingAnnotation, Block, Expr, ExprKind, HirId, Local, PatKind, QPath, Stmt, StmtKind}; |
| 8 | +use rustc_lint::{LateContext, LateLintPass, LintContext}; |
| 9 | +use rustc_middle::lint::in_external_macro; |
| 10 | +use rustc_session::impl_lint_pass; |
| 11 | +use rustc_span::{sym, Span, Symbol}; |
| 12 | + |
| 13 | +declare_clippy_lint! { |
| 14 | + /// ### What it does |
| 15 | + /// Checks for calls to `push` immediately after creating a new `PathBuf`. |
| 16 | + /// |
| 17 | + /// ### Why is this bad? |
| 18 | + /// The `.join()` is easier to read than multiple `push` calls. |
| 19 | + /// |
| 20 | + /// ### Known problems |
| 21 | + /// `.join()` introduces an implicit `clone()` |
| 22 | + /// |
| 23 | + /// ### Example |
| 24 | + /// ```rust |
| 25 | + /// let mut path_buf = PathBuf::new(); |
| 26 | + /// path_buf.push("foo"); |
| 27 | + /// ``` |
| 28 | + /// Use instead: |
| 29 | + /// ```rust |
| 30 | + /// let path_buf = PathBuf::new().join("foo"); |
| 31 | + /// ``` |
| 32 | + #[clippy::version = "1.75.0"] |
| 33 | + pub PATHBUF_INIT_THEN_PUSH, |
| 34 | + complexity, |
| 35 | + "`push` immediately after `PathBuf` creation" |
| 36 | +} |
| 37 | + |
| 38 | +impl_lint_pass!(PathbufThenPush => [PATHBUF_INIT_THEN_PUSH]); |
| 39 | + |
| 40 | +#[derive(Default)] |
| 41 | +pub struct PathbufThenPush { |
| 42 | + searcher: Option<PathbufPushSearcher>, |
| 43 | +} |
| 44 | + |
| 45 | +struct PathbufPushSearcher { |
| 46 | + local_id: HirId, |
| 47 | + lhs_is_let: bool, |
| 48 | + let_ty_span: Option<Span>, |
| 49 | + init_val_span: Span, |
| 50 | + arg_span: Option<Span>, |
| 51 | + name: Symbol, |
| 52 | + err_span: Span, |
| 53 | +} |
| 54 | + |
| 55 | +impl PathbufPushSearcher { |
| 56 | + fn display_err(&self, cx: &LateContext<'_>) { |
| 57 | + let Some(arg_span) = self.arg_span else { return }; |
| 58 | + let Some(arg_str) = snippet_opt(cx, arg_span) else { |
| 59 | + return; |
| 60 | + }; |
| 61 | + let Some(init_val) = snippet_opt(cx, self.init_val_span) else { |
| 62 | + return; |
| 63 | + }; |
| 64 | + let mut s = if self.lhs_is_let { |
| 65 | + String::from("let ") |
| 66 | + } else { |
| 67 | + String::new() |
| 68 | + }; |
| 69 | + s.push_str("mut "); |
| 70 | + s.push_str(self.name.as_str()); |
| 71 | + if let Some(span) = self.let_ty_span { |
| 72 | + s.push_str(": "); |
| 73 | + s.push_str(&snippet(cx, span, "_")); |
| 74 | + } |
| 75 | + s.push_str(&format!(" = {init_val}.join({arg_str});",)); |
| 76 | + |
| 77 | + span_lint_and_sugg( |
| 78 | + cx, |
| 79 | + PATHBUF_INIT_THEN_PUSH, |
| 80 | + self.err_span, |
| 81 | + "calls to `push` immediately after creation", |
| 82 | + "consider using the `.join()`", |
| 83 | + s, |
| 84 | + Applicability::HasPlaceholders, |
| 85 | + ); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +impl<'tcx> LateLintPass<'tcx> for PathbufThenPush { |
| 90 | + fn check_block(&mut self, _: &LateContext<'tcx>, _: &'tcx Block<'tcx>) { |
| 91 | + self.searcher = None; |
| 92 | + } |
| 93 | + |
| 94 | + fn check_local(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>) { |
| 95 | + if let Some(init_expr) = local.init |
| 96 | + && let PatKind::Binding(BindingAnnotation::MUT, id, name, None) = local.pat.kind |
| 97 | + && !in_external_macro(cx.sess(), local.span) |
| 98 | + && let ty = cx.typeck_results().pat_ty(local.pat) |
| 99 | + && is_type_diagnostic_item(cx, ty, sym::PathBuf) |
| 100 | + { |
| 101 | + self.searcher = Some(PathbufPushSearcher { |
| 102 | + local_id: id, |
| 103 | + lhs_is_let: true, |
| 104 | + name: name.name, |
| 105 | + let_ty_span: local.ty.map(|ty| ty.span), |
| 106 | + err_span: local.span, |
| 107 | + init_val_span: init_expr.span, |
| 108 | + arg_span: None, |
| 109 | + }); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { |
| 114 | + if self.searcher.is_none() |
| 115 | + && let ExprKind::Assign(left, right, _) = expr.kind |
| 116 | + && let ExprKind::Path(QPath::Resolved(None, path)) = left.kind |
| 117 | + && let [name] = &path.segments |
| 118 | + && let Res::Local(id) = path.res |
| 119 | + && !in_external_macro(cx.sess(), expr.span) |
| 120 | + && let ty = cx.typeck_results().expr_ty(left) |
| 121 | + && is_type_diagnostic_item(cx, ty, sym::PathBuf) |
| 122 | + { |
| 123 | + self.searcher = Some(PathbufPushSearcher { |
| 124 | + local_id: id, |
| 125 | + lhs_is_let: false, |
| 126 | + let_ty_span: None, |
| 127 | + name: name.ident.name, |
| 128 | + err_span: expr.span, |
| 129 | + init_val_span: right.span, |
| 130 | + arg_span: None, |
| 131 | + }); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) { |
| 136 | + if let Some(mut searcher) = self.searcher.take() { |
| 137 | + if let StmtKind::Expr(expr) | StmtKind::Semi(expr) = stmt.kind |
| 138 | + && let ExprKind::MethodCall(name, self_arg, [arg_expr], _) = expr.kind |
| 139 | + && path_to_local_id(self_arg, searcher.local_id) |
| 140 | + && name.ident.as_str() == "push" |
| 141 | + { |
| 142 | + searcher.err_span = searcher.err_span.to(stmt.span); |
| 143 | + searcher.arg_span = Some(arg_expr.span); |
| 144 | + searcher.display_err(cx); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + fn check_block_post(&mut self, cx: &LateContext<'tcx>, _: &'tcx Block<'tcx>) { |
| 150 | + if let Some(searcher) = self.searcher.take() { |
| 151 | + searcher.display_err(cx); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments