Skip to content

Commit 16566e9

Browse files
committed
Use LanguageItems::require less
1 parent dff041f commit 16566e9

13 files changed

+29
-39
lines changed

clippy_lints/src/lifetimes.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ use rustc_hir::intravisit::{
66
walk_fn_decl, walk_generic_param, walk_generics, walk_impl_item_ref, walk_item, walk_param_bound,
77
walk_poly_trait_ref, walk_trait_ref, walk_ty, Visitor,
88
};
9+
use rustc_hir::lang_items;
910
use rustc_hir::FnRetTy::Return;
1011
use rustc_hir::{
1112
BareFnTy, BodyId, FnDecl, GenericArg, GenericBound, GenericParam, GenericParamKind, Generics, Impl, ImplItem,
12-
ImplItemKind, Item, ItemKind, LangItem, Lifetime, LifetimeName, ParamName, PolyTraitRef, PredicateOrigin, TraitFn,
13-
TraitItem, TraitItemKind, Ty, TyKind, WherePredicate,
13+
ImplItemKind, Item, ItemKind, Lifetime, LifetimeName, ParamName, PolyTraitRef, PredicateOrigin, TraitFn, TraitItem,
14+
TraitItemKind, Ty, TyKind, WherePredicate,
1415
};
1516
use rustc_lint::{LateContext, LateLintPass};
1617
use rustc_middle::hir::nested_filter as middle_nested_filter;
@@ -364,8 +365,6 @@ fn unique_lifetimes(lts: &[RefLt]) -> usize {
364365
lts.iter().collect::<FxHashSet<_>>().len()
365366
}
366367

