Skip to content

Commit c0bdb3d

Browse files
committed
Auto merge of #11258 - Jarcho:ice_11256, r=Centri3
Resolve type aliases in `type_certainty` Fixes #11256 changelog: `unwrap_or_default`: Fix ICE when local types are declared using alias types
2 parents 4c2f460 + caf6014 commit c0bdb3d

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

clippy_utils/src/ty/type_certainty/mod.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//! be considered a bug.
1313
1414
use crate::def_path_res;
15-
use rustc_hir::def::Res;
15+
use rustc_hir::def::{DefKind, Res};
1616
use rustc_hir::def_id::DefId;
1717
use rustc_hir::intravisit::{walk_qpath, walk_ty, Visitor};
1818
use rustc_hir::{self as hir, Expr, ExprKind, GenericArgs, HirId, Node, PathSegment, QPath, TyKind};
@@ -219,7 +219,12 @@ fn path_segment_certainty(
219219
// See the comment preceding `qpath_certainty`. `def_id` could refer to a type or a value.
220220
let certainty = lhs.join_clearing_def_ids(rhs);
221221
if resolves_to_type {
222-
certainty.with_def_id(def_id)
222+
if cx.tcx.def_kind(def_id) == DefKind::TyAlias {
223+
adt_def_id(cx.tcx.type_of(def_id).instantiate_identity())
224+
.map_or(certainty, |def_id| certainty.with_def_id(def_id))
225+
} else {
226+
certainty.with_def_id(def_id)
227+
}
223228
} else {
224229
certainty
225230
}

tests/ui/unwrap_or_else_default.fixed

+4
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ fn type_certainty(option: Option<Vec<u64>>) {
109109
// should not be changed: no type annotation, unconcretized initializer
110110
let option = None;
111111
option.unwrap_or_else(Vec::new).push(1);
112+
113+
type Alias = Option<Vec<u32>>;
114+
let option: Alias = Option::<Vec<u32>>::Some(Vec::new());
115+
option.unwrap_or_default().push(1);
112116
}
113117

114118
fn method_call_with_deref() {

tests/ui/unwrap_or_else_default.rs

+4
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ fn type_certainty(option: Option<Vec<u64>>) {
109109
// should not be changed: no type annotation, unconcretized initializer
110110
let option = None;
111111
option.unwrap_or_else(Vec::new).push(1);
112+
113+
type Alias = Option<Vec<u32>>;
114+
let option: Alias = Option::<Vec<u32>>::Some(Vec::new());
115+
option.unwrap_or_else(Vec::new).push(1);
112116
}
113117

114118
fn method_call_with_deref() {

tests/ui/unwrap_or_else_default.stderr

+8-2
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,17 @@ error: use of `unwrap_or_else` to construct default value
8484
LL | option.unwrap_or_else(Vec::new).push(1);
8585
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
8686

87+
error: use of `unwrap_or_else` to construct default value
88+
--> $DIR/unwrap_or_else_default.rs:115:12
89+
|
90+
LL | option.unwrap_or_else(Vec::new).push(1);
91+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
92+
8793
error: use of `or_insert_with` to construct default value
88-
--> $DIR/unwrap_or_else_default.rs:128:32
94+
--> $DIR/unwrap_or_else_default.rs:132:32
8995
|
9096
LL | let _ = inner_map.entry(0).or_insert_with(Default::default);
9197
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `or_default()`
9298

93-
error: aborting due to 15 previous errors
99+
error: aborting due to 16 previous errors
94100

0 commit comments

Comments
 (0)