Skip to content

Commit 599b3d7

Browse files
committed
create path_ty, asref_def_id when constructing the lint pass
1 parent cdcc86b commit 599b3d7

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,6 @@ pub fn register_lint_passes(store: &mut rustc_lint::LintStore, conf: &'static Co
831831
store.register_late_pass(|_| Box::new(cloned_ref_to_slice_refs::ClonedRefToSliceRefs::new(conf)));
832832
store.register_late_pass(|_| Box::new(infallible_try_from::InfallibleTryFrom));
833833
store.register_late_pass(|_| Box::new(coerce_container_to_any::CoerceContainerToAny));
834-
store.register_late_pass(|_| Box::new(needless_path_new::NeedlessPathNew));
834+
store.register_late_pass(|tcx| Box::new(needless_path_new::NeedlessPathNew::new(tcx)));
835835
// add lints here, do not remove this comment, it's used in `new_lint`
836836
}

clippy_lints/src/needless_path_new.rs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ use clippy_utils::source::snippet;
44
use clippy_utils::ty::implements_trait;
55
use rustc_errors::Applicability;
66
use rustc_hir::def::{DefKind, Res};
7+
use rustc_hir::def_id::DefId;
78
use rustc_hir::{Expr, ExprKind, QPath};
89
use rustc_lint::{LateContext, LateLintPass};
9-
use rustc_middle::ty::{self, List, Ty};
10-
use rustc_session::declare_lint_pass;
10+
use rustc_middle::ty::{List, Ty, TyCtxt};
11+
use rustc_session::impl_lint_pass;
1112
use rustc_span::sym;
1213
use std::iter;
1314

@@ -36,12 +37,35 @@ declare_clippy_lint! {
3637
being enclosed in `Path::new` when the argument implements the trait"
3738
}
3839

39-
declare_lint_pass!(NeedlessPathNew => [NEEDLESS_PATH_NEW]);
40+
impl_lint_pass!(NeedlessPathNew<'_> => [NEEDLESS_PATH_NEW]);
4041

41-
impl<'tcx> LateLintPass<'tcx> for NeedlessPathNew {
42+
pub struct NeedlessPathNew<'tcx> {
43+
path_ty: Option<Ty<'tcx>>,
44+
asref_def_id: Option<DefId>,
45+
}
46+
47+
impl<'tcx> NeedlessPathNew<'tcx> {
48+
pub fn new(tcx: TyCtxt<'tcx>) -> Self {
49+
Self {
50+
path_ty: (tcx.get_diagnostic_item(sym::Path))
51+
.map(|path_def_id| Ty::new_adt(tcx, tcx.adt_def(path_def_id), List::empty())),
52+
asref_def_id: tcx.get_diagnostic_item(sym::AsRef),
53+
}
54+
}
55+
}
56+
57+
impl<'tcx> LateLintPass<'tcx> for NeedlessPathNew<'tcx> {
4258
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>) {
4359
let tcx = cx.tcx;
4460

61+
let Some(path_ty) = self.path_ty else {
62+
return;
63+
};
64+
65+
let Some(asref_def_id) = self.asref_def_id else {
66+
return;
67+
};
68+
4569
let (fn_did, arguments) = match e.kind {
4670
ExprKind::Call(callee, args)
4771
if let Res::Def(DefKind::Fn | DefKind::AssocFn, did) = path_res(cx, callee) =>
@@ -72,17 +96,6 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPathNew {
7296
}
7397
};
7498

75-
let path_ty = {
76-
let Some(path_def_id) = tcx.get_diagnostic_item(sym::Path) else {
77-
return;
78-
};
79-
Ty::new_adt(tcx, tcx.adt_def(path_def_id), List::empty())
80-
};
81-
82-
let Some(asref_def_id) = tcx.get_diagnostic_item(sym::AsRef) else {
83-
return;
84-
};
85-
8699
let implements_asref_path = |arg| implements_trait(cx, arg, asref_def_id, &[path_ty.into()]);
87100

88101
let parameters = sig.inputs();

0 commit comments

Comments
 (0)