Skip to content

Commit 53783de

Browse files
committed
Remove MaybePath
1 parent e1a4c90 commit 53783de

File tree

6 files changed

+48
-96
lines changed

6 files changed

+48
-96
lines changed

clippy_lints/src/manual_clamp.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use clippy_utils::sugg::Sugg;
77
use clippy_utils::ty::implements_trait;
88
use clippy_utils::visitors::is_const_evaluatable;
99
use clippy_utils::{
10-
MaybePath, eq_expr_value, is_diag_trait_item, is_in_const_context, is_trait_method, path_res, path_to_local_id,
11-
peel_blocks, peel_blocks_with_stmt, sym,
10+
eq_expr_value, is_diag_trait_item, is_in_const_context, is_trait_method, path_res, path_to_local_id, peel_blocks,
11+
peel_blocks_with_stmt, sym,
1212
};
1313
use itertools::Itertools;
1414
use rustc_errors::{Applicability, Diag};
@@ -516,7 +516,7 @@ fn is_two_if_pattern<'tcx>(cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) ->
516516
},
517517
span: first_expr.span.to(second_expr.span),
518518
make_assignment: Some(maybe_input_first_path),
519-
hir_with_ignore_attr: Some(first_expr.hir_id()),
519+
hir_with_ignore_attr: Some(first_expr.hir_id),
520520
})
521521
} else {
522522
None

clippy_lints/src/manual_let_else.rs

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@ use crate::question_mark::{QUESTION_MARK, QuestionMark};
22
use clippy_config::types::MatchLintBehaviour;
33
use clippy_utils::diagnostics::span_lint_and_then;
44
use clippy_utils::higher::IfLetOrMatch;
5-
use clippy_utils::res::MaybeDef;
5+
use clippy_utils::res::{MaybeDef, MaybeQPath};
66
use clippy_utils::source::snippet_with_context;
7-
use clippy_utils::{
8-
MaybePath, is_lint_allowed, is_never_expr, is_wild, msrvs, pat_and_expr_can_be_question_mark, path_res, peel_blocks,
9-
};
7+
use clippy_utils::{is_lint_allowed, is_never_expr, is_wild, msrvs, pat_and_expr_can_be_question_mark, peel_blocks};
108
use rustc_ast::BindingMode;
119
use rustc_data_structures::fx::FxHashMap;
1210
use rustc_errors::Applicability;
1311
use rustc_hir::def::{CtorOf, DefKind, Res};
14-
use rustc_hir::{Arm, Expr, ExprKind, HirId, MatchSource, Pat, PatExpr, PatExprKind, PatKind, QPath, Stmt, StmtKind};
12+
use rustc_hir::{Arm, Expr, ExprKind, MatchSource, Pat, PatExpr, PatExprKind, PatKind, QPath, Stmt, StmtKind};
1513
use rustc_lint::{LateContext, LintContext};
1614
use rustc_span::Span;
1715
use rustc_span::symbol::{Symbol, sym};
@@ -131,39 +129,25 @@ fn is_arms_disjointed(cx: &LateContext<'_>, arm1: &Arm<'_>, arm2: &Arm<'_>) -> b
131129

132130
/// Returns `true` if the given pattern is a variant of an enum.
133131
pub fn is_enum_variant(cx: &LateContext<'_>, pat: &Pat<'_>) -> bool {
134-
struct Pat<'hir>(&'hir rustc_hir::Pat<'hir>);
135-
136-
impl<'hir> MaybePath<'hir> for Pat<'hir> {
137-
fn qpath_opt(&self) -> Option<&QPath<'hir>> {
138-
match self.0.kind {
139-
PatKind::Struct(ref qpath, fields, _)
140-
if fields
141-
.iter()
142-
.all(|field| is_wild(field.pat) || matches!(field.pat.kind, PatKind::Binding(..))) =>
143-
{
144-
Some(qpath)
145-
},
146-
PatKind::TupleStruct(ref qpath, pats, _)
147-
if pats
148-
.iter()
149-
.all(|pat| is_wild(pat) || matches!(pat.kind, PatKind::Binding(..))) =>
150-
{
151-
Some(qpath)
152-
},
153-
PatKind::Expr(&PatExpr {
154-
kind: PatExprKind::Path(ref qpath),
155-
..
156-
}) => Some(qpath),
157-
_ => None,
158-
}
159-
}
160-
161-
fn hir_id(&self) -> HirId {
162-
self.0.hir_id
163-
}
164-
}
165-
166-
let res = path_res(cx, &Pat(pat));
132+
let path = match pat.kind {
133+
PatKind::Struct(ref qpath, fields, _)
134+
if fields
135+
.iter()
136+
.all(|field| is_wild(field.pat) || matches!(field.pat.kind, PatKind::Binding(..))) =>
137+
{
138+
(qpath, pat.hir_id)
139+
},
140+
PatKind::TupleStruct(ref qpath, pats, _)
141+
if pats
142+
.iter()
143+
.all(|pat| is_wild(pat) || matches!(pat.kind, PatKind::Binding(..))) =>
144+
{
145+
(qpath, pat.hir_id)
146+
},
147+
PatKind::Expr(e) if let Some((qpath, id)) = e.opt_qpath() => (qpath, id),
148+
_ => return false,
149+
};
150+
let res = path.res(cx);
167151
matches!(
168152
res,
169153
Res::Def(DefKind::Variant, ..) | Res::Def(DefKind::Ctor(CtorOf::Variant, _), _)

clippy_lints/src/methods/unnecessary_literal_unwrap.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2-
use clippy_utils::{MaybePath, is_res_lang_ctor, last_path_segment, path_res, sym};
2+
use clippy_utils::res::MaybeQPath;
3+
use clippy_utils::{is_res_lang_ctor, last_path_segment, path_res, sym};
34
use rustc_errors::Applicability;
45
use rustc_hir::{self as hir, AmbigArg};
56
use rustc_lint::LateContext;
@@ -37,10 +38,12 @@ pub(super) fn check(
3738
}
3839

3940
let (constructor, call_args, ty) = if let hir::ExprKind::Call(call, call_args) = init.kind {
40-
let Some(qpath) = call.qpath_opt() else { return };
41+
let Some((qpath, hir_id)) = call.opt_qpath() else {
42+
return;
43+
};
4144

4245
let args = last_path_segment(qpath).args.map(|args| args.args);
43-
let res = cx.qpath_res(qpath, call.hir_id());
46+
let res = cx.qpath_res(qpath, hir_id);
4447

4548
if is_res_lang_ctor(cx, res, hir::LangItem::OptionSome) {
4649
(sym::Some, call_args, get_ty_from_args(args, 0))

clippy_lints/src/utils/author.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use clippy_utils::{MaybePath, get_attr, higher, path_def_id, sym};
1+
use clippy_utils::res::MaybeQPath;
2+
use clippy_utils::{get_attr, higher, path_def_id, sym};
23
use itertools::Itertools;
34
use rustc_ast::LitIntType;
45
use rustc_ast::ast::{LitFloatType, LitKind};
@@ -268,16 +269,16 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
268269
chain!(self, "{symbol}.as_str() == {:?}", symbol.value.as_str());
269270
}
270271

271-
fn qpath<'p>(&self, qpath: &Binding<&QPath<'_>>, has_hir_id: &Binding<&impl MaybePath<'p>>) {
272+
fn qpath(&self, qpath: &Binding<&QPath<'_>>, hir_id_binding: &str, hir_id: HirId) {
272273
if let QPath::LangItem(lang_item, ..) = *qpath.value {
273274
chain!(self, "matches!({qpath}, QPath::LangItem(LangItem::{lang_item:?}, _))");
274-
} else if let Some(def_id) = self.cx.qpath_res(qpath.value, has_hir_id.value.hir_id()).opt_def_id()
275+
} else if let Some(def_id) = self.cx.qpath_res(qpath.value, hir_id).opt_def_id()
275276
&& !def_id.is_local()
276277
{
277278
bind!(self, def_id);
278279
chain!(
279280
self,
280-
"let Some({def_id}) = cx.qpath_res({qpath}, {has_hir_id}.hir_id).opt_def_id()"
281+
"let Some({def_id}) = cx.qpath_res({qpath}, {hir_id_binding}.hir_id).opt_def_id()"
281282
);
282283
if let Some(name) = self.cx.tcx.get_diagnostic_name(def_id.value) {
283284
chain!(self, "cx.tcx.is_diagnostic_item(sym::{name}, {def_id})");
@@ -291,7 +292,7 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
291292
}
292293
}
293294

294-
fn maybe_path<'p>(&self, path: &Binding<&impl MaybePath<'p>>) {
295+
fn maybe_path<'p>(&self, path: &Binding<impl MaybeQPath<'p>>) {
295296
if let Some(id) = path_def_id(self.cx, path.value)
296297
&& !id.is_local()
297298
{
@@ -671,7 +672,7 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
671672
StructTailExpr::None | StructTailExpr::DefaultFields(_) => None,
672673
});
673674
kind!("Struct({qpath}, {fields}, {base})");
674-
self.qpath(qpath, expr);
675+
self.qpath(qpath, &expr.name, expr.value.hir_id);
675676
self.slice(fields, |field| {
676677
self.ident(field!(field.ident));
677678
self.expr(field!(field.expr));
@@ -757,7 +758,7 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
757758
let ignore = etc.is_some();
758759
bind!(self, qpath, fields);
759760
kind!("Struct(ref {qpath}, {fields}, {ignore})");
760-
self.qpath(qpath, pat);
761+
self.qpath(qpath, &pat.name, pat.value.hir_id);
761762
self.slice(fields, |field| {
762763
self.ident(field!(field.ident));
763764
self.pat(field!(field.pat));
@@ -771,7 +772,7 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
771772
PatKind::TupleStruct(ref qpath, fields, skip_pos) => {
772773
bind!(self, qpath, fields);
773774
kind!("TupleStruct(ref {qpath}, {fields}, {skip_pos:?})");
774-
self.qpath(qpath, pat);
775+
self.qpath(qpath, &pat.name, pat.value.hir_id);
775776
self.slice(fields, |pat| self.pat(pat));
776777
},
777778
PatKind::Tuple(fields, skip_pos) => {

clippy_utils/src/lib.rs

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ use crate::ast_utils::unordered_over;
132132
use crate::consts::{ConstEvalCtxt, Constant};
133133
use crate::higher::Range;
134134
use crate::msrvs::Msrv;
135-
use crate::res::{MaybeDef, MaybeResPath};
135+
use crate::res::{MaybeDef, MaybeQPath, MaybeResPath};
136136
use crate::ty::{adt_and_variant_of_res, can_partially_move_ty, expr_sig, is_copy, is_recursively_primitive_type};
137137
use crate::visitors::for_each_expr_without_closures;
138138

@@ -471,53 +471,16 @@ pub fn path_to_local_with_projections(expr: &Expr<'_>) -> Option<HirId> {
471471
}
472472
}
473473

474-
pub trait MaybePath<'hir> {
475-
fn hir_id(&self) -> HirId;
476-
fn qpath_opt(&self) -> Option<&QPath<'hir>>;
477-
}
478-
479-
macro_rules! maybe_path {
480-
($ty:ident, $kind:ident) => {
481-
impl<'hir> MaybePath<'hir> for hir::$ty<'hir> {
482-
fn hir_id(&self) -> HirId {
483-
self.hir_id
484-
}
485-
fn qpath_opt(&self) -> Option<&QPath<'hir>> {
486-
match &self.kind {
487-
hir::$kind::Path(qpath) => Some(qpath),
488-
_ => None,
489-
}
490-
}
491-
}
492-
};
493-
}
494-
maybe_path!(Expr, ExprKind);
495-
impl<'hir> MaybePath<'hir> for Pat<'hir> {
496-
fn hir_id(&self) -> HirId {
497-
self.hir_id
498-
}
499-
fn qpath_opt(&self) -> Option<&QPath<'hir>> {
500-
match &self.kind {
501-
PatKind::Expr(PatExpr {
502-
kind: PatExprKind::Path(qpath),
503-
..
504-
}) => Some(qpath),
505-
_ => None,
506-
}
507-
}
508-
}
509-
maybe_path!(Ty, TyKind);
510-
511474
/// If `maybe_path` is a path node, resolves it, otherwise returns `Res::Err`
512-
pub fn path_res<'tcx>(cx: &LateContext<'_>, maybe_path: &impl MaybePath<'tcx>) -> Res {
513-
match maybe_path.qpath_opt() {
475+
pub fn path_res<'tcx>(cx: &LateContext<'_>, maybe_path: impl MaybeQPath<'tcx>) -> Res {
476+
match maybe_path.opt_qpath() {
514477
None => Res::Err,
515-
Some(qpath) => cx.qpath_res(qpath, maybe_path.hir_id()),
478+
Some((qpath, id)) => cx.qpath_res(qpath, id),
516479
}
517480
}
518481

519482
/// If `maybe_path` is a path node which resolves to an item, retrieves the item ID
520-
pub fn path_def_id<'tcx>(cx: &LateContext<'_>, maybe_path: &impl MaybePath<'tcx>) -> Option<DefId> {
483+
pub fn path_def_id<'tcx>(cx: &LateContext<'_>, maybe_path: impl MaybeQPath<'tcx>) -> Option<DefId> {
521484
path_res(cx, maybe_path).opt_def_id()
522485
}
523486

clippy_utils/src/paths.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
//! Whenever possible, please consider diagnostic items over hardcoded paths.
55
//! See <https://github.com/rust-lang/rust-clippy/issues/5393> for more information.
66
7-
use crate::{MaybePath, path_def_id, sym};
7+
use crate::res::MaybeQPath;
8+
use crate::{path_def_id, sym};
89
use rustc_ast::Mutability;
910
use rustc_data_structures::fx::FxHashMap;
1011
use rustc_hir::def::Namespace::{MacroNS, TypeNS, ValueNS};
@@ -96,7 +97,7 @@ impl PathLookup {
9697
}
9798

9899
/// Resolves `maybe_path` to a [`DefId`] and checks if the [`PathLookup`] matches it
99-
pub fn matches_path<'tcx>(&self, cx: &LateContext<'_>, maybe_path: &impl MaybePath<'tcx>) -> bool {
100+
pub fn matches_path<'tcx>(&self, cx: &LateContext<'_>, maybe_path: impl MaybeQPath<'tcx>) -> bool {
100101
path_def_id(cx, maybe_path).is_some_and(|def_id| self.matches(cx, def_id))
101102
}
102103

0 commit comments

Comments
 (0)