Skip to content

Commit 8c60012

Browse files
Normalize substs before resolving instance in NoopMethodCall lint
1 parent f83e026 commit 8c60012

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

compiler/rustc_lint/src/noop_method_call.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
4646
};
4747
// We only care about method calls corresponding to the `Clone`, `Deref` and `Borrow`
4848
// traits and ignore any other method call.
49-
let (trait_id, did) = match cx.typeck_results().type_dependent_def(expr.hir_id) {
49+
let did = match cx.typeck_results().type_dependent_def(expr.hir_id) {
5050
// Verify we are dealing with a method/associated function.
5151
Some((DefKind::AssocFn, did)) => match cx.tcx.trait_of_item(did) {
5252
// Check that we're dealing with a trait method for one of the traits we care about.
@@ -56,21 +56,22 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
5656
Some(sym::Borrow | sym::Clone | sym::Deref)
5757
) =>
5858
{
59-
(trait_id, did)
59+
did
6060
}
6161
_ => return,
6262
},
6363
_ => return,
6464
};
65-
let substs = cx.typeck_results().node_substs(expr.hir_id);
65+
let substs = cx
66+
.tcx
67+
.normalize_erasing_regions(cx.param_env, cx.typeck_results().node_substs(expr.hir_id));
6668
if substs.needs_subst() {
6769
// We can't resolve on types that require monomorphization, so we don't handle them if
6870
// we need to perform substitution.
6971
return;
7072
}
71-
let param_env = cx.tcx.param_env(trait_id);
7273
// Resolve the trait method instance.
73-
let Ok(Some(i)) = ty::Instance::resolve(cx.tcx, param_env, did, substs) else {
74+
let Ok(Some(i)) = ty::Instance::resolve(cx.tcx, cx.param_env, did, substs) else {
7475
return
7576
};
7677
// (Re)check that it implements the noop diagnostic.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// check-pass
2+
// Checks that the NoopMethodCall lint doesn't call Instance::resolve on unresolved consts
3+
4+
#![feature(generic_const_exprs)]
5+
#![allow(incomplete_features)]
6+
7+
#[derive(Debug, Clone)]
8+
pub struct Aes128CipherKey([u8; Aes128Cipher::KEY_LEN]);
9+
10+
impl Aes128CipherKey {
11+
pub fn new(key: &[u8; Aes128Cipher::KEY_LEN]) -> Self {
12+
Self(key.clone())
13+
}
14+
}
15+
16+
#[derive(Debug, Clone)]
17+
pub struct Aes128Cipher;
18+
19+
impl Aes128Cipher {
20+
const KEY_LEN: usize = 16;
21+
}
22+
23+
fn main() {}

0 commit comments

Comments
 (0)