Skip to content

Commit 626efab

Browse files
committed
fix
1 parent 33ec4e4 commit 626efab

File tree

11 files changed

+33
-29
lines changed

11 files changed

+33
-29
lines changed

compiler/rustc_lint/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ lint_non_upper_case_global = {$sort} `{$name}` should have an upper case name
413413
414414
lint_noop_method_call = call to `.{$method}()` on a reference in this situation does nothing
415415
.label = unnecessary method call
416-
.note = the type `{$receiver_ty}` which `{$method}` is being called on is the same as the type returned from `{$method}`, so the method call does not do anything and can be removed
416+
.note = the type `{$orig_ty}` does not implement `{$trait_}`, so calling `{$method}` on `&{$orig_ty}` copies the reference, which does not do anything and can be removed
417417
418418
lint_only_cast_u8_to_char = only `u8` can be cast into `char`
419419
.suggestion = use a `char` literal instead

compiler/rustc_lint/src/lints.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,8 @@ pub enum NonUpperCaseGlobalSub {
12311231
#[note]
12321232
pub struct NoopMethodCallDiag<'a> {
12331233
pub method: Symbol,
1234-
pub receiver_ty: Ty<'a>,
1234+
pub orig_ty: Ty<'a>,
1235+
pub trait_: Symbol,
12351236
#[label]
12361237
pub label: Span,
12371238
}

compiler/rustc_lint/src/noop_method_call.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,9 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
8585

8686
let Some(trait_id) = cx.tcx.trait_of_item(did) else { return };
8787

