Skip to content

Commit 3f686a0

Browse files
committed
Remove is_res_lang_ctor
1 parent 3ed7aa0 commit 3f686a0

28 files changed

+181
-123
lines changed

clippy_lints/src/if_then_some_else_none.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ use clippy_config::Conf;
22
use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::eager_or_lazy::switch_to_eager_eval;
44
use clippy_utils::msrvs::{self, Msrv};
5-
use clippy_utils::res::MaybeQPath;
5+
use clippy_utils::res::{MaybeDef, MaybeQPath};
66
use clippy_utils::source::{snippet_with_applicability, snippet_with_context, walk_span_to_context};
77
use clippy_utils::sugg::Sugg;
88
use clippy_utils::{
9-
contains_return, expr_adjustment_requires_coercion, higher, is_else_clause, is_in_const_context, is_res_lang_ctor,
10-
peel_blocks, sym,
9+
contains_return, expr_adjustment_requires_coercion, higher, is_else_clause, is_in_const_context, peel_blocks, sym,
1110
};
1211
use rustc_errors::Applicability;
1312
use rustc_hir::LangItem::{OptionNone, OptionSome};
@@ -74,8 +73,8 @@ impl<'tcx> LateLintPass<'tcx> for IfThenSomeElseNone {
7473
&& let ExprKind::Call(then_call, [then_arg]) = then_expr.kind
7574
&& !expr.span.from_expansion()
7675
&& !then_expr.span.from_expansion()
77-
&& is_res_lang_ctor(cx, then_call.res(cx), OptionSome)
78-
&& is_res_lang_ctor(cx, peel_blocks(els).res(cx), OptionNone)
76+
&& then_call.res(cx).ctor_parent(cx).is_lang_item(cx, OptionSome)
77+
&& peel_blocks(els).res(cx).ctor_parent(cx).is_lang_item(cx, OptionNone)
7978
&& !is_else_clause(cx.tcx, expr)
8079
&& !is_in_const_context(cx)
8180
&& self.msrv.meets(cx, msrvs::BOOL_THEN)

clippy_lints/src/loops/manual_find.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use super::MANUAL_FIND;
22
use super::utils::make_iterator_snippet;
33
use clippy_utils::diagnostics::span_lint_and_then;
4-
use clippy_utils::res::{MaybeQPath, MaybeResPath};
4+
use clippy_utils::res::{MaybeDef, MaybeQPath, MaybeResPath};
55
use clippy_utils::source::snippet_with_applicability;
66
use clippy_utils::ty::implements_trait;
77
use clippy_utils::usage::contains_return_break_continue_macro;
8-
use clippy_utils::{higher, is_res_lang_ctor, peel_blocks_with_stmt};
8+
use clippy_utils::{higher, peel_blocks_with_stmt};
99
use rustc_errors::Applicability;
1010
use rustc_hir::lang_items::LangItem;
1111
use rustc_hir::{BindingMode, Block, Expr, ExprKind, HirId, Node, Pat, PatKind, Stmt, StmtKind};
@@ -34,7 +34,7 @@ pub(super) fn check<'tcx>(
3434
&& let StmtKind::Semi(semi) = stmt.kind
3535
&& let ExprKind::Ret(Some(ret_value)) = semi.kind
3636
&& let ExprKind::Call(ctor, [inner_ret]) = ret_value.kind
37-
&& is_res_lang_ctor(cx, ctor.res(cx), LangItem::OptionSome)
37+
&& ctor.res(cx).ctor_parent(cx).is_lang_item(cx, LangItem::OptionSome)
3838
&& inner_ret.res_local_id() == Some(binding_id)
3939
&& !contains_return_break_continue_macro(cond)
4040
&& let Some((last_stmt, last_ret)) = last_stmt_and_ret(cx, expr)
@@ -150,7 +150,7 @@ fn last_stmt_and_ret<'tcx>(
150150
&& let Some((_, Node::Block(block))) = parent_iter.next()
151151
&& let Some((last_stmt, last_ret)) = extract(block)
152152
&& last_stmt.hir_id == node_hir
153-
&& is_res_lang_ctor(cx, last_ret.res(cx), LangItem::OptionNone)
153+
&& last_ret.res(cx).ctor_parent(cx).is_lang_item(cx, LangItem::OptionNone)
154154
&& let Some((_, Node::Expr(_block))) = parent_iter.next()
155155
// This includes the function header
156156
&& let Some((_, func)) = parent_iter.next()

clippy_lints/src/loops/while_let_on_iterator.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ use std::ops::ControlFlow;
22

33
use super::WHILE_LET_ON_ITERATOR;
44
use clippy_utils::diagnostics::span_lint_and_sugg;
5+
use clippy_utils::res::MaybeDef;
56
use clippy_utils::source::snippet_with_applicability;
67
use clippy_utils::visitors::is_res_used;
7-
use clippy_utils::{get_enclosing_loop_or_multi_call_closure, higher, is_refutable, is_res_lang_ctor, is_trait_method};
8+
use clippy_utils::{get_enclosing_loop_or_multi_call_closure, higher, is_refutable, is_trait_method};
89
use rustc_errors::Applicability;
910
use rustc_hir::def::Res;
1011
use rustc_hir::intravisit::{Visitor, walk_expr};
@@ -19,7 +20,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
1920
if let Some(higher::WhileLet { if_then, let_pat, let_expr, label, .. }) = higher::WhileLet::hir(expr)
2021
// check for `Some(..)` pattern
2122
&& let PatKind::TupleStruct(ref pat_path, some_pat, _) = let_pat.kind
22-
&& is_res_lang_ctor(cx, cx.qpath_res(pat_path, let_pat.hir_id), LangItem::OptionSome)
23+
&& cx.qpath_res(pat_path, let_pat.hir_id).ctor_parent(cx).is_lang_item(cx, LangItem::OptionSome)
2324
// check for call to `Iterator::next`
2425
&& let ExprKind::MethodCall(method_name, iter_expr, [], _) = let_expr.kind
2526
&& method_name.ident.name == sym::next

clippy_lints/src/match_result_ok.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::res::MaybeDef;
33
use clippy_utils::source::snippet_with_context;
4-
use clippy_utils::{higher, is_res_lang_ctor, sym};
4+
use clippy_utils::{higher, sym};
55
use rustc_errors::Applicability;
66
use rustc_hir::{Expr, ExprKind, LangItem, PatKind};
77
use rustc_lint::{LateContext, LateLintPass};
@@ -58,7 +58,7 @@ impl<'tcx> LateLintPass<'tcx> for MatchResultOk {
5858
&& let PatKind::TupleStruct(ref pat_path, [ok_pat], _) = let_pat.kind //get operation
5959
&& ok_path.ident.name == sym::ok
6060
&& cx.typeck_results().expr_ty(recv).is_diag_item(cx, sym::Result)
61-
&& is_res_lang_ctor(cx, cx.qpath_res(pat_path, let_pat.hir_id), LangItem::OptionSome)
61+
&& cx.qpath_res(pat_path, let_pat.hir_id).ctor_parent(cx).is_lang_item(cx, LangItem::OptionSome)
6262
&& let ctxt = expr.span.ctxt()
6363
&& let_expr.span.ctxt() == ctxt
6464
&& let_pat.span.ctxt() == ctxt

clippy_lints/src/matches/collapsible_match.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use clippy_utils::diagnostics::span_lint_hir_and_then;
22
use clippy_utils::higher::IfLetOrMatch;
33
use clippy_utils::msrvs::Msrv;
4+
use clippy_utils::res::MaybeDef;
45
use clippy_utils::source::snippet;
56
use clippy_utils::visitors::is_local_used;
67
use clippy_utils::{
7-
SpanlessEq, get_ref_operators, is_res_lang_ctor, is_unit_expr, path_to_local, peel_blocks_with_stmt,
8-
peel_ref_operators,
8+
SpanlessEq, get_ref_operators, is_unit_expr, path_to_local, peel_blocks_with_stmt, peel_ref_operators,
99
};
1010
use rustc_ast::BorrowKind;
1111
use rustc_errors::MultiSpan;
@@ -136,7 +136,10 @@ fn arm_is_wild_like(cx: &LateContext<'_>, arm: &Arm<'_>) -> bool {
136136
kind: PatExprKind::Path(qpath),
137137
hir_id,
138138
..
139-
}) => is_res_lang_ctor(cx, cx.qpath_res(qpath, *hir_id), OptionNone),
139+
}) => cx
140+
.qpath_res(qpath, *hir_id)
141+
.ctor_parent(cx)
142+
.is_lang_item(cx, OptionNone),
140143
_ => false,
141144
}
142145
}

