|
| 1 | +use clippy_utils::diagnostics::span_lint_and_help; |
| 2 | +use clippy_utils::ty::is_type_diagnostic_item; |
| 3 | +use clippy_utils::{match_qpath, paths, peel_hir_expr_refs}; |
| 4 | +use rustc_hir::{StmtKind,BorrowKind, Mutability, BindingAnnotation, PatKind, Expr, ExprKind}; |
| 5 | +use rustc_lint::{LateContext, LateLintPass}; |
| 6 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 7 | +use rustc_span::sym; |
| 8 | + |
| 9 | +declare_clippy_lint! { |
| 10 | + /// ### What it does |
| 11 | + /// |
| 12 | + /// ### Why is this bad? |
| 13 | + /// |
| 14 | + /// ### Example |
| 15 | + /// ```rust |
| 16 | + /// // example code where clippy issues a warning |
| 17 | + /// ``` |
| 18 | + /// Use instead: |
| 19 | + /// ```rust |
| 20 | + /// // example code which does not raise clippy warning |
| 21 | + /// ``` |
| 22 | + #[clippy::version = "1.62.0"] |
| 23 | + pub PATH_FROM_FORMAT, |
| 24 | + pedantic, |
| 25 | + "default lint description" |
| 26 | +} |
| 27 | + |
| 28 | +declare_lint_pass!(PathFromFormat => [PATH_FROM_FORMAT]); |
| 29 | + |
| 30 | +impl<'tcx> LateLintPass<'tcx> for PathFromFormat { |
| 31 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { |
| 32 | + if_chain! { |
| 33 | + if let ExprKind::Call(func, args) = expr.kind; |
| 34 | + if let ExprKind::Path(ref qpath) = func.kind; |
| 35 | + if match_qpath(qpath, &["PathBuf", "from"]); |
| 36 | + // if args.len() == 1; |
| 37 | + if let Some(macro_def_id) = args[0].span.ctxt().outer_expn_data().macro_def_id; |
| 38 | + if cx.tcx.get_diagnostic_name(macro_def_id) == Some(sym::format_macro); |
| 39 | + // if let ExprKind::Block(block, None) = args[0].kind; |
| 40 | + // if block.stmts.len() == 1; |
| 41 | + // if let StmtKind::Local(local) = block.stmts[0].kind; |
| 42 | + // if let Some(init) = local.init; |
| 43 | + // if let ExprKind::Call(func1, args1) = init.kind; |
| 44 | + // if let ExprKind::Path(ref qpath1) = func1.kind; |
| 45 | + // if match_qpath(qpath1, &["$crate", "fmt", "format"]); |
| 46 | + // if args1.len() == 1; |
| 47 | + // if let ExprKind::Call(func2, args2) = args1[0].kind; |
| 48 | + // if let ExprKind::Path(ref qpath2) = func2.kind; |
| 49 | + // if match_qpath(qpath2, &["$crate", "fmt", "Arguments", "new_v1"]); |
| 50 | + // if args2.len() == 2; |
| 51 | + // if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, inner) = args2[0].kind; |
| 52 | + // if let ExprKind::Array(elements) = inner.kind; |
| 53 | + // if elements.len() == 2; |
| 54 | + // if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, inner1) = args2[1].kind; |
| 55 | + // if let ExprKind::Array(elements1) = inner1.kind; |
| 56 | + // if elements1.len() == 1; |
| 57 | + // if let ExprKind::Call(func3, args3) = elements1[0].kind; |
| 58 | + // if let ExprKind::Path(ref qpath3) = func3.kind; |
| 59 | + // if match_qpath(qpath3, &["$crate", "fmt", "ArgumentV1", "new_display"]); |
| 60 | + // if args3.len() == 1; |
| 61 | + // if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, inner2) = args3[0].kind; |
| 62 | + // if let ExprKind::Path(ref qpath4) = inner2.kind; |
| 63 | + // if match_qpath(qpath4, &["base_path"]); |
| 64 | + // if let PatKind::Binding(BindingAnnotation::Unannotated, _, name, None) = local.pat.kind; |
| 65 | + // if name.as_str() == "res"; |
| 66 | + // if let Some(trailing_expr) = block.expr; |
| 67 | + // if let ExprKind::Path(ref qpath5) = trailing_expr.kind; |
| 68 | + // if match_qpath(qpath5, &["res"]); |
| 69 | + then { |
| 70 | + span_lint_and_help( |
| 71 | + cx, |
| 72 | + PATH_FROM_FORMAT, |
| 73 | + expr.span, |
| 74 | + "`format!(..)` used to form `PathBuf`", |
| 75 | + None, |
| 76 | + "consider using `.join()` or `.push()` to avoid the extra allocation", |
| 77 | + ); |
| 78 | + // report your lint here |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments