Skip to content

Commit e79b979

Browse files
committed
Auto merge of #85740 - flip1995:clippy_backport, r=Mark-Simulacrum
[Beta] Backport ICE fix in Clippy This backports an ICE / stack overflow fix from rust-lang/rust-clippy#7170. cc rust-lang/rust-clippy#7169 cc `@llogiq` `@camsteffen` r? `@Mark-Simulacrum`
2 parents e7f4b8b + 47e2265 commit e79b979

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

src/tools/clippy/clippy_lints/src/matches.rs

+12-4
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
}
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+
}
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)