Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 8172166

Browse files
committed
rustc: Panic by default in DefIdTree::parent
Only crate root def-ids don't have a parent, and in majority of cases the argument of `DefIdTree::parent` cannot be a crate root. So we now panic by default in `parent` and introduce a new non-panicing function `opt_parent` for cases where the argument can be a crate root. Same applies to `local_parent`/`opt_local_parent`.
1 parent defc537 commit 8172166

File tree

6 files changed

+9
-9
lines changed

6 files changed

+9
-9
lines changed

clippy_lints/src/matches/redundant_pattern_match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ fn find_sugg_for_if_let<'tcx>(
193193
PatKind::TupleStruct(ref qpath, [sub_pat], _) => {
194194
if let PatKind::Wild = sub_pat.kind {
195195
let res = cx.typeck_results().qpath_res(qpath, check_pat.hir_id);
196-
let Some(id) = res.opt_def_id().and_then(|ctor_id| cx.tcx.parent(ctor_id)) else { return };
196+
let Some(id) = res.opt_def_id().map(|ctor_id| cx.tcx.parent(ctor_id)) else { return };
197197
let lang_items = cx.tcx.lang_items();
198198
if Some(id) == lang_items.result_ok_variant() {
199199
("is_ok()", try_get_generic_ty(op_ty, 0).unwrap_or(op_ty))

clippy_lints/src/methods/bind_instead_of_map.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub(crate) trait BindInsteadOfMap {
4242

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

5454
fn lint_msg(cx: &LateContext<'_>) -> Option<String> {
5555
let variant_id = cx.tcx.lang_items().require(Self::VARIANT_LANG_ITEM).ok()?;
56-
let item_id = cx.tcx.parent(variant_id)?;
56+
let item_id = cx.tcx.parent(variant_id);
5757
Some(format!(
5858
"using `{}.{}(|x| {}(y))`, which is more succinctly expressed as `{}(|x| y)`",
5959
cx.tcx.item_name(item_id),
@@ -145,7 +145,7 @@ pub(crate) trait BindInsteadOfMap {
145145
if_chain! {
146146
if let Some(adt) = cx.typeck_results().expr_ty(recv).ty_adt_def();
147147
if let Ok(vid) = cx.tcx.lang_items().require(Self::VARIANT_LANG_ITEM);
148-
if Some(adt.did()) == cx.tcx.parent(vid);
148+
if adt.did() == cx.tcx.parent(vid);
149149
then {} else { return false; }
150150
}
151151

@@ -182,7 +182,7 @@ pub(crate) trait BindInsteadOfMap {
182182
fn is_variant(cx: &LateContext<'_>, res: Res) -> bool {
183183
if let Res::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Fn), id) = res {
184184
if let Ok(variant_id) = cx.tcx.lang_items().require(Self::VARIANT_LANG_ITEM) {
185-
return cx.tcx.parent(id) == Some(variant_id);
185+
return cx.tcx.parent(id) == variant_id;
186186
}
187187
}
188188
false

clippy_lints/src/methods/chars_cmp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub(super) fn check(
1919
if_chain! {
2020
if let Some(args) = method_chain_args(info.chain, chain_methods);
2121
if let hir::ExprKind::Call(fun, [arg_char]) = info.other.kind;
22-
if let Some(id) = path_def_id(cx, fun).and_then(|ctor_id| cx.tcx.parent(ctor_id));
22+
if let Some(id) = path_def_id(cx, fun).map(|ctor_id| cx.tcx.parent(ctor_id));
2323
if Some(id) == cx.tcx.lang_items().option_some_variant();
2424
then {
2525
let mut applicability = Applicability::MachineApplicable;

clippy_lints/src/methods/option_map_or_none.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub(super) fn check<'tcx>(
7575
let arg_snippet = snippet(cx, span, "..");
7676
let body = cx.tcx.hir().body(id);
7777
if let Some((func, [arg_char])) = reduce_unit_expression(&body.value);
78-
if let Some(id) = path_def_id(cx, func).and_then(|ctor_id| cx.tcx.parent(ctor_id));
78+
if let Some(id) = path_def_id(cx, func).map(|ctor_id| cx.tcx.parent(ctor_id));
7979
if Some(id) == cx.tcx.lang_items().option_some_variant();
8080
then {
8181
let func_snippet = snippet(cx, arg_char.span, "..");

clippy_lints/src/missing_doc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
114114
hir::ItemKind::Fn(..) => {
115115
// ignore main()
116116
if it.ident.name == sym::main {
117-
let at_root = cx.tcx.local_parent(it.def_id) == Some(CRATE_DEF_ID);
117+
let at_root = cx.tcx.local_parent(it.def_id) == CRATE_DEF_ID;
118118
if at_root {
119119
return;
120120
}

clippy_utils/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ pub fn is_lang_ctor(cx: &LateContext<'_>, qpath: &QPath<'_>, lang_item: LangItem
235235
if let QPath::Resolved(_, path) = qpath {
236236
if let Res::Def(DefKind::Ctor(..), ctor_id) = path.res {
237237
if let Ok(item_id) = cx.tcx.lang_items().require(lang_item) {
238-
return cx.tcx.parent(ctor_id) == Some(item_id);
238+
return cx.tcx.parent(ctor_id) == item_id;
239239
}
240240
}
241241
}

0 commit comments

Comments
 (0)