367-
const CLOSURE_TRAIT_BOUNDS: [LangItem; 3] = [LangItem::Fn, LangItem::FnMut, LangItem::FnOnce];
368-
369368
/// A visitor usable for `rustc_front::visit::walk_ty()`.
370369
struct RefVisitor<'a, 'tcx> {
371370
cx: &'a LateContext<'tcx>,
@@ -424,12 +423,8 @@ impl<'a, 'tcx> Visitor<'tcx> for RefVisitor<'a, 'tcx> {
424423

425424
fn visit_poly_trait_ref(&mut self, poly_tref: &'tcx PolyTraitRef<'tcx>) {
426425
let trait_ref = &poly_tref.trait_ref;
427-
if CLOSURE_TRAIT_BOUNDS.iter().any(|&item| {
428-
self.cx
429-
.tcx
430-
.lang_items()
431-
.require(item)
432-
.map_or(false, |id| Some(id) == trait_ref.trait_def_id())
426+
if let Some(id) = trait_ref.trait_def_id() && lang_items::FN_TRAITS.iter().any(|&item| {
427+
self.cx.tcx.lang_items().get(item) == Some(id)
433428
}) {
434429
let mut sub_visitor = RefVisitor::new(self.cx);
435430
sub_visitor.visit_trait_ref(trait_ref);

clippy_lints/src/manual_retain.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ fn check_into_iter(
9292
&& match_def_path(cx, filter_def_id, &paths::CORE_ITER_FILTER)
9393
&& let hir::ExprKind::MethodCall(_, struct_expr, [], _) = &into_iter_expr.kind
9494
&& let Some(into_iter_def_id) = cx.typeck_results().type_dependent_def_id(into_iter_expr.hir_id)
95-
&& cx.tcx.lang_items().require(hir::LangItem::IntoIterIntoIter).ok() == Some(into_iter_def_id)
95+
&& Some(into_iter_def_id) == cx.tcx.lang_items().into_iter_fn()
9696
&& match_acceptable_type(cx, left_expr, msrv)
9797
&& SpanlessEq::new(cx).eq_expr(left_expr, struct_expr) {
9898
suggest(cx, parent_expr, left_expr, target_expr);

clippy_lints/src/methods/bind_instead_of_map.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub(crate) trait BindInsteadOfMap {
4141
const GOOD_METHOD_NAME: &'static str;
4242

4343
fn no_op_msg(cx: &LateContext<'_>) -> Option<String> {
44-
let variant_id = cx.tcx.lang_items().require(Self::VARIANT_LANG_ITEM).ok()?;
44+
let variant_id = cx.tcx.lang_items().get(Self::VARIANT_LANG_ITEM)?;
4545
let item_id = cx.tcx.parent(variant_id);
4646
Some(format!(
4747
"using `{}.{}({})`, which is a no-op",
@@ -52,7 +52,7 @@ pub(crate) trait BindInsteadOfMap {
5252
}
5353

5454
fn lint_msg(cx: &LateContext<'_>) -> Option<String> {
55-
let variant_id = cx.tcx.lang_items().require(Self::VARIANT_LANG_ITEM).ok()?;
55+
let variant_id = cx.tcx.lang_items().get(Self::VARIANT_LANG_ITEM)?;
5656
let item_id = cx.tcx.parent(variant_id);
5757
Some(format!(
5858
"using `{}.{}(|x| {}(y))`, which is more succinctly expressed as `{}(|x| y)`",
@@ -144,7 +144,7 @@ pub(crate) trait BindInsteadOfMap {
144144
fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, arg: &hir::Expr<'_>) -> bool {
145145
if_chain! {
146146
if let Some(adt) = cx.typeck_results().expr_ty(recv).ty_adt_def();
147-
if let Ok(vid) = cx.tcx.lang_items().require(Self::VARIANT_LANG_ITEM);
147+
if let Some(vid) = cx.tcx.lang_items().get(Self::VARIANT_LANG_ITEM);
148148
if adt.did() == cx.tcx.parent(vid);
149149
then {} else { return false; }
150150
}
@@ -181,7 +181,7 @@ pub(crate) trait BindInsteadOfMap {
181181

182182
fn is_variant(cx: &LateContext<'_>, res: Res) -> bool {
183183
if let Res::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Fn), id) = res {
184-
if let Ok(variant_id) = cx.tcx.lang_items().require(Self::VARIANT_LANG_ITEM) {
184+
if let Some(variant_id) = cx.tcx.lang_items().get(Self::VARIANT_LANG_ITEM) {
185185
return cx.tcx.parent(id) == variant_id;
186186
}
187187
}

clippy_lints/src/methods/unnecessary_iter_cloned.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clippy_utils::source::snippet_opt;
55
use clippy_utils::ty::{get_associated_type, get_iterator_item_ty, implements_trait};
66
use clippy_utils::{fn_def_id, get_parent_expr};
77
use rustc_errors::Applicability;
8-
use rustc_hir::{def_id::DefId, Expr, ExprKind, LangItem};
8+
use rustc_hir::{def_id::DefId, Expr, ExprKind};
99
use rustc_lint::LateContext;
1010
use rustc_span::{sym, Symbol};
1111

@@ -100,5 +100,5 @@ pub fn check_for_loop_iter(
100100

101101
/// Returns true if the named method is `IntoIterator::into_iter`.
102102
pub fn is_into_iter(cx: &LateContext<'_>, callee_def_id: DefId) -> bool {
103-
cx.tcx.lang_items().require(LangItem::IntoIterIntoIter) == Ok(callee_def_id)
103+
Some(callee_def_id) == cx.tcx.lang_items().into_iter_fn()
104104
}

clippy_lints/src/methods/unnecessary_to_owned.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use clippy_utils::visitors::find_all_ret_expressions;
77
use clippy_utils::{fn_def_id, get_parent_expr, is_diag_item_method, is_diag_trait_item, return_ty};
88
use clippy_utils::{meets_msrv, msrvs};
99
use rustc_errors::Applicability;
10-
use rustc_hir::{def_id::DefId, BorrowKind, Expr, ExprKind, ItemKind, LangItem, Node};
10+
use rustc_hir::{def_id::DefId, BorrowKind, Expr, ExprKind, ItemKind, Node};
1111
use rustc_hir_typeck::{FnCtxt, Inherited};
1212
use rustc_infer::infer::TyCtxtInferExt;
1313
use rustc_lint::LateContext;
@@ -378,7 +378,7 @@ fn can_change_type<'a>(cx: &LateContext<'a>, mut expr: &'a Expr<'a>, mut ty: Ty<
378378
Node::Expr(parent_expr) => {
379379
if let Some((callee_def_id, call_substs, recv, call_args)) = get_callee_substs_and_args(cx, parent_expr)
380380
{
381-
if cx.tcx.lang_items().require(LangItem::IntoFutureIntoFuture) == Ok(callee_def_id) {
381+
if Some(callee_def_id) == cx.tcx.lang_items().into_future_fn() {
382382
return false;
383383
}
384384

clippy_lints/src/operators/assign_op_pattern.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub(super) fn check<'tcx>(
2828
let rty = cx.typeck_results().expr_ty(rhs);
2929
if_chain! {
3030
if let Some((_, lang_item)) = binop_traits(op.node);
31-
if let Ok(trait_id) = cx.tcx.lang_items().require(lang_item);
31+
if let Some(trait_id) = cx.tcx.lang_items().get(lang_item);
3232
let parent_fn = cx.tcx.hir().get_parent_item(e.hir_id).def_id;
3333
if trait_ref_of_method(cx, parent_fn)
3434
.map_or(true, |t| t.path.res.def_id() != trait_id);

clippy_lints/src/suspicious_trait_impl.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ impl<'tcx> LateLintPass<'tcx> for SuspiciousImpl {
6060
if_chain! {
6161
if let hir::ExprKind::Binary(binop, _, _) | hir::ExprKind::AssignOp(binop, ..) = expr.kind;
6262
if let Some((binop_trait_lang, op_assign_trait_lang)) = binop_traits(binop.node);
63-
if let Ok(binop_trait_id) = cx.tcx.lang_items().require(binop_trait_lang);
64-
if let Ok(op_assign_trait_id) = cx.tcx.lang_items().require(op_assign_trait_lang);
63+
if let Some(binop_trait_id) = cx.tcx.lang_items().get(binop_trait_lang);
64+
if let Some(op_assign_trait_id) = cx.tcx.lang_items().get(op_assign_trait_lang);
6565

6666
// Check for more than one binary operation in the implemented function
6767
// Linting when multiple operations are involved can result in false positives
@@ -78,7 +78,7 @@ impl<'tcx> LateLintPass<'tcx> for SuspiciousImpl {
7878
(&OP_ASSIGN_TRAITS, SUSPICIOUS_OP_ASSIGN_IMPL),
7979
]
8080
.iter()
81-
.find(|&(ts, _)| ts.iter().any(|&t| Ok(trait_id) == cx.tcx.lang_items().require(t)));
81+
.find(|&(ts, _)| ts.iter().any(|&t| Some(trait_id) == cx.tcx.lang_items().get(t)));
8282
if count_binops(body.value) == 1;
8383
then {
8484
span_lint(

clippy_lints/src/unnecessary_owned_empty_strings.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::{match_def_path, paths};
33
use if_chain::if_chain;
44
use rustc_ast::ast::LitKind;
55
use rustc_errors::Applicability;
6-
use rustc_hir::{BorrowKind, Expr, ExprKind, LangItem, Mutability};
6+
use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability};
77
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_middle::ty;
99
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -55,7 +55,7 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryOwnedEmptyStrings {
5555
);
5656
} else {
5757
if_chain! {
58-
if cx.tcx.lang_items().require(LangItem::FromFrom).ok() == Some(fun_def_id);
58+
if Some(fun_def_id) == cx.tcx.lang_items().from_fn();
5959
if let [.., last_arg] = args;
6060
if let ExprKind::Lit(spanned) = &last_arg.kind;
6161
if let LitKind::Str(symbol, _) = spanned.node;

clippy_lints/src/unused_peekable.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use clippy_utils::ty::{match_type, peel_mid_ty_refs_is_mutable};
33
use clippy_utils::{fn_def_id, is_trait_method, path_to_local_id, paths, peel_ref_operators};
44
use rustc_ast::Mutability;
55
use rustc_hir::intravisit::{walk_expr, Visitor};
6-
use rustc_hir::lang_items::LangItem;
76
use rustc_hir::{Block, Expr, ExprKind, HirId, Local, Node, PatKind, PathSegment, StmtKind};
87
use rustc_lint::{LateContext, LateLintPass};
98
use rustc_middle::hir::nested_filter::OnlyBodies;
@@ -132,11 +131,11 @@ impl<'tcx> Visitor<'tcx> for PeekableVisitor<'_, 'tcx> {
132131
// If the Peekable is passed to a function, stop
133132
ExprKind::Call(_, args) => {
134133
if let Some(func_did) = fn_def_id(self.cx, expr)
135-
&& let Ok(into_iter_did) = self
134+
&& let Some(into_iter_did) = self
136135
.cx
137136
.tcx
138137
.lang_items()
139-
.require(LangItem::IntoIterIntoIter)
138+
.into_iter_fn()
140139
&& func_did == into_iter_did
141140
{
142141
// Probably a for loop desugar, stop searching

clippy_lints/src/useless_conversion.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clippy_utils::ty::{is_type_diagnostic_item, same_type_and_consts};
55
use clippy_utils::{get_parent_expr, is_trait_method, match_def_path, paths};
66
use if_chain::if_chain;
77
use rustc_errors::Applicability;
8-
use rustc_hir::{Expr, ExprKind, HirId, LangItem, MatchSource};
8+
use rustc_hir::{Expr, ExprKind, HirId, MatchSource};
99
use rustc_lint::{LateContext, LateLintPass};
1010
use rustc_middle::ty;
1111
use rustc_session::{declare_tool_lint, impl_lint_pass};
@@ -153,7 +153,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
153153
}
154154

155155
if_chain! {
156-
if cx.tcx.lang_items().require(LangItem::FromFrom).ok() == Some(def_id);
156+
if Some(def_id) == cx.tcx.lang_items().from_fn();
157157
if same_type_and_consts(a, b);
158158

159159
then {

clippy_lints/src/utils/internal_lints/unnecessary_def_path.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl UnnecessaryDefPath {
152152
has_ctor,
153153
),
154154
(0, Item::LangItem(item)) => (
155-
format!("{cx_snip}.tcx.lang_items().require(LangItem::{item}).ok() == Some({def_snip})"),
155+
format!("{cx_snip}.tcx.lang_items().get(LangItem::{item}) == Some({def_snip})"),
156156
has_ctor,
157157
),
158158
// match_trait_method
@@ -184,7 +184,7 @@ impl UnnecessaryDefPath {
184184
(3, Item::LangItem(item)) => (
185185
format!(
186186
"path_res({cx_snip}, {def_snip}).opt_def_id()\
187-
.map_or(false, |id| {cx_snip}.tcx.lang_items().require(LangItem::{item}).ok() == Some(id))",
187+
.map_or(false, |id| {cx_snip}.tcx.lang_items().get(LangItem::{item}) == Some(id))",
188188
),
189189
false,
190190
),

clippy_utils/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ pub fn in_constant(cx: &LateContext<'_>, id: HirId) -> bool {
247247
/// For example, use this to check whether a function call or a pattern is `Some(..)`.
248248
pub fn is_res_lang_ctor(cx: &LateContext<'_>, res: Res, lang_item: LangItem) -> bool {
249249
if let Res::Def(DefKind::Ctor(..), id) = res
250-
&& let Ok(lang_id) = cx.tcx.lang_items().require(lang_item)
250+
&& let Some(lang_id) = cx.tcx.lang_items().get(lang_item)
251251
&& let Some(id) = cx.tcx.opt_parent(id)
252252
{
253253
id == lang_id
@@ -303,7 +303,7 @@ pub fn is_lang_item_or_ctor(cx: &LateContext<'_>, did: DefId, item: LangItem) ->
303303
_ => did,
304304
};
305305

306-
cx.tcx.lang_items().require(item).map_or(false, |id| id == did)
306+
cx.tcx.lang_items().get(item) == Some(did)
307307
}
308308

309309
pub fn is_unit_expr(expr: &Expr<'_>) -> bool {

clippy_utils/src/ty.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,7 @@ pub fn is_type_diagnostic_item(cx: &LateContext<'_>, ty: Ty<'_>, diag_item: Symb
318318
/// Returns `false` if the `LangItem` is not defined.
319319
pub fn is_type_lang_item(cx: &LateContext<'_>, ty: Ty<'_>, lang_item: hir::LangItem) -> bool {
320320
match ty.kind() {
321-
ty::Adt(adt, _) => cx
322-
.tcx
323-
.lang_items()
324-
.require(lang_item)
325-
.map_or(false, |li| li == adt.did()),
321+
ty::Adt(adt, _) => cx.tcx.lang_items().get(lang_item) == Some(adt.did()),
326322
_ => false,
327323
}
328324
}

0 commit comments

Comments
 (0)