Skip to content

Commit e2b3508

Browse files
committed
Remove QPath::LangItem from try
1 parent 930dfc8 commit e2b3508

File tree

7 files changed

+28
-25
lines changed

7 files changed

+28
-25
lines changed

clippy_lints/src/matches/try_err.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clippy_utils::source::snippet_with_applicability;
55
use clippy_utils::ty::option_arg_ty;
66
use rustc_errors::Applicability;
77
use rustc_hir::LangItem::ResultErr;
8-
use rustc_hir::{Expr, ExprKind, LangItem, MatchSource, QPath};
8+
use rustc_hir::{Expr, ExprKind, LangItem, MatchSource};
99
use rustc_lint::LateContext;
1010
use rustc_middle::ty::{self, Ty};
1111
use rustc_span::{hygiene, sym};
@@ -23,8 +23,8 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, scrutine
2323
// val,
2424
// };
2525
if let ExprKind::Call(match_fun, [try_arg]) = scrutinee.kind
26-
&& let ExprKind::Path(ref match_fun_path) = match_fun.kind
27-
&& matches!(match_fun_path, QPath::LangItem(LangItem::TryTraitBranch, ..))
26+
&& let ExprKind::Path(match_fun_path) = match_fun.kind
27+
&& cx.tcx.qpath_is_lang_item(match_fun_path, LangItem::TryTraitBranch)
2828
&& let ExprKind::Call(err_fun, [err_arg]) = try_arg.kind
2929
&& err_fun.res(cx).ctor_parent(cx).is_lang_item(cx, ResultErr)
3030
&& let Some(return_ty) = find_return_type(cx, &expr.kind)

clippy_lints/src/methods/clone_on_copy.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_with_context;
33
use clippy_utils::ty::is_copy;
44
use rustc_errors::Applicability;
5-
use rustc_hir::{BindingMode, ByRef, Expr, ExprKind, MatchSource, Node, PatKind, QPath};
5+
use rustc_hir::{BindingMode, ByRef, Expr, ExprKind, MatchSource, Node, PatKind};
66
use rustc_lint::LateContext;
77
use rustc_middle::ty;
88
use rustc_middle::ty::adjustment::Adjust;
@@ -47,7 +47,8 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>)
4747
// ? is a Call, makes sure not to rec *x?, but rather (*x)?
4848
ExprKind::Call(hir_callee, [_]) => matches!(
4949
hir_callee.kind,
50-
ExprKind::Path(QPath::LangItem(rustc_hir::LangItem::TryTraitBranch, ..))
50+
ExprKind::Path(qpath)
51+
if cx.tcx.qpath_is_lang_item(qpath, rustc_hir::LangItem::TryTraitBranch)
5152
),
5253
ExprKind::MethodCall(_, self_arg, ..) if expr.hir_id == self_arg.hir_id => true,
5354
ExprKind::Match(_, _, MatchSource::TryDesugar(_) | MatchSource::AwaitDesugar)