clippy_lints/src/matches/manual_filter.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::path_to_local_id;
23
use clippy_utils::res::{MaybeDef, MaybeQPath};
34
use clippy_utils::visitors::contains_unsafe_block;
4-
use clippy_utils::{is_res_lang_ctor, path_to_local_id};
55

66
use rustc_hir::LangItem::{OptionNone, OptionSome};
77
use rustc_hir::{Arm, Expr, ExprKind, HirId, Pat, PatKind};
@@ -66,15 +66,15 @@ fn is_some_expr(cx: &LateContext<'_>, target: HirId, ctxt: SyntaxContext, expr:
6666
&& let ExprKind::Call(callee, [arg]) = inner_expr.kind
6767
{
6868
return ctxt == expr.span.ctxt()
69-
&& is_res_lang_ctor(cx, callee.res(cx), OptionSome)
69+
&& callee.res(cx).ctor_parent(cx).is_lang_item(cx, OptionSome)
7070
&& path_to_local_id(arg, target);
7171
}
7272
false
7373
}
7474

7575
fn is_none_expr(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
7676
if let Some(inner_expr) = peels_blocks_incl_unsafe_opt(expr) {
77-
return is_res_lang_ctor(cx, inner_expr.res(cx), OptionNone);
77+
return inner_expr.res(cx).ctor_parent(cx).is_lang_item(cx, OptionNone);
7878
}
7979
false
8080
}

