Skip to content

Commit b5e4915

Browse files
committed
fix
1 parent a98f4d3 commit b5e4915

File tree

11 files changed

+40
-40
lines changed

11 files changed

+40
-40
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ lint_non_upper_case_global = {$sort} `{$name}` should have an upper case name
405405
406406
lint_noop_method_call = call to `.{$method}()` on a reference in this situation does nothing
407407
.label = unnecessary method call
408-
.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
408+
.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
409409
410410
lint_only_cast_u8_to_char = only `u8` can be cast into `char`
411411
.suggestion = use a `char` literal instead

compiler/rustc_lint/src/lints.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,8 @@ pub enum NonUpperCaseGlobalSub {
12061206
#[note]
12071207
pub struct NoopMethodCallDiag<'a> {
12081208
pub method: Symbol,
1209-
pub receiver_ty: Ty<'a>,
1209+
pub orig_ty: Ty<'a>,
1210+
pub trait_: Symbol,
12101211
#[label]
12111212
pub label: Span,
12121213
}

compiler/rustc_lint/src/noop_method_call.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,16 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
7575

7676
// We only care about method calls corresponding to the `Clone`, `Deref` and `Borrow`
7777
// traits and ignore any other method call.
78-
let did = match cx.typeck_results().type_dependent_def(expr.hir_id) {
79-
// Verify we are dealing with a method/associated function.
80-
Some((DefKind::AssocFn, did)) => match cx.tcx.trait_of_item(did) {
81-
// Check that we're dealing with a trait method for one of the traits we care about.
82-
Some(trait_id)
83-
if matches!(
84-
cx.tcx.get_diagnostic_name(trait_id),
85-
Some(sym::Borrow | sym::Clone | sym::Deref)
86-
) =>
87-
{
88-
did
89-
}
90-
_ => return,
91-
},
92-
_ => return,
78+
//
79+
// Verify we are dealing with a method/associated function, and check that we're dealing
80+
// with a trait method for one of the traits we care about.
81+
let (did, trait_) = if let Some((DefKind::AssocFn, did)) = cx.typeck_results().type_dependent_def(expr.hir_id)
82+
&& let Some(trait_id) = cx.tcx.trait_of_item(did)
83+
&& let Some(trait_ @ (sym::Borrow | sym::Clone | sym::Deref)) = cx.tcx.get_diagnostic_name(trait_id)
84+
{
85+
(did, trait_)
86+
} else {
87+
return;
9388
};
9489
let substs = cx
9590
.tcx
@@ -121,11 +116,13 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
121116
let expr_span = expr.span;
122117
let span = expr_span.with_lo(receiver.span.hi());
123118

119+
let orig_ty = expr_ty.peel_refs();
120+
124121
if receiver_ty == expr_ty {
125122
cx.emit_spanned_lint(
126123
NOOP_METHOD_CALL,
127124
span,
128-
NoopMethodCallDiag { method: call.ident.name, receiver_ty, label: span },
125+
NoopMethodCallDiag { method: call.ident.name, orig_ty, trait_, label: span },
129126
);
130127
} else {
131128
cx.emit_spanned_lint(

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ use rustc_span::{BytePos, DesugaringKind, ExpnKind, MacroKind, Span, DUMMY_SP};
4040
use rustc_target::spec::abi;
4141
use std::borrow::Cow;
4242
use std::iter;
43-
use std::ops::Deref;
4443

4544
use super::InferCtxtPrivExt;
4645
use crate::infer::InferCtxtExt as _;
@@ -3466,7 +3465,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
34663465
// to an associated type (as seen from `trait_pred`) in the predicate. Like in
34673466
// trait_pred `S: Sum<<Self as Iterator>::Item>` and predicate `i32: Sum<&()>`
34683467
let mut type_diffs = vec![];
3469-
if let ObligationCauseCode::ExprBindingObligation(def_id, _, _, idx) = parent_code.deref()
3468+
if let ObligationCauseCode::ExprBindingObligation(def_id, _, _, idx) = parent_code
34703469
&& let Some(node_substs) = typeck_results.node_substs_opt(call_hir_id)
34713470
&& let where_clauses = self.tcx.predicates_of(def_id).instantiate(self.tcx, node_substs)
34723471
&& let Some(where_pred) = where_clauses.predicates.get(*idx)

library/core/benches/iter.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ fn bench_next_chunk_copied(b: &mut Bencher) {
421421

422422
/// Exercises the TrustedRandomAccess specialization in ArrayChunks
423423
#[bench]
424+
#[allow(noop_method_call)]
424425
fn bench_next_chunk_trusted_random_access(b: &mut Bencher) {
425426
let v = vec![1u8; 1024];
426427

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

Lines changed: 1 addition & 0 deletions
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::uninlined_format_args

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

Lines changed: 1 addition & 0 deletions
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::uninlined_format_args

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,73 @@
11
error: explicit `deref` method call
2-
--> $DIR/explicit_deref_methods.rs:36:19
2+
--> $DIR/explicit_deref_methods.rs:37:19
33
|
44
LL | let b: &str = a.deref();
55
| ^^^^^^^^^ help: try this: `&*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:38:23
10+
--> $DIR/explicit_deref_methods.rs:39:23
1111
|
1212
LL | let b: &mut str = a.deref_mut();
1313
| ^^^^^^^^^^^^^ help: try this: `&mut **a`
1414

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

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

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

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

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

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

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

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

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

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

src/tools/compiletest/src/lib.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 2 additions & 2 deletions
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)