Skip to content

Commit 182a185

Browse files
committed
Auto merge of #7170 - flip1995:revert_drop_order, r=llogiq
Fix stack overflow issue in `redundant_pattern_matching` Fixes #7169 ~~cc `@Jarcho` Since tomorrow is release day and we need to get this also fixed in beta, I'll just revert the PR instead of looking into the root issue. Your changes are good, so if you have an idea what could cause this stack overflow and know how to fix it, please open a PR that reverts this revert with a fix.~~ r? `@llogiq` changelog: none (fixes stack overflow, but this was introduced in this release cycle)
2 parents f35345d + 59874f3 commit 182a185

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

clippy_lints/src/matches.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,6 +1712,7 @@ mod redundant_pattern_match {
17121712
use clippy_utils::{is_lang_ctor, is_qpath_def_path, is_trait_method, paths};
17131713
use if_chain::if_chain;
17141714
use rustc_ast::ast::LitKind;
1715+
use rustc_data_structures::fx::FxHashSet;
17151716
use rustc_errors::Applicability;
17161717
use rustc_hir::LangItem::{OptionNone, OptionSome, PollPending, PollReady, ResultErr, ResultOk};
17171718
use rustc_hir::{
@@ -1739,6 +1740,13 @@ mod redundant_pattern_match {
17391740
/// deallocate memory. For these types, and composites containing them, changing the drop order
17401741
/// won't result in any observable side effects.
17411742
fn type_needs_ordered_drop(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
1743+
type_needs_ordered_drop_inner(cx, ty, &mut FxHashSet::default())
1744+
}
1745+
1746+
fn type_needs_ordered_drop_inner(cx: &LateContext<'tcx>, ty: Ty<'tcx>, seen: &mut FxHashSet<Ty<'tcx>>) -> bool {
1747+
if !seen.insert(ty) {
1748+
return false;
1749+
}
17421750
if !ty.needs_drop(cx.tcx, cx.param_env) {
17431751
false
17441752
} else if !cx
@@ -1750,12 +1758,12 @@ mod redundant_pattern_match {
17501758
// This type doesn't implement drop, so no side effects here.
17511759
// Check if any component type has any.
17521760
match ty.kind() {
1753-
ty::Tuple(_) => ty.tuple_fields().any(|ty| type_needs_ordered_drop(cx, ty)),
1754-
ty::Array(ty, _) => type_needs_ordered_drop(cx, ty),
1761+
ty::Tuple(_) => ty.tuple_fields().any(|ty| type_needs_ordered_drop_inner(cx, ty, seen)),
1762+
ty::Array(ty, _) => type_needs_ordered_drop_inner(cx, ty, seen),
17551763
ty::Adt(adt, subs) => adt
17561764
.all_fields()
17571765
.map(|f| f.ty(cx.tcx, subs))
1758-
.any(|ty| type_needs_ordered_drop(cx, ty)),
1766+
.any(|ty| type_needs_ordered_drop_inner(cx, ty, seen)),
17591767
_ => true,
17601768
}
17611769
}
@@ -1772,7 +1780,7 @@ mod redundant_pattern_match {
17721780
{
17731781
// Check all of the generic arguments.
17741782
if let ty::Adt(_, subs) = ty.kind() {
1775-
subs.types().any(|ty| type_needs_ordered_drop(cx, ty))
1783+
subs.types().any(|ty| type_needs_ordered_drop_inner(cx, ty, seen))
17761784
} else {
17771785
true
17781786
}

tests/ui/crashes/ice-7169.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#[derive(Default)]
2+
struct A<T> {
3+
a: Vec<A<T>>,
4+
b: T,
5+
}
6+
7+
fn main() {
8+
if let Ok(_) = Ok::<_, ()>(A::<String>::default()) {}
9+
}

tests/ui/crashes/ice-7169.stderr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: redundant pattern matching, consider using `is_ok()`
2+
--> $DIR/ice-7169.rs:8:12
3+
|
4+
LL | if let Ok(_) = Ok::<_, ()>(A::<String>::default()) {}
5+
| -------^^^^^-------------------------------------- help: try this: `if Ok::<_, ()>(A::<String>::default()).is_ok()`
6+
|
7+
= note: `-D clippy::redundant-pattern-matching` implied by `-D warnings`
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)