Skip to content

Commit d89d214

Browse files
authored
Rollup merge of #102489 - compiler-errors:issue-102074, r=oli-obk
Normalize substs before resolving instance in `NoopMethodCall` lint Fixes #102074 r? types
2 parents f24d00d + e1b313a commit d89d214

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

Diff for: compiler/rustc_lint/src/noop_method_call.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::context::LintContext;
2-
use crate::rustc_middle::ty::TypeVisitable;
32
use crate::LateContext;
43
use crate::LateLintPass;
54
use rustc_errors::fluent;
@@ -46,7 +45,7 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
4645
};
4746
// We only care about method calls corresponding to the `Clone`, `Deref` and `Borrow`
4847
// traits and ignore any other method call.
49-
let (trait_id, did) = match cx.typeck_results().type_dependent_def(expr.hir_id) {
48+
let did = match cx.typeck_results().type_dependent_def(expr.hir_id) {
5049
// Verify we are dealing with a method/associated function.
5150
Some((DefKind::AssocFn, did)) => match cx.tcx.trait_of_item(did) {
5251
// Check that we're dealing with a trait method for one of the traits we care about.
@@ -56,21 +55,17 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
5655
Some(sym::Borrow | sym::Clone | sym::Deref)
5756
) =>
5857
{
59-
(trait_id, did)
58+
did
6059
}
6160
_ => return,
6261
},
6362
_ => return,
6463
};
65-
let substs = cx.typeck_results().node_substs(expr.hir_id);
66-
if substs.needs_subst() {
67-
// We can't resolve on types that require monomorphization, so we don't handle them if
68-
// we need to perform substitution.
69-
return;
70-
}
71-
let param_env = cx.tcx.param_env(trait_id);
64+
let substs = cx
65+
.tcx
66+
.normalize_erasing_regions(cx.param_env, cx.typeck_results().node_substs(expr.hir_id));
7267
// Resolve the trait method instance.
73-
let Ok(Some(i)) = ty::Instance::resolve(cx.tcx, param_env, did, substs) else {
68+
let Ok(Some(i)) = ty::Instance::resolve(cx.tcx, cx.param_env, did, substs) else {
7469
return
7570
};
7671
// (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() {}

Diff for: src/test/ui/lint/noop-method-call.rs

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ fn main() {
4646

4747
fn generic<T>(non_clone_type: &PlainType<T>) {
4848
non_clone_type.clone();
49+
//~^ WARNING call to `.clone()` on a reference in this situation does nothing
4950
}
5051

5152
fn non_generic(non_clone_type: &PlainType<u32>) {

Diff for: src/test/ui/lint/noop-method-call.stderr

+10-2
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,20 @@ LL | let non_borrow_type_borrow: &PlainType<u32> = non_borrow_type.borrow();
2828
= note: the type `&PlainType<u32>` which `borrow` is being called on is the same as the type returned from `borrow`, so the method call does not do anything and can be removed
2929

3030
warning: call to `.clone()` on a reference in this situation does nothing
31-
--> $DIR/noop-method-call.rs:52:19
31+
--> $DIR/noop-method-call.rs:48:19
32+
|
33+
LL | non_clone_type.clone();
34+
| ^^^^^^^^ unnecessary method call
35+
|
36+
= note: the type `&PlainType<T>` which `clone` is being called on is the same as the type returned from `clone`, so the method call does not do anything and can be removed
37+
38+
warning: call to `.clone()` on a reference in this situation does nothing
39+
--> $DIR/noop-method-call.rs:53:19
3240
|
3341
LL | non_clone_type.clone();
3442
| ^^^^^^^^ unnecessary method call
3543
|
3644
= note: the type `&PlainType<u32>` which `clone` is being called on is the same as the type returned from `clone`, so the method call does not do anything and can be removed
3745

38-
warning: 4 warnings emitted
46+
warning: 5 warnings emitted
3947

0 commit comments

Comments
 (0)