88-
if !matches!(
89-
cx.tcx.get_diagnostic_name(trait_id),
90-
Some(sym::Borrow | sym::Clone | sym::Deref)
91-
) {
88+
let Some(trait_) = cx.tcx.get_diagnostic_name(trait_id) else { return };
89+
90+
if !matches!(trait_, sym::Borrow | sym::Clone | sym::Deref) {
9291
return;
9392
};
9493

@@ -113,11 +112,13 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
113112
let expr_span = expr.span;
114113
let span = expr_span.with_lo(receiver.span.hi());
115114

115+
let orig_ty = expr_ty.peel_refs();
116+
116117
if receiver_ty == expr_ty {
117118
cx.emit_spanned_lint(
118119
NOOP_METHOD_CALL,
119120
span,
120-
NoopMethodCallDiag { method: call.ident.name, receiver_ty, label: span },
121+
NoopMethodCallDiag { method: call.ident.name, orig_ty, trait_, label: span },
121122
);
122123
} else {
123124
match name {

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ use rustc_span::{BytePos, DesugaringKind, ExpnKind, MacroKind, Span, DUMMY_SP};
4141
use rustc_target::spec::abi;
4242
use std::borrow::Cow;
4343
use std::iter;
44-
use std::ops::Deref;
4544

4645
use super::InferCtxtPrivExt;
4746
use crate::infer::InferCtxtExt as _;
@@ -3580,7 +3579,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
35803579
// to an associated type (as seen from `trait_pred`) in the predicate. Like in
35813580
// trait_pred `S: Sum<<Self as Iterator>::Item>` and predicate `i32: Sum<&()>`
35823581
let mut type_diffs = vec![];
3583-
if let ObligationCauseCode::ExprBindingObligation(def_id, _, _, idx) = parent_code.deref()
3582+
if let ObligationCauseCode::ExprBindingObligation(def_id, _, _, idx) = parent_code
35843583
&& let Some(node_args) = typeck_results.node_args_opt(call_hir_id)
35853584
&& let where_clauses = self.tcx.predicates_of(def_id).instantiate(self.tcx, node_args)
35863585
&& let Some(where_pred) = where_clauses.predicates.get(*idx)

library/core/benches/iter.rs

+1
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ fn bench_next_chunk_copied(b: &mut Bencher) {
473473

474474
/// Exercises the TrustedRandomAccess specialization in ArrayChunks
475475
#[bench]
476+
#[allow(noop_method_call)]
476477
fn bench_next_chunk_trusted_random_access(b: &mut Bencher) {
477478
let v = vec![1u8; 1024];
478479

src/tools/clippy/tests/ui/explicit_deref_methods.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![allow(
55
clippy::borrow_deref_ref,
66
suspicious_double_ref_op,
7+
noop_method_call,
78
clippy::explicit_auto_deref,
89
clippy::needless_borrow,
910
clippy::no_effect,

src/tools/clippy/tests/ui/explicit_deref_methods.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![allow(
55
clippy::borrow_deref_ref,
66
suspicious_double_ref_op,
7+
noop_method_call,
78
clippy::explicit_auto_deref,
89
clippy::needless_borrow,
910
clippy::no_effect,

src/tools/clippy/tests/ui/explicit_deref_methods.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,73 @@
11
error: explicit `deref` method call
2-
--> $DIR/explicit_deref_methods.rs:54:19
2+
--> $DIR/explicit_deref_methods.rs:55:19
33
|
44
LL | let b: &str = a.deref();
55
| ^^^^^^^^^ help: try: `&*a`
66
|
77
= note: `-D clippy::explicit-deref-methods` implied by `-D warnings`
88

99
error: explicit `deref_mut` method call
10-
--> $DIR/explicit_deref_methods.rs:56:23
10+
--> $DIR/explicit_deref_methods.rs:57:23
1111
|
1212
LL | let b: &mut str = a.deref_mut();
1313
| ^^^^^^^^^^^^^ help: try: `&mut **a`
1414

1515
error: explicit `deref` method call
16-
--> $DIR/explicit_deref_methods.rs:59:39
16+
--> $DIR/explicit_deref_methods.rs:60:39
1717
|
1818
LL | let b: String = format!("{}, {}", a.deref(), a.deref());
1919
| ^^^^^^^^^ help: try: `&*a`
2020

2121
error: explicit `deref` method call
22-
--> $DIR/explicit_deref_methods.rs:59:50
22+
--> $DIR/explicit_deref_methods.rs:60:50
2323
|
2424
LL | let b: String = format!("{}, {}", a.deref(), a.deref());
2525
| ^^^^^^^^^ help: try: `&*a`
2626

2727
error: explicit `deref` method call
28-
--> $DIR/explicit_deref_methods.rs:61:20
28+
--> $DIR/explicit_deref_methods.rs:62:20
2929
|
3030
LL | println!("{}", a.deref());
3131
| ^^^^^^^^^ help: try: `&*a`
3232

3333
error: explicit `deref` method call
34-
--> $DIR/explicit_deref_methods.rs:64:11
34+
--> $DIR/explicit_deref_methods.rs:65:11
3535
|
3636
LL | match a.deref() {
3737
| ^^^^^^^^^ help: try: `&*a`
3838

3939
error: explicit `deref` method call
40-
--> $DIR/explicit_deref_methods.rs:68:28
40+
--> $DIR/explicit_deref_methods.rs:69:28
4141
|
4242
LL | let b: String = concat(a.deref());
4343
| ^^^^^^^^^ help: try: `&*a`
4444

4545
error: explicit `deref` method call
46-
--> $DIR/explicit_deref_methods.rs:70:13
46+
--> $DIR/explicit_deref_methods.rs:71:13
4747
|
4848
LL | let b = just_return(a).deref();
4949
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `just_return(a)`
5050

5151
error: explicit `deref` method call
52-
--> $DIR/explicit_deref_methods.rs:72:28
52+
--> $DIR/explicit_deref_methods.rs:73:28
5353
|
5454
LL | let b: String = concat(just_return(a).deref());
5555
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `just_return(a)`
5656

5757
error: explicit `deref` method call
58-
--> $DIR/explicit_deref_methods.rs:74:19
58+
--> $DIR/explicit_deref_methods.rs:75:19
5959
|
6060
LL | let b: &str = a.deref().deref();
6161
| ^^^^^^^^^^^^^^^^^ help: try: `&**a`
6262

6363
error: explicit `deref` method call
64-
--> $DIR/explicit_deref_methods.rs:77:13
64+
--> $DIR/explicit_deref_methods.rs:78:13
6565
|
6666
LL | let b = opt_a.unwrap().deref();
6767
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*opt_a.unwrap()`
6868

6969
error: explicit `deref` method call
70-
--> $DIR/explicit_deref_methods.rs:114:31
70+
--> $DIR/explicit_deref_methods.rs:115:31
7171
|
7272
LL | let b: &str = expr_deref!(a.deref());
7373
| ^^^^^^^^^ help: try: `&*a`

src/tools/compiletest/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,7 @@ fn check_overlapping_tests(found_paths: &BTreeSet<PathBuf>) {
11191119
for path in found_paths {
11201120
for ancestor in path.ancestors().skip(1) {
11211121
if found_paths.contains(ancestor) {
1122-
collisions.push((path, ancestor.clone()));
1122+
collisions.push((path, ancestor));
11231123
}
11241124
}
11251125
}

tests/ui/lint/noop-method-call.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: call to `.clone()` on a reference in this situation does nothing
44
LL | let non_clone_type_ref_clone: &PlainType<u32> = non_clone_type_ref.clone();
55
| ^^^^^^^^ unnecessary method call
66
|
7-
= 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
7+
= note: the type `PlainType<u32>` does not implement `Clone`, so calling `clone` on `&PlainType<u32>` copies the reference, which does not do anything and can be removed
88
= note: `#[warn(noop_method_call)]` on by default
99

1010
warning: using `.clone()` on a double reference, which returns `&CloneType<u32>` instead of cloning the inner type
@@ -21,7 +21,7 @@ warning: call to `.deref()` on a reference in this situation does nothing
2121
LL | let non_deref_type_deref: &PlainType<u32> = non_deref_type.deref();
2222
| ^^^^^^^^ unnecessary method call
2323
|
24-
= note: the type `&PlainType<u32>` which `deref` is being called on is the same as the type returned from `deref`, so the method call does not do anything and can be removed
24+
= note: the type `PlainType<u32>` does not implement `Deref`, so calling `deref` on `&PlainType<u32>` copies the reference, which does not do anything and can be removed
2525

2626
warning: using `.deref()` on a double reference, which returns `&PlainType<u32>` instead of dereferencing the inner type
2727
--> $DIR/noop-method-call.rs:30:63
@@ -35,7 +35,7 @@ warning: call to `.borrow()` on a reference in this situation does nothing
3535
LL | let non_borrow_type_borrow: &PlainType<u32> = non_borrow_type.borrow();
3636
| ^^^^^^^^^ unnecessary method call
3737
|
38-
= 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
38+
= note: the type `PlainType<u32>` does not implement `Borrow`, so calling `borrow` on `&PlainType<u32>` copies the reference, which does not do anything and can be removed
3939

4040
warning: using `.clone()` on a double reference, which returns `&str` instead of cloning the inner type
4141
--> $DIR/noop-method-call.rs:42:44
@@ -49,15 +49,15 @@ warning: call to `.clone()` on a reference in this situation does nothing
4949
LL | non_clone_type.clone();
5050
| ^^^^^^^^ unnecessary method call
5151
|
52-
= 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
52+
= note: the type `PlainType<T>` does not implement `Clone`, so calling `clone` on `&PlainType<T>` copies the reference, which does not do anything and can be removed
5353

5454
warning: call to `.clone()` on a reference in this situation does nothing
5555
--> $DIR/noop-method-call.rs:52:19
5656
|
5757
LL | non_clone_type.clone();
5858
| ^^^^^^^^ unnecessary method call
5959
|
60-
= 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
60+
= note: the type `PlainType<u32>` does not implement `Clone`, so calling `clone` on `&PlainType<u32>` copies the reference, which does not do anything and can be removed
6161

6262
warning: 8 warnings emitted
6363

tests/ui/lint/suspicious-double-ref-op.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ error: call to `.clone()` on a reference in this situation does nothing
1616
LL | let _ = &mut encoded.clone();
1717
| ^^^^^^^^ unnecessary method call
1818
|
19-
= note: the type `&[u8]` 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
19+
= note: the type `[u8]` does not implement `Clone`, so calling `clone` on `&[u8]` copies the reference, which does not do anything and can be removed
2020
note: the lint level is defined here
2121
--> $DIR/suspicious-double-ref-op.rs:2:35
2222
|
@@ -29,7 +29,7 @@ error: call to `.clone()` on a reference in this situation does nothing
2929
LL | let _ = &encoded.clone();
3030
| ^^^^^^^^ unnecessary method call
3131
|
32-
= note: the type `&[u8]` 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
32+
= note: the type `[u8]` does not implement `Clone`, so calling `clone` on `&[u8]` copies the reference, which does not do anything and can be removed
3333

3434
error: aborting due to 3 previous errors
3535

0 commit comments

Comments
 (0)