|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::sugg::Sugg; |
| 3 | +use clippy_utils::{expr_or_init, is_path_diagnostic_item, path_res}; |
| 4 | +use rustc_errors::Applicability; |
| 5 | +use rustc_hir::def::{CtorKind, DefKind, Res}; |
| 6 | +use rustc_hir::{Expr, ExprKind, QPath}; |
| 7 | +use rustc_lint::{LateContext, LateLintPass}; |
| 8 | +use rustc_middle::ty::{self, GenericPredicates, ParamTy, Ty}; |
| 9 | +use rustc_session::declare_lint_pass; |
| 10 | +use rustc_span::sym; |
| 11 | +use std::iter; |
| 12 | + |
| 13 | +declare_clippy_lint! { |
| 14 | + /// ### What it does |
| 15 | + /// Detects expressions being enclosed in `Path::new` when passed to a function that accepts |
| 16 | + /// `impl AsRef<Path>`, when the enclosed expression could be used. |
| 17 | + /// |
| 18 | + /// ### Why is this bad? |
| 19 | + /// It is unnecessarily verbose |
| 20 | + /// |
| 21 | + /// ### Example |
| 22 | + /// ```no_run |
| 23 | + /// # use std::{fs, path::Path}; |
| 24 | + /// fs::write(Path::new("foo.txt"), "foo"); |
| 25 | + /// ``` |
| 26 | + /// Use instead: |
| 27 | + /// ```no_run |
| 28 | + /// # use std::{fs, path::Path}; |
| 29 | + /// fs::write("foo.txt", "foo"); |
| 30 | + /// ``` |
| 31 | + #[clippy::version = "1.90.0"] |
| 32 | + pub NEEDLESS_PATH_NEW, |
| 33 | + nursery, |
| 34 | + "an argument passed to a function that accepts `impl AsRef<Path>` \ |
| 35 | + being enclosed in `Path::new` when the argument implements the trait" |
| 36 | +} |
| 37 | + |
| 38 | +declare_lint_pass!(NeedlessPathNew => [NEEDLESS_PATH_NEW]); |
| 39 | + |
| 40 | +impl<'tcx> LateLintPass<'tcx> for NeedlessPathNew { |
| 41 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>) { |
| 42 | + let tcx = cx.tcx; |
| 43 | + |
| 44 | + let (fn_did, args) = match e.kind { |
| 45 | + ExprKind::Call(callee, args) |
| 46 | + if let Res::Def(DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(_, CtorKind::Fn), did) = |
| 47 | + // re: `expr_or_init`: `callee` might be a variable storing a fn ptr, for example, |
| 48 | + // so we need to get to the actual initializer |
| 49 | + path_res(cx, expr_or_init(cx, callee)) => |
| 50 | + { |
| 51 | + (did, args) |
| 52 | + }, |
| 53 | + ExprKind::MethodCall(_, _, args, _) |
| 54 | + if let Some(did) = cx.typeck_results().type_dependent_def_id(e.hir_id) => |
| 55 | + { |
| 56 | + (did, args) |
| 57 | + }, |
| 58 | + _ => return, |
| 59 | + }; |
| 60 | + |
| 61 | + let sig = tcx.fn_sig(fn_did).skip_binder().skip_binder(); |
| 62 | + |
| 63 | + let has_required_preds = |_param_ty: &ParamTy, _preds: GenericPredicates<'_>| -> bool { |
| 64 | + // TODO |
| 65 | + true |
| 66 | + }; |
| 67 | + |
| 68 | + // `ExprKind::MethodCall` doesn't include the receiver in `args`, but does in `sig.inputs()` |
| 69 | + // -- so we iterate over both in `rev`erse in order to line them up starting from the _end_ |
| 70 | + // |
| 71 | + // and for `ExprKind::Call` this is basically a no-op |
| 72 | + iter::zip(sig.inputs().iter().rev(), args.iter().rev()) |
| 73 | + .enumerate() |
| 74 | + .for_each(|(arg_idx, (arg_ty, arg))| { |
| 75 | + // we want `arg` to be `Path::new(x)` |
| 76 | + if let ExprKind::Call(path_new, [x]) = arg.kind |
| 77 | + && let ExprKind::Path(QPath::TypeRelative(path, new)) = path_new.kind |
| 78 | + && is_path_diagnostic_item(cx, path, sym::Path) |
| 79 | + && new.ident.name == sym::new |
| 80 | + && let ty::Param(arg_param_ty) = arg_ty.kind() |
| 81 | + && !is_used_anywhere_else( |
| 82 | + arg_param_ty, |
| 83 | + sig.inputs() |
| 84 | + .iter() |
| 85 | + // `arg_idx` is based on the reversed order, so we need to reverse as well |
| 86 | + .rev() |
| 87 | + .enumerate() |
| 88 | + .filter_map(|(i, input)| (i != arg_idx).then_some(*input)), |
| 89 | + ) |
| 90 | + && has_required_preds(arg_param_ty, cx.tcx.predicates_of(fn_did)) |
| 91 | + { |
| 92 | + let mut applicability = Applicability::MachineApplicable; |
| 93 | + let sugg = Sugg::hir_with_applicability(cx, x, "_", &mut applicability); |
| 94 | + span_lint_and_sugg( |
| 95 | + cx, |
| 96 | + NEEDLESS_PATH_NEW, |
| 97 | + arg.span, |
| 98 | + "the expression enclosed in `Path::new` implements `AsRef<Path>`", |
| 99 | + "remove the enclosing `Path::new`", |
| 100 | + sugg.to_string(), |
| 101 | + applicability, |
| 102 | + ); |
| 103 | + } |
| 104 | + }) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +fn is_used_anywhere_else<'a>(param_ty: &'_ ParamTy, mut other_sig_tys: impl Iterator<Item = Ty<'a>>) -> bool { |
| 109 | + other_sig_tys.any(|sig_ty| { |
| 110 | + sig_ty.walk().any(|generic_arg| { |
| 111 | + if let Some(ty) = generic_arg.as_type() |
| 112 | + && let ty::Param(pt) = ty.kind() |
| 113 | + && pt == param_ty |
| 114 | + { |
| 115 | + true |
| 116 | + } else { |
| 117 | + false |
| 118 | + } |
| 119 | + }) |
| 120 | + }) |
| 121 | +} |
0 commit comments