clippy_lints/src/matches/manual_map.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ use super::MANUAL_MAP;
22
use super::manual_utils::{SomeExpr, check_with};
33
use clippy_utils::diagnostics::span_lint_and_sugg;
44

5-
use clippy_utils::is_res_lang_ctor;
6-
7-
use clippy_utils::res::MaybeQPath;
5+
use clippy_utils::res::{MaybeDef, MaybeQPath};
86
use rustc_hir::LangItem::OptionSome;
97
use rustc_hir::{Arm, Block, BlockCheckMode, Expr, ExprKind, Pat, UnsafeSource};
108
use rustc_lint::LateContext;
@@ -92,7 +90,7 @@ fn get_some_expr<'tcx>(
9290
// TODO: Allow more complex expressions.
9391
match expr.kind {
9492
ExprKind::Call(callee, [arg])
95-
if ctxt == expr.span.ctxt() && is_res_lang_ctor(cx, callee.res(cx), OptionSome) =>
93+
if ctxt == expr.span.ctxt() && callee.res(cx).ctor_parent(cx).is_lang_item(cx, OptionSome) =>
9694
{
9795
Some(SomeExpr::new_no_negated(arg, needs_unsafe_block))
9896
},

clippy_lints/src/matches/manual_ok_err.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::res::MaybeQPath;
2+
use clippy_utils::res::{MaybeDef, MaybeQPath};
33
use clippy_utils::source::{indent_of, reindent_multiline};
44
use clippy_utils::sugg::Sugg;
55
use clippy_utils::ty::{option_arg_ty, peel_and_count_ty_refs};
6-
use clippy_utils::{get_parent_expr, is_res_lang_ctor, peel_blocks, span_contains_comment};
6+
use clippy_utils::{get_parent_expr, peel_blocks, span_contains_comment};
77
use rustc_ast::{BindingMode, Mutability};
88
use rustc_errors::Applicability;
99
use rustc_hir::LangItem::{OptionNone, OptionSome, ResultErr};
@@ -73,7 +73,10 @@ fn is_variant_or_wildcard(cx: &LateContext<'_>, pat: &Pat<'_>, can_be_wild: bool
7373
true
7474
},
7575
PatKind::TupleStruct(qpath, ..) => {
76-
is_res_lang_ctor(cx, cx.qpath_res(&qpath, pat.hir_id), ResultErr) == must_match_err
76+
cx.qpath_res(&qpath, pat.hir_id)
77+
.ctor_parent(cx)
78+
.is_lang_item(cx, ResultErr)
79+
== must_match_err
7780
},
7881
PatKind::Binding(_, _, _, Some(pat)) | PatKind::Ref(pat, _) => {
7982
is_variant_or_wildcard(cx, pat, can_be_wild, must_match_err)
@@ -104,7 +107,7 @@ fn is_ok_or_err<'hir>(cx: &LateContext<'_>, pat: &Pat<'hir>) -> Option<(bool, &'
104107
/// Check if `expr` contains `Some(ident)`, possibly as a block
105108
fn is_some_ident<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, ident: &Ident, ty: Ty<'tcx>) -> bool {
106109
if let ExprKind::Call(body_callee, [body_arg]) = peel_blocks(expr).kind
107-
&& is_res_lang_ctor(cx, body_callee.res(cx), OptionSome)
110+
&& body_callee.res(cx).ctor_parent(cx).is_lang_item(cx, OptionSome)
108111
&& cx.typeck_results().expr_ty(body_arg) == ty
109112
&& let ExprKind::Path(QPath::Resolved(
110113
_,
@@ -121,7 +124,7 @@ fn is_some_ident<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, ident: &Ident, t
121124

122125
/// Check if `expr` is `None`, possibly as a block
123126
fn is_none(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
124-
is_res_lang_ctor(cx, peel_blocks(expr).res(cx), OptionNone)
127+
peel_blocks(expr).res(cx).ctor_parent(cx).is_lang_item(cx, OptionNone)
125128
}
126129

127130
/// Suggest replacing `expr` by `scrutinee.METHOD()`, where `METHOD` is either `ok` or

clippy_lints/src/matches/manual_utils.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
55
use clippy_utils::sugg::Sugg;
66
use clippy_utils::ty::{is_copy, is_unsafe_fn, peel_and_count_ty_refs};
77
use clippy_utils::{
8-
CaptureKind, can_move_expr_to_closure, expr_requires_coercion, is_else_clause, is_lint_allowed, is_res_lang_ctor,
9-
path_to_local_id, peel_blocks, peel_hir_expr_refs, peel_hir_expr_while,
8+
CaptureKind, can_move_expr_to_closure, expr_requires_coercion, is_else_clause, is_lint_allowed, path_to_local_id,
9+
peel_blocks, peel_hir_expr_refs, peel_hir_expr_while,
1010
};
1111
use rustc_ast::util::parser::ExprPrecedence;
1212
use rustc_errors::Applicability;
@@ -259,9 +259,19 @@ pub(super) fn try_parse_pattern<'tcx>(
259259
kind: PatExprKind::Path(qpath),
260260
hir_id,
261261
..
262-
}) if is_res_lang_ctor(cx, cx.qpath_res(qpath, *hir_id), OptionNone) => Some(OptionPat::None),
262+
}) if cx
263+
.qpath_res(qpath, *hir_id)
264+
.ctor_parent(cx)
265+
.is_lang_item(cx, OptionNone) =>
266+
{
267+
Some(OptionPat::None)
268+
},
263269
PatKind::TupleStruct(ref qpath, [pattern], _)
264-
if is_res_lang_ctor(cx, cx.qpath_res(qpath, pat.hir_id), OptionSome) && pat.span.ctxt() == ctxt =>
270+
if cx
271+
.qpath_res(qpath, pat.hir_id)
272+
.ctor_parent(cx)
273+
.is_lang_item(cx, OptionSome)
274+
&& pat.span.ctxt() == ctxt =>
265275
{
266276
Some(OptionPat::Some { pattern, ref_count })
267277
},
@@ -273,5 +283,5 @@ pub(super) fn try_parse_pattern<'tcx>(
273283

