Skip to content

Commit 1c8e658

Browse files
committed
Use LanguageItems::require less
1 parent a6180ed commit 1c8e658

File tree

16 files changed

+34
-45
lines changed

16 files changed

+34
-45
lines changed

compiler/rustc_hir_analysis/src/astconv/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use rustc_hir as hir;
2323
use rustc_hir::def::{CtorOf, DefKind, Namespace, Res};
2424
use rustc_hir::def_id::{DefId, LocalDefId};
2525
use rustc_hir::intravisit::{walk_generics, Visitor as _};
26-
use rustc_hir::lang_items::LangItem;
2726
use rustc_hir::{GenericArg, GenericArgs, OpaqueTyOrigin};
2827
use rustc_middle::middle::stability::AllowUnstable;
2928
use rustc_middle::ty::subst::{self, GenericArgKind, InternalSubsts, SubstsRef};
@@ -884,9 +883,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
884883
}
885884
}
886885

887-
let sized_def_id = tcx.lang_items().require(LangItem::Sized);
886+
let sized_def_id = tcx.lang_items().sized_trait();
888887
match (&sized_def_id, unbound) {
889-
(Ok(sized_def_id), Some(tpb))
888+
(Some(sized_def_id), Some(tpb))
890889
if tpb.path.res == Res::Def(DefKind::Trait, *sized_def_id) =>
891890
{
892891
// There was in fact a `?Sized` bound, return without doing anything
@@ -906,7 +905,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
906905
// There was no `?Sized` bound; add implicitly sized if `Sized` is available.
907906
}
908907
}
909-
if sized_def_id.is_err() {
908+
if sized_def_id.is_none() {
910909
// No lang item for `Sized`, so we can't add it as a bound.
911910
return;
912911
}

compiler/rustc_middle/src/ty/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2456,7 +2456,7 @@ impl<'tcx> TyCtxt<'tcx> {
24562456

24572457
#[inline]
24582458
pub fn mk_lang_item(self, ty: Ty<'tcx>, item: LangItem) -> Option<Ty<'tcx>> {
2459-
let def_id = self.lang_items().require(item).ok()?;
2459+
let def_id = self.lang_items().get(item)?;
24602460
Some(self.mk_generic_adt(def_id, ty))
24612461
}
24622462

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
10191019
let mut never_suggest_borrow: Vec<_> =
10201020
[LangItem::Copy, LangItem::Clone, LangItem::Unpin, LangItem::Sized]
10211021
.iter()
1022-
.filter_map(|lang_item| self.tcx.lang_items().require(*lang_item).ok())
1022+
.filter_map(|lang_item| self.tcx.lang_items().get(*lang_item))
10231023
.collect();
10241024

10251025
if let Some(def_id) = self.tcx.get_diagnostic_item(sym::Send) {

src/tools/clippy/clippy_lints/src/lifetimes.rs

Lines changed: 5 additions & 10 deletions
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);

src/tools/clippy/clippy_lints/src/manual_retain.rs

Lines changed: 1 addition & 1 deletion
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);

src/tools/clippy/clippy_lints/src/methods/bind_instead_of_map.rs

Lines changed: 4 additions & 4 deletions
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
}

src/tools/clippy/clippy_lints/src/methods/unnecessary_iter_cloned.rs

Lines changed: 2 additions & 2 deletions
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
}

src/tools/clippy/clippy_lints/src/methods/unnecessary_to_owned.rs

Lines changed: 2 additions & 2 deletions
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

src/tools/clippy/clippy_lints/src/operators/assign_op_pattern.rs

Lines changed: 1 addition & 1 deletion
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);

src/tools/clippy/clippy_lints/src/suspicious_trait_impl.rs

Lines changed: 3 additions & 3 deletions
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(

0 commit comments

Comments
 (0)