clippy_lints/src/methods/str_splitn.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use clippy_utils::{paths, sym};
99
use core::ops::ControlFlow;
1010
use rustc_errors::Applicability;
1111
use rustc_hir::{
12-
BindingMode, Expr, ExprKind, HirId, LangItem, LetStmt, MatchSource, Node, Pat, PatKind, QPath, Stmt, StmtKind,
12+
BindingMode, Expr, ExprKind, HirId, LangItem, LetStmt, MatchSource, Node, Pat, PatKind, Stmt, StmtKind,
1313
};
1414
use rustc_lint::LateContext;
1515
use rustc_middle::ty;
@@ -332,12 +332,12 @@ fn parse_iter_usage<'tcx>(
332332
let (unwrap_kind, span) = if let Some((_, Node::Expr(e))) = iter.next() {
333333
match e.kind {
334334
ExprKind::Call(
335-
Expr {
336-
kind: ExprKind::Path(QPath::LangItem(LangItem::TryTraitBranch, ..)),
335+
&Expr {
336+
kind: ExprKind::Path(qpath),
337337
..
338338
},
339339
[_],
340-
) => {
340+
) if cx.tcx.qpath_is_lang_item(qpath, LangItem::TryTraitBranch) => {
341341
let parent_span = e.span.parent_callsite().unwrap();
342342
if parent_span.ctxt() == ctxt {
343343
(Some(UnwrapKind::QuestionMark), parent_span)

clippy_lints/src/needless_question_mark.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_hir_and_then;
22
use clippy_utils::res::MaybeQPath;
33
use rustc_errors::Applicability;
44
use rustc_hir::def::{DefKind, Res};
5-
use rustc_hir::{Block, Body, Expr, ExprKind, LangItem, MatchSource, QPath};
5+
use rustc_hir::{Block, Body, Expr, ExprKind, LangItem, MatchSource};
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_session::declare_lint_pass;
88

@@ -105,7 +105,8 @@ fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
105105
}
106106
&& let ExprKind::Match(inner_expr_with_q, _, MatchSource::TryDesugar(_)) = &arg.kind
107107
&& let ExprKind::Call(called, [inner_expr]) = &inner_expr_with_q.kind
108-
&& let ExprKind::Path(QPath::LangItem(LangItem::TryTraitBranch, ..)) = &called.kind
108+
&& let ExprKind::Path(qpath) = called.kind
109+
&& cx.tcx.qpath_is_lang_item(qpath, LangItem::TryTraitBranch)
109110
&& expr.span.eq_ctxt(inner_expr.span)
110111
&& let expr_ty = cx.typeck_results().expr_ty(expr)
111112
&& let inner_ty = cx.typeck_results().expr_ty(inner_expr)

clippy_lints/src/question_mark.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -530,11 +530,13 @@ impl QuestionMark {
530530
}
531531
}
532532

533-
fn is_try_block(bl: &Block<'_>) -> bool {
533+
fn is_try_block(cx: &LateContext<'_>, bl: &Block<'_>) -> bool {
534534
if let Some(expr) = bl.expr
535535
&& let ExprKind::Call(callee, [_]) = expr.kind
536+
&& let ExprKind::Path(qpath) = callee.kind
537+
&& cx.tcx.qpath_is_lang_item(qpath, LangItem::TryTraitFromOutput)
536538
{
537-
callee.opt_lang_path() == Some(LangItem::TryTraitFromOutput)
539+
true
538540
} else {
539541
false
540542
}
@@ -590,8 +592,8 @@ impl<'tcx> LateLintPass<'tcx> for QuestionMark {
590592
}
591593
}
592594

593-
fn check_block(&mut self, _: &LateContext<'tcx>, block: &'tcx Block<'tcx>) {
594-
if is_try_block(block) {
595+
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) {
596+
if is_try_block(cx, block) {
595597
*self
596598
.try_block_depth_stack
597599
.last_mut()
@@ -607,8 +609,8 @@ impl<'tcx> LateLintPass<'tcx> for QuestionMark {
607609
self.try_block_depth_stack.pop();
608610
}
609611

610-
fn check_block_post(&mut self, _: &LateContext<'tcx>, block: &'tcx Block<'tcx>) {
611-
if is_try_block(block) {
612+
fn check_block_post(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) {
613+
if is_try_block(cx, block) {
612614
*self
613615
.try_block_depth_stack
614616
.last_mut()

clippy_lints/src/returns/needless_return.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use clippy_utils::{
77
use rustc_ast::MetaItemInner;
88
use rustc_errors::Applicability;
99
use rustc_hir::intravisit::FnKind;
10-
use rustc_hir::{Body, Expr, ExprKind, HirId, LangItem, MatchSource, QPath, StmtKind};
10+
use rustc_hir::{Body, Expr, ExprKind, HirId, LangItem, MatchSource, StmtKind};
1111
use rustc_lint::{LateContext, Level, LintContext};
1212
use rustc_middle::ty::{self, Ty};
1313
use rustc_span::{BytePos, Pos, Span};
@@ -134,7 +134,8 @@ fn check_final_expr<'tcx>(
134134
let replacement = if let Some(inner_expr) = inner {
135135
// if desugar of `do yeet`, don't lint
136136
if let ExprKind::Call(path_expr, [_]) = inner_expr.kind
137-
&& let ExprKind::Path(QPath::LangItem(LangItem::TryTraitFromYeet, ..)) = path_expr.kind
137+
&& let ExprKind::Path(qpath) = path_expr.kind
138+
&& cx.tcx.qpath_is_lang_item(qpath, LangItem::TryTraitFromYeet)
138139
{
139140
return;
140141
}

clippy_lints/src/unused_io_amount.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ fn check_expr<'a>(cx: &LateContext<'a>, expr: &'a hir::Expr<'a>) {
192192

193193
fn should_lint<'a>(cx: &LateContext<'a>, mut inner: &'a hir::Expr<'a>) -> Option<IoOp> {
194194
inner = unpack_match(inner);
195-
inner = unpack_try(inner);
195+
inner = unpack_try(cx, inner);
196196
inner = unpack_call_chain(inner);
197197
inner = unpack_await(cx, inner);
198198
// we type-check it to get whether it's a read/write or their vectorized forms
@@ -256,12 +256,10 @@ fn unpack_call_chain<'a>(mut expr: &'a hir::Expr<'a>) -> &'a hir::Expr<'a> {
256256
expr
257257
}
258258

259-
fn unpack_try<'a>(mut expr: &'a hir::Expr<'a>) -> &'a hir::Expr<'a> {
259+
fn unpack_try<'a>(cx: &LateContext<'_>, mut expr: &'a hir::Expr<'a>) -> &'a hir::Expr<'a> {
260260
while let ExprKind::Call(func, [arg_0]) = expr.kind
261-
&& matches!(
262-
func.kind,
263-
ExprKind::Path(hir::QPath::LangItem(hir::LangItem::TryTraitBranch, ..))
264-
)
261+
&& let ExprKind::Path(qpath) = func.kind
262+
&& cx.tcx.qpath_is_lang_item(qpath, hir::LangItem::TryTraitBranch)
265263
{
266264
expr = arg_0;
267265
}

0 commit comments

Comments
 (0)