Skip to content

Commit bd583d9

Browse files
committed
Factor out is_qpath_def_path
1 parent ece7fa4 commit bd583d9

File tree

4 files changed

+44
-48
lines changed

4 files changed

+44
-48
lines changed

clippy_lints/src/infinite_iter.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint;
22
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
3-
use clippy_utils::{get_trait_def_id, higher, is_qpath_def_path, paths};
3+
use clippy_utils::{get_trait_def_id, higher, match_def_path, path_def_id, paths};
44
use rustc_hir::{BorrowKind, Expr, ExprKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -167,13 +167,9 @@ fn is_infinite(cx: &LateContext<'_>, expr: &Expr<'_>) -> Finiteness {
167167
},
168168
ExprKind::Block(block, _) => block.expr.as_ref().map_or(Finite, |e| is_infinite(cx, e)),
169169
ExprKind::Box(e) | ExprKind::AddrOf(BorrowKind::Ref, _, e) => is_infinite(cx, e),
170-
ExprKind::Call(path, _) => {
171-
if let ExprKind::Path(ref qpath) = path.kind {
172-
is_qpath_def_path(cx, qpath, path.hir_id, &paths::ITER_REPEAT).into()
173-
} else {
174-
Finite
175-
}
176-
},
170+
ExprKind::Call(path, _) => path_def_id(cx, path)
171+
.map_or(false, |id| match_def_path(cx, id, &paths::ITER_REPEAT))
172+
.into(),
177173
ExprKind::Struct(..) => higher::Range::hir(expr).map_or(false, |r| r.end.is_none()).into(),
178174
_ => Finite,
179175
}

clippy_lints/src/matches.rs

+35-27
Original file line numberDiff line numberDiff line change
@@ -1765,22 +1765,22 @@ where
17651765
mod redundant_pattern_match {
17661766
use super::REDUNDANT_PATTERN_MATCHING;
17671767
use clippy_utils::diagnostics::span_lint_and_then;
1768-
use clippy_utils::higher;
17691768
use clippy_utils::source::snippet;
17701769
use clippy_utils::sugg::Sugg;
17711770
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item, is_type_lang_item, match_type};
1772-
use clippy_utils::{is_lang_ctor, is_qpath_def_path, is_trait_method, paths};
1771+
use clippy_utils::{higher, match_def_path};
1772+
use clippy_utils::{is_lang_ctor, is_trait_method, paths};
17731773
use if_chain::if_chain;
17741774
use rustc_ast::ast::LitKind;
17751775
use rustc_data_structures::fx::FxHashSet;
17761776
use rustc_errors::Applicability;
1777-
use rustc_hir::LangItem::{OptionNone, OptionSome, PollPending, PollReady, ResultErr, ResultOk};
1777+
use rustc_hir::LangItem::{OptionNone, PollPending};
17781778
use rustc_hir::{
17791779
intravisit::{walk_expr, Visitor},
17801780
Arm, Block, Expr, ExprKind, LangItem, MatchSource, Node, Pat, PatKind, QPath, UnOp,
17811781
};
17821782
use rustc_lint::LateContext;
1783-
use rustc_middle::ty::{self, subst::GenericArgKind, Ty};
1783+
use rustc_middle::ty::{self, subst::GenericArgKind, DefIdTree, Ty};
17841784
use rustc_span::sym;
17851785

17861786
pub fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
@@ -1956,28 +1956,31 @@ mod redundant_pattern_match {
19561956
has_else: bool,
19571957
) {
19581958
// also look inside refs
1959-
let mut kind = &let_pat.kind;
19601959
// if we have &None for example, peel it so we can detect "if let None = x"
1961-
if let PatKind::Ref(inner, _mutability) = kind {
1962-
kind = &inner.kind;
1963-
}
1960+
let check_pat = match let_pat.kind {
1961+
PatKind::Ref(inner, _mutability) => inner,
1962+
_ => let_pat,
1963+
};
19641964
let op_ty = cx.typeck_results().expr_ty(let_expr);
19651965
// Determine which function should be used, and the type contained by the corresponding
19661966
// variant.
1967-
let (good_method, inner_ty) = match kind {
1968-
PatKind::TupleStruct(ref path, [sub_pat], _) => {
1967+
let (good_method, inner_ty) = match check_pat.kind {
1968+
PatKind::TupleStruct(ref qpath, [sub_pat], _) => {
19691969
if let PatKind::Wild = sub_pat.kind {
1970-
if is_lang_ctor(cx, path, ResultOk) {
1970+
let res = cx.typeck_results().qpath_res(qpath, check_pat.hir_id);
1971+
let Some(id) = res.opt_def_id().and_then(|ctor_id| cx.tcx.parent(ctor_id)) else { return };
1972+
let lang_items = cx.tcx.lang_items();
1973+
if Some(id) == lang_items.result_ok_variant() {
19711974
("is_ok()", try_get_generic_ty(op_ty, 0).unwrap_or(op_ty))
1972-
} else if is_lang_ctor(cx, path, ResultErr) {
1975+
} else if Some(id) == lang_items.result_err_variant() {
19731976
("is_err()", try_get_generic_ty(op_ty, 1).unwrap_or(op_ty))
1974-
} else if is_lang_ctor(cx, path, OptionSome) {
1977+
} else if Some(id) == lang_items.option_some_variant() {
19751978
("is_some()", op_ty)
1976-
} else if is_lang_ctor(cx, path, PollReady) {
1979+
} else if Some(id) == lang_items.poll_ready_variant() {
19771980
("is_ready()", op_ty)
1978-
} else if is_qpath_def_path(cx, path, sub_pat.hir_id, &paths::IPADDR_V4) {
1981+
} else if match_def_path(cx, id, &paths::IPADDR_V4) {
19791982
("is_ipv4()", op_ty)
1980-
} else if is_qpath_def_path(cx, path, sub_pat.hir_id, &paths::IPADDR_V6) {
1983+
} else if match_def_path(cx, id, &paths::IPADDR_V6) {
19811984
("is_ipv6()", op_ty)
19821985
} else {
19831986
return;
@@ -2177,17 +2180,22 @@ mod redundant_pattern_match {
21772180
should_be_left: &'a str,
21782181
should_be_right: &'a str,
21792182
) -> Option<&'a str> {
2180-
let body_node_pair = if is_qpath_def_path(cx, path_left, arms[0].pat.hir_id, expected_left)
2181-
&& is_qpath_def_path(cx, path_right, arms[1].pat.hir_id, expected_right)
2182-
{
2183-
(&(*arms[0].body).kind, &(*arms[1].body).kind)
2184-
} else if is_qpath_def_path(cx, path_right, arms[1].pat.hir_id, expected_left)
2185-
&& is_qpath_def_path(cx, path_left, arms[0].pat.hir_id, expected_right)
2186-
{
2187-
(&(*arms[1].body).kind, &(*arms[0].body).kind)
2188-
} else {
2189-
return None;
2190-
};
2183+
let left_id = cx
2184+
.typeck_results()
2185+
.qpath_res(path_left, arms[0].pat.hir_id)
2186+
.opt_def_id()?;
2187+
let right_id = cx
2188+
.typeck_results()
2189+
.qpath_res(path_right, arms[1].pat.hir_id)
2190+
.opt_def_id()?;
2191+
let body_node_pair =
2192+
if match_def_path(cx, left_id, expected_left) && match_def_path(cx, right_id, expected_right) {
2193+
(&(*arms[0].body).kind, &(*arms[1].body).kind)
2194+
} else if match_def_path(cx, right_id, expected_left) && match_def_path(cx, right_id, expected_right) {
2195+
(&(*arms[1].body).kind, &(*arms[0].body).kind)
2196+
} else {
2197+
return None;
2198+
};
21912199

21922200
match body_node_pair {
21932201
(ExprKind::Lit(ref lit_left), ExprKind::Lit(ref lit_right)) => match (&lit_left.node, &lit_right.node) {

clippy_lints/src/methods/manual_saturating_arithmetic.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::is_qpath_def_path;
32
use clippy_utils::source::snippet_with_applicability;
3+
use clippy_utils::{match_def_path, path_def_id};
44
use if_chain::if_chain;
55
use rustc_ast::ast;
66
use rustc_errors::Applicability;
@@ -93,12 +93,12 @@ fn is_min_or_max<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>) -> Option<M
9393
let ty_str = ty.to_string();
9494

9595
// `std::T::MAX` `std::T::MIN` constants
96-
if let hir::ExprKind::Path(path) = &expr.kind {
97-
if is_qpath_def_path(cx, path, expr.hir_id, &["core", &ty_str, "MAX"][..]) {
96+
if let Some(id) = path_def_id(cx, expr) {
97+
if match_def_path(cx, id, &["core", &ty_str, "MAX"]) {
9898
return Some(MinMax::Max);
9999
}
100100

101-
if is_qpath_def_path(cx, path, expr.hir_id, &["core", &ty_str, "MIN"][..]) {
101+
if match_def_path(cx, id, &["core", &ty_str, "MIN"]) {
102102
return Some(MinMax::Min);
103103
}
104104
}

clippy_utils/src/lib.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -357,13 +357,6 @@ pub fn match_qpath(path: &QPath<'_>, segments: &[&str]) -> bool {
357357
}
358358
}
359359

360-
/// Resolves the path to a `DefId` and checks if it matches the given path.
361-
pub fn is_qpath_def_path(cx: &LateContext<'_>, path: &QPath<'_>, hir_id: HirId, segments: &[&str]) -> bool {
362-
cx.qpath_res(path, hir_id)
363-
.opt_def_id()
364-
.map_or(false, |id| match_def_path(cx, id, segments))
365-
}
366-
367360
/// If the expression is a path, resolves it to a `DefId` and checks if it matches the given path.
368361
///
369362
/// Please use `is_expr_diagnostic_item` if the target is a diagnostic item.
@@ -1775,8 +1768,7 @@ pub fn is_expr_identity_function(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool
17751768

17761769
match expr.kind {
17771770
ExprKind::Closure(_, _, body_id, _, _) => is_body_identity_function(cx, cx.tcx.hir().body(body_id)),
1778-
ExprKind::Path(ref path) => is_qpath_def_path(cx, path, expr.hir_id, &paths::CONVERT_IDENTITY),
1779-
_ => false,
1771+
_ => path_def_id(cx, expr).map_or(false, |id| match_def_path(cx, id, &paths::CONVERT_IDENTITY)),
17801772
}
17811773
}
17821774

0 commit comments

Comments
 (0)