Skip to content

Commit 6dd09e1

Browse files
committed
normalize_erasing_regions -> try_normalize_erasing_regions
1 parent 7e18f4f commit 6dd09e1

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

clippy_lints/src/dereference.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,9 +1150,10 @@ fn replace_types<'tcx>(
11501150
let assoc_item = cx.tcx.associated_item(item_def_id);
11511151
let projection = cx.tcx
11521152
.mk_projection(assoc_item.def_id, cx.tcx.mk_substs_trait(new_ty, &[]));
1153-
let projected_ty = cx.tcx.normalize_erasing_regions(cx.param_env, projection);
11541153

1155-
if substs[term_param_ty.index as usize] != ty::GenericArg::from(projected_ty) {
1154+
if let Ok(projected_ty) = cx.tcx.try_normalize_erasing_regions(cx.param_env, projection)
1155+
&& substs[term_param_ty.index as usize] != ty::GenericArg::from(projected_ty)
1156+
{
11561157
deque.push_back((*term_param_ty, projected_ty));
11571158
}
11581159
}

tests/ui/needless_borrow.fixed

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ fn main() {
139139
let _ = std::any::Any::type_id(&""); // Don't lint. `Any` is only bound
140140
let _ = Box::new(&""); // Don't lint. Type parameter appears in return type
141141
ref_as_ref_path(&""); // Don't lint. Argument type is not a type parameter
142+
refs_only(&()); // Don't lint. `&T` implements trait, but `T` doesn't
142143
multiple_constraints_normalizes_to_different(&[[""]], &[""]); // Don't lint. Projected type appears in arguments
143144
}
144145

@@ -238,6 +239,20 @@ where
238239
{
239240
}
240241

242+
trait RefsOnly {
243+
type Referent;
244+
}
245+
246+
impl<T> RefsOnly for &T {
247+
type Referent = T;
248+
}
249+
250+
fn refs_only<T, U>(_: T)
251+
where
252+
T: RefsOnly<Referent = U>,
253+
{
254+
}
255+
241256
fn multiple_constraints_normalizes_to_different<T, U, V>(_: T, _: U)
242257
where
243258
T: IntoIterator<Item = U>,

tests/ui/needless_borrow.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ fn main() {
139139
let _ = std::any::Any::type_id(&""); // Don't lint. `Any` is only bound
140140
let _ = Box::new(&""); // Don't lint. Type parameter appears in return type
141141
ref_as_ref_path(&""); // Don't lint. Argument type is not a type parameter
142+
refs_only(&()); // Don't lint. `&T` implements trait, but `T` doesn't
142143
multiple_constraints_normalizes_to_different(&[[""]], &[""]); // Don't lint. Projected type appears in arguments
143144
}
144145

@@ -238,6 +239,20 @@ where
238239
{
239240
}
240241

242+
trait RefsOnly {
243+
type Referent;
244+
}
245+
246+
impl<T> RefsOnly for &T {
247+
type Referent = T;
248+
}
249+
250+
fn refs_only<T, U>(_: T)
251+
where
252+
T: RefsOnly<Referent = U>,
253+
{
254+
}
255+
241256
fn multiple_constraints_normalizes_to_different<T, U, V>(_: T, _: U)
242257
where
243258
T: IntoIterator<Item = U>,

tests/ui/needless_borrow.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,19 +157,19 @@ LL | let _ = Some("").unwrap_or(&"");
157157
| ^^^ help: change this to: `""`
158158

159159
error: this expression borrows a value the compiler would automatically borrow
160-
--> $DIR/needless_borrow.rs:186:13
160+
--> $DIR/needless_borrow.rs:187:13
161161
|
162162
LL | (&self.f)()
163163
| ^^^^^^^^^ help: change this to: `(self.f)`
164164

165165
error: this expression borrows a value the compiler would automatically borrow
166-
--> $DIR/needless_borrow.rs:195:13
166+
--> $DIR/needless_borrow.rs:196:13
167167
|
168168
LL | (&mut self.f)()
169169
| ^^^^^^^^^^^^^ help: change this to: `(self.f)`
170170

171171
error: the borrowed expression implements the required traits
172-
--> $DIR/needless_borrow.rs:283:55
172+
--> $DIR/needless_borrow.rs:298:55
173173
|
174174
LL | let _ = std::process::Command::new("ls").args(&["-a", "-l"]).status().unwrap();
175175
| ^^^^^^^^^^^^^ help: change this to: `["-a", "-l"]`

0 commit comments

Comments
 (0)