274284
// Checks for the `None` value.
275285
fn is_none_expr(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
276-
is_res_lang_ctor(cx, peel_blocks(expr).res(cx), OptionNone)
286+
peel_blocks(expr).res(cx).ctor_parent(cx).is_lang_item(cx, OptionNone)
277287
}

clippy_lints/src/matches/match_as_ref.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::res::MaybeQPath;
2+
use clippy_utils::res::{MaybeDef, MaybeQPath};
33
use clippy_utils::source::snippet_with_applicability;
4-
use clippy_utils::{is_none_arm, is_res_lang_ctor, peel_blocks};
4+
use clippy_utils::{is_none_arm, peel_blocks};
55
use rustc_errors::Applicability;
66
use rustc_hir::{Arm, BindingMode, ByRef, Expr, ExprKind, LangItem, Mutability, PatKind, QPath};
77
use rustc_lint::LateContext;
@@ -59,10 +59,13 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr:
5959
// Checks if arm has the form `Some(ref v) => Some(v)` (checks for `ref` and `ref mut`)
6060
fn is_ref_some_arm(cx: &LateContext<'_>, arm: &Arm<'_>) -> Option<Mutability> {
6161
if let PatKind::TupleStruct(ref qpath, [first_pat, ..], _) = arm.pat.kind
62-
&& is_res_lang_ctor(cx, cx.qpath_res(qpath, arm.pat.hir_id), LangItem::OptionSome)
62+
&& cx
63+
.qpath_res(qpath, arm.pat.hir_id)
64+
.ctor_parent(cx)
65+
.is_lang_item(cx, LangItem::OptionSome)
6366
&& let PatKind::Binding(BindingMode(ByRef::Yes(mutabl), _), .., ident, _) = first_pat.kind
6467
&& let ExprKind::Call(e, [arg]) = peel_blocks(arm.body).kind
65-
&& is_res_lang_ctor(cx, e.res(cx), LangItem::OptionSome)
68+
&& e.res(cx).ctor_parent(cx).is_lang_item(cx, LangItem::OptionSome)
6669
&& let ExprKind::Path(QPath::Resolved(_, path2)) = arg.kind
6770
&& path2.segments.len() == 1
6871
&& ident.name == path2.segments[0].ident.name

0 commit comments

Comments
 (0)