From a98f4d3b9c41c950f33b38f050fff6492864b58f Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Wed, 24 May 2023 16:05:42 +0000 Subject: [PATCH 1/3] make `noop_method_call` warn by default --- compiler/rustc_lint/src/noop_method_call.rs | 3 +-- tests/ui/issues/issue-11820.rs | 2 ++ tests/ui/lint/noop-method-call.rs | 1 - tests/ui/lint/noop-method-call.stderr | 22 +++++++++------------ tests/ui/underscore-imports/cycle.rs | 2 ++ tests/ui/underscore-imports/hygiene.rs | 1 + 6 files changed, 15 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_lint/src/noop_method_call.rs b/compiler/rustc_lint/src/noop_method_call.rs index d054966459d85..ad67a3ce0e6eb 100644 --- a/compiler/rustc_lint/src/noop_method_call.rs +++ b/compiler/rustc_lint/src/noop_method_call.rs @@ -16,7 +16,6 @@ declare_lint! { /// /// ```rust /// # #![allow(unused)] - /// #![warn(noop_method_call)] /// struct Foo; /// let foo = &Foo; /// let clone: &Foo = foo.clone(); @@ -32,7 +31,7 @@ declare_lint! { /// calling `clone` on a `&T` where `T` does not implement clone, actually doesn't do anything /// as references are copy. This lint detects these calls and warns the user about them. pub NOOP_METHOD_CALL, - Allow, + Warn, "detects the use of well-known noop methods" } diff --git a/tests/ui/issues/issue-11820.rs b/tests/ui/issues/issue-11820.rs index 7ffe9652797cf..dc6349b10ee58 100644 --- a/tests/ui/issues/issue-11820.rs +++ b/tests/ui/issues/issue-11820.rs @@ -1,6 +1,8 @@ // run-pass // pretty-expanded FIXME #23616 +#![allow(noop_method_call)] + struct NoClone; fn main() { diff --git a/tests/ui/lint/noop-method-call.rs b/tests/ui/lint/noop-method-call.rs index dbcf2a5131b5b..8ce6c859b2480 100644 --- a/tests/ui/lint/noop-method-call.rs +++ b/tests/ui/lint/noop-method-call.rs @@ -1,7 +1,6 @@ // check-pass #![allow(unused)] -#![warn(noop_method_call)] use std::borrow::Borrow; use std::ops::Deref; diff --git a/tests/ui/lint/noop-method-call.stderr b/tests/ui/lint/noop-method-call.stderr index 37cd1a0fc18ea..ae61913f99381 100644 --- a/tests/ui/lint/noop-method-call.stderr +++ b/tests/ui/lint/noop-method-call.stderr @@ -1,18 +1,14 @@ warning: call to `.clone()` on a reference in this situation does nothing - --> $DIR/noop-method-call.rs:16:71 + --> $DIR/noop-method-call.rs:15:71 | LL | let non_clone_type_ref_clone: &PlainType = non_clone_type_ref.clone(); | ^^^^^^^^ unnecessary method call | = note: the type `&PlainType` 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 -note: the lint level is defined here - --> $DIR/noop-method-call.rs:4:9 - | -LL | #![warn(noop_method_call)] - | ^^^^^^^^^^^^^^^^ + = note: `#[warn(noop_method_call)]` on by default warning: using `.clone()` on a double reference, which returns `&CloneType` instead of cloning the inner type - --> $DIR/noop-method-call.rs:23:63 + --> $DIR/noop-method-call.rs:22:63 | LL | let clone_type_ref_clone: &CloneType = clone_type_ref.clone(); | ^^^^^^^^ @@ -20,7 +16,7 @@ LL | let clone_type_ref_clone: &CloneType = clone_type_ref.clone(); = note: `#[warn(suspicious_double_ref_op)]` on by default warning: call to `.deref()` on a reference in this situation does nothing - --> $DIR/noop-method-call.rs:27:63 + --> $DIR/noop-method-call.rs:26:63 | LL | let non_deref_type_deref: &PlainType = non_deref_type.deref(); | ^^^^^^^^ unnecessary method call @@ -28,13 +24,13 @@ LL | let non_deref_type_deref: &PlainType = non_deref_type.deref(); = note: the type `&PlainType` 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 warning: using `.deref()` on a double reference, which returns `&PlainType` instead of dereferencing the inner type - --> $DIR/noop-method-call.rs:31:63 + --> $DIR/noop-method-call.rs:30:63 | LL | let non_deref_type_deref: &PlainType = non_deref_type.deref(); | ^^^^^^^^ warning: call to `.borrow()` on a reference in this situation does nothing - --> $DIR/noop-method-call.rs:35:66 + --> $DIR/noop-method-call.rs:34:66 | LL | let non_borrow_type_borrow: &PlainType = non_borrow_type.borrow(); | ^^^^^^^^^ unnecessary method call @@ -42,13 +38,13 @@ LL | let non_borrow_type_borrow: &PlainType = non_borrow_type.borrow(); = note: the type `&PlainType` 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 warning: using `.clone()` on a double reference, which returns `&str` instead of cloning the inner type - --> $DIR/noop-method-call.rs:43:44 + --> $DIR/noop-method-call.rs:42:44 | LL | let _v: Vec<&str> = xs.iter().map(|x| x.clone()).collect(); // could use `*x` instead | ^^^^^^^^ warning: call to `.clone()` on a reference in this situation does nothing - --> $DIR/noop-method-call.rs:48:19 + --> $DIR/noop-method-call.rs:47:19 | LL | non_clone_type.clone(); | ^^^^^^^^ unnecessary method call @@ -56,7 +52,7 @@ LL | non_clone_type.clone(); = note: the type `&PlainType` 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 warning: call to `.clone()` on a reference in this situation does nothing - --> $DIR/noop-method-call.rs:53:19 + --> $DIR/noop-method-call.rs:52:19 | LL | non_clone_type.clone(); | ^^^^^^^^ unnecessary method call diff --git a/tests/ui/underscore-imports/cycle.rs b/tests/ui/underscore-imports/cycle.rs index bacf9b2d5a96a..c8a293687878f 100644 --- a/tests/ui/underscore-imports/cycle.rs +++ b/tests/ui/underscore-imports/cycle.rs @@ -2,6 +2,8 @@ // check-pass +#![allow(noop_method_call)] + mod x { pub use crate::y::*; pub use std::ops::Deref as _; diff --git a/tests/ui/underscore-imports/hygiene.rs b/tests/ui/underscore-imports/hygiene.rs index c4db65245386f..7795ccb797199 100644 --- a/tests/ui/underscore-imports/hygiene.rs +++ b/tests/ui/underscore-imports/hygiene.rs @@ -3,6 +3,7 @@ // check-pass #![feature(decl_macro)] +#![allow(noop_method_call)] mod x { pub use std::ops::Deref as _; From b5e49153aca523a9c26600154337462e9214928b Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Thu, 25 May 2023 05:21:44 +0000 Subject: [PATCH 2/3] fix --- compiler/rustc_lint/messages.ftl | 2 +- compiler/rustc_lint/src/lints.rs | 3 +- compiler/rustc_lint/src/noop_method_call.rs | 29 +++++++++---------- .../src/traits/error_reporting/suggestions.rs | 3 +- library/core/benches/iter.rs | 1 + .../tests/ui/explicit_deref_methods.fixed | 1 + .../clippy/tests/ui/explicit_deref_methods.rs | 1 + .../tests/ui/explicit_deref_methods.stderr | 24 +++++++-------- src/tools/compiletest/src/lib.rs | 2 +- tests/ui/lint/noop-method-call.stderr | 10 +++---- tests/ui/lint/suspicious-double-ref-op.stderr | 4 +-- 11 files changed, 40 insertions(+), 40 deletions(-) diff --git a/compiler/rustc_lint/messages.ftl b/compiler/rustc_lint/messages.ftl index 98fe3821947d5..cb8fe4735c0e2 100644 --- a/compiler/rustc_lint/messages.ftl +++ b/compiler/rustc_lint/messages.ftl @@ -405,7 +405,7 @@ lint_non_upper_case_global = {$sort} `{$name}` should have an upper case name lint_noop_method_call = call to `.{$method}()` on a reference in this situation does nothing .label = unnecessary method call - .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 + .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 lint_only_cast_u8_to_char = only `u8` can be cast into `char` .suggestion = use a `char` literal instead diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index fd15f7952023a..5d9971acfc3c3 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -1206,7 +1206,8 @@ pub enum NonUpperCaseGlobalSub { #[note] pub struct NoopMethodCallDiag<'a> { pub method: Symbol, - pub receiver_ty: Ty<'a>, + pub orig_ty: Ty<'a>, + pub trait_: Symbol, #[label] pub label: Span, } diff --git a/compiler/rustc_lint/src/noop_method_call.rs b/compiler/rustc_lint/src/noop_method_call.rs index ad67a3ce0e6eb..7207492e342f7 100644 --- a/compiler/rustc_lint/src/noop_method_call.rs +++ b/compiler/rustc_lint/src/noop_method_call.rs @@ -75,21 +75,16 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall { // We only care about method calls corresponding to the `Clone`, `Deref` and `Borrow` // traits and ignore any other method call. - let did = match cx.typeck_results().type_dependent_def(expr.hir_id) { - // Verify we are dealing with a method/associated function. - Some((DefKind::AssocFn, did)) => match cx.tcx.trait_of_item(did) { - // Check that we're dealing with a trait method for one of the traits we care about. - Some(trait_id) - if matches!( - cx.tcx.get_diagnostic_name(trait_id), - Some(sym::Borrow | sym::Clone | sym::Deref) - ) => - { - did - } - _ => return, - }, - _ => return, + // + // Verify we are dealing with a method/associated function, and check that we're dealing + // with a trait method for one of the traits we care about. + let (did, trait_) = if let Some((DefKind::AssocFn, did)) = cx.typeck_results().type_dependent_def(expr.hir_id) + && let Some(trait_id) = cx.tcx.trait_of_item(did) + && let Some(trait_ @ (sym::Borrow | sym::Clone | sym::Deref)) = cx.tcx.get_diagnostic_name(trait_id) + { + (did, trait_) + } else { + return; }; let substs = cx .tcx @@ -121,11 +116,13 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall { let expr_span = expr.span; let span = expr_span.with_lo(receiver.span.hi()); + let orig_ty = expr_ty.peel_refs(); + if receiver_ty == expr_ty { cx.emit_spanned_lint( NOOP_METHOD_CALL, span, - NoopMethodCallDiag { method: call.ident.name, receiver_ty, label: span }, + NoopMethodCallDiag { method: call.ident.name, orig_ty, trait_, label: span }, ); } else { cx.emit_spanned_lint( diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 42038dbc3d82e..ed6f6ede7381b 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -40,7 +40,6 @@ use rustc_span::{BytePos, DesugaringKind, ExpnKind, MacroKind, Span, DUMMY_SP}; use rustc_target::spec::abi; use std::borrow::Cow; use std::iter; -use std::ops::Deref; use super::InferCtxtPrivExt; use crate::infer::InferCtxtExt as _; @@ -3466,7 +3465,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { // to an associated type (as seen from `trait_pred`) in the predicate. Like in // trait_pred `S: Sum<::Item>` and predicate `i32: Sum<&()>` let mut type_diffs = vec![]; - if let ObligationCauseCode::ExprBindingObligation(def_id, _, _, idx) = parent_code.deref() + if let ObligationCauseCode::ExprBindingObligation(def_id, _, _, idx) = parent_code && let Some(node_substs) = typeck_results.node_substs_opt(call_hir_id) && let where_clauses = self.tcx.predicates_of(def_id).instantiate(self.tcx, node_substs) && let Some(where_pred) = where_clauses.predicates.get(*idx) diff --git a/library/core/benches/iter.rs b/library/core/benches/iter.rs index 60ef83223d104..4d68960431c7a 100644 --- a/library/core/benches/iter.rs +++ b/library/core/benches/iter.rs @@ -421,6 +421,7 @@ fn bench_next_chunk_copied(b: &mut Bencher) { /// Exercises the TrustedRandomAccess specialization in ArrayChunks #[bench] +#[allow(noop_method_call)] fn bench_next_chunk_trusted_random_access(b: &mut Bencher) { let v = vec![1u8; 1024]; diff --git a/src/tools/clippy/tests/ui/explicit_deref_methods.fixed b/src/tools/clippy/tests/ui/explicit_deref_methods.fixed index 60482c66da7c4..f5510461a40aa 100644 --- a/src/tools/clippy/tests/ui/explicit_deref_methods.fixed +++ b/src/tools/clippy/tests/ui/explicit_deref_methods.fixed @@ -4,6 +4,7 @@ #![allow( clippy::borrow_deref_ref, suspicious_double_ref_op, + noop_method_call, clippy::explicit_auto_deref, clippy::needless_borrow, clippy::uninlined_format_args diff --git a/src/tools/clippy/tests/ui/explicit_deref_methods.rs b/src/tools/clippy/tests/ui/explicit_deref_methods.rs index e3613e216bb22..9b67308b91f1b 100644 --- a/src/tools/clippy/tests/ui/explicit_deref_methods.rs +++ b/src/tools/clippy/tests/ui/explicit_deref_methods.rs @@ -4,6 +4,7 @@ #![allow( clippy::borrow_deref_ref, suspicious_double_ref_op, + noop_method_call, clippy::explicit_auto_deref, clippy::needless_borrow, clippy::uninlined_format_args diff --git a/src/tools/clippy/tests/ui/explicit_deref_methods.stderr b/src/tools/clippy/tests/ui/explicit_deref_methods.stderr index 4b10ed1377b0d..809a94ddccd83 100644 --- a/src/tools/clippy/tests/ui/explicit_deref_methods.stderr +++ b/src/tools/clippy/tests/ui/explicit_deref_methods.stderr @@ -1,5 +1,5 @@ error: explicit `deref` method call - --> $DIR/explicit_deref_methods.rs:36:19 + --> $DIR/explicit_deref_methods.rs:37:19 | LL | let b: &str = a.deref(); | ^^^^^^^^^ help: try this: `&*a` @@ -7,67 +7,67 @@ LL | let b: &str = a.deref(); = note: `-D clippy::explicit-deref-methods` implied by `-D warnings` error: explicit `deref_mut` method call - --> $DIR/explicit_deref_methods.rs:38:23 + --> $DIR/explicit_deref_methods.rs:39:23 | LL | let b: &mut str = a.deref_mut(); | ^^^^^^^^^^^^^ help: try this: `&mut **a` error: explicit `deref` method call - --> $DIR/explicit_deref_methods.rs:41:39 + --> $DIR/explicit_deref_methods.rs:42:39 | LL | let b: String = format!("{}, {}", a.deref(), a.deref()); | ^^^^^^^^^ help: try this: `&*a` error: explicit `deref` method call - --> $DIR/explicit_deref_methods.rs:41:50 + --> $DIR/explicit_deref_methods.rs:42:50 | LL | let b: String = format!("{}, {}", a.deref(), a.deref()); | ^^^^^^^^^ help: try this: `&*a` error: explicit `deref` method call - --> $DIR/explicit_deref_methods.rs:43:20 + --> $DIR/explicit_deref_methods.rs:44:20 | LL | println!("{}", a.deref()); | ^^^^^^^^^ help: try this: `&*a` error: explicit `deref` method call - --> $DIR/explicit_deref_methods.rs:46:11 + --> $DIR/explicit_deref_methods.rs:47:11 | LL | match a.deref() { | ^^^^^^^^^ help: try this: `&*a` error: explicit `deref` method call - --> $DIR/explicit_deref_methods.rs:50:28 + --> $DIR/explicit_deref_methods.rs:51:28 | LL | let b: String = concat(a.deref()); | ^^^^^^^^^ help: try this: `&*a` error: explicit `deref` method call - --> $DIR/explicit_deref_methods.rs:52:13 + --> $DIR/explicit_deref_methods.rs:53:13 | LL | let b = just_return(a).deref(); | ^^^^^^^^^^^^^^^^^^^^^^ help: try this: `just_return(a)` error: explicit `deref` method call - --> $DIR/explicit_deref_methods.rs:54:28 + --> $DIR/explicit_deref_methods.rs:55:28 | LL | let b: String = concat(just_return(a).deref()); | ^^^^^^^^^^^^^^^^^^^^^^ help: try this: `just_return(a)` error: explicit `deref` method call - --> $DIR/explicit_deref_methods.rs:56:19 + --> $DIR/explicit_deref_methods.rs:57:19 | LL | let b: &str = a.deref().deref(); | ^^^^^^^^^^^^^^^^^ help: try this: `&**a` error: explicit `deref` method call - --> $DIR/explicit_deref_methods.rs:59:13 + --> $DIR/explicit_deref_methods.rs:60:13 | LL | let b = opt_a.unwrap().deref(); | ^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&*opt_a.unwrap()` error: explicit `deref` method call - --> $DIR/explicit_deref_methods.rs:85:31 + --> $DIR/explicit_deref_methods.rs:86:31 | LL | let b: &str = expr_deref!(a.deref()); | ^^^^^^^^^ help: try this: `&*a` diff --git a/src/tools/compiletest/src/lib.rs b/src/tools/compiletest/src/lib.rs index fc48d0159905b..1a765477fe501 100644 --- a/src/tools/compiletest/src/lib.rs +++ b/src/tools/compiletest/src/lib.rs @@ -1119,7 +1119,7 @@ fn check_overlapping_tests(found_paths: &BTreeSet) { for path in found_paths { for ancestor in path.ancestors().skip(1) { if found_paths.contains(ancestor) { - collisions.push((path, ancestor.clone())); + collisions.push((path, ancestor)); } } } diff --git a/tests/ui/lint/noop-method-call.stderr b/tests/ui/lint/noop-method-call.stderr index ae61913f99381..fb02c6f361e0a 100644 --- a/tests/ui/lint/noop-method-call.stderr +++ b/tests/ui/lint/noop-method-call.stderr @@ -4,7 +4,7 @@ warning: call to `.clone()` on a reference in this situation does nothing LL | let non_clone_type_ref_clone: &PlainType = non_clone_type_ref.clone(); | ^^^^^^^^ unnecessary method call | - = note: the type `&PlainType` 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 + = note: the type `PlainType` does not implement `Clone`, so calling `clone` on `&PlainType` copies the reference, which does not do anything and can be removed = note: `#[warn(noop_method_call)]` on by default warning: using `.clone()` on a double reference, which returns `&CloneType` instead of cloning the inner type @@ -21,7 +21,7 @@ warning: call to `.deref()` on a reference in this situation does nothing LL | let non_deref_type_deref: &PlainType = non_deref_type.deref(); | ^^^^^^^^ unnecessary method call | - = note: the type `&PlainType` 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 + = note: the type `PlainType` does not implement `Deref`, so calling `deref` on `&PlainType` copies the reference, which does not do anything and can be removed warning: using `.deref()` on a double reference, which returns `&PlainType` instead of dereferencing the inner type --> $DIR/noop-method-call.rs:30:63 @@ -35,7 +35,7 @@ warning: call to `.borrow()` on a reference in this situation does nothing LL | let non_borrow_type_borrow: &PlainType = non_borrow_type.borrow(); | ^^^^^^^^^ unnecessary method call | - = note: the type `&PlainType` 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 + = note: the type `PlainType` does not implement `Borrow`, so calling `borrow` on `&PlainType` copies the reference, which does not do anything and can be removed warning: using `.clone()` on a double reference, which returns `&str` instead of cloning the inner type --> $DIR/noop-method-call.rs:42:44 @@ -49,7 +49,7 @@ warning: call to `.clone()` on a reference in this situation does nothing LL | non_clone_type.clone(); | ^^^^^^^^ unnecessary method call | - = note: the type `&PlainType` 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 + = note: the type `PlainType` does not implement `Clone`, so calling `clone` on `&PlainType` copies the reference, which does not do anything and can be removed warning: call to `.clone()` on a reference in this situation does nothing --> $DIR/noop-method-call.rs:52:19 @@ -57,7 +57,7 @@ warning: call to `.clone()` on a reference in this situation does nothing LL | non_clone_type.clone(); | ^^^^^^^^ unnecessary method call | - = note: the type `&PlainType` 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 + = note: the type `PlainType` does not implement `Clone`, so calling `clone` on `&PlainType` copies the reference, which does not do anything and can be removed warning: 8 warnings emitted diff --git a/tests/ui/lint/suspicious-double-ref-op.stderr b/tests/ui/lint/suspicious-double-ref-op.stderr index d15487ca23865..f2128f2a0edad 100644 --- a/tests/ui/lint/suspicious-double-ref-op.stderr +++ b/tests/ui/lint/suspicious-double-ref-op.stderr @@ -16,7 +16,7 @@ error: call to `.clone()` on a reference in this situation does nothing LL | let _ = &mut encoded.clone(); | ^^^^^^^^ unnecessary method call | - = 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 + = 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 note: the lint level is defined here --> $DIR/suspicious-double-ref-op.rs:2:35 | @@ -29,7 +29,7 @@ error: call to `.clone()` on a reference in this situation does nothing LL | let _ = &encoded.clone(); | ^^^^^^^^ unnecessary method call | - = 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 + = 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 error: aborting due to 3 previous errors From 59b2ca3c43255983f71961a7145f11a9dadee9ac Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Sun, 4 Jun 2023 15:04:23 +0000 Subject: [PATCH 3/3] deny noop method call --- compiler/rustc_lint/src/noop_method_call.rs | 2 +- tests/ui/lint/noop-method-call.rs | 1 + tests/ui/lint/noop-method-call.stderr | 22 ++++++++++++--------- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_lint/src/noop_method_call.rs b/compiler/rustc_lint/src/noop_method_call.rs index 7207492e342f7..6546f979ef9fa 100644 --- a/compiler/rustc_lint/src/noop_method_call.rs +++ b/compiler/rustc_lint/src/noop_method_call.rs @@ -31,7 +31,7 @@ declare_lint! { /// calling `clone` on a `&T` where `T` does not implement clone, actually doesn't do anything /// as references are copy. This lint detects these calls and warns the user about them. pub NOOP_METHOD_CALL, - Warn, + Deny, "detects the use of well-known noop methods" } diff --git a/tests/ui/lint/noop-method-call.rs b/tests/ui/lint/noop-method-call.rs index 8ce6c859b2480..dbcf2a5131b5b 100644 --- a/tests/ui/lint/noop-method-call.rs +++ b/tests/ui/lint/noop-method-call.rs @@ -1,6 +1,7 @@ // check-pass #![allow(unused)] +#![warn(noop_method_call)] use std::borrow::Borrow; use std::ops::Deref; diff --git a/tests/ui/lint/noop-method-call.stderr b/tests/ui/lint/noop-method-call.stderr index fb02c6f361e0a..cb0777775a0ec 100644 --- a/tests/ui/lint/noop-method-call.stderr +++ b/tests/ui/lint/noop-method-call.stderr @@ -1,14 +1,18 @@ warning: call to `.clone()` on a reference in this situation does nothing - --> $DIR/noop-method-call.rs:15:71 + --> $DIR/noop-method-call.rs:16:71 | LL | let non_clone_type_ref_clone: &PlainType = non_clone_type_ref.clone(); | ^^^^^^^^ unnecessary method call | = note: the type `PlainType` does not implement `Clone`, so calling `clone` on `&PlainType` copies the reference, which does not do anything and can be removed - = note: `#[warn(noop_method_call)]` on by default +note: the lint level is defined here + --> $DIR/noop-method-call.rs:4:9 + | +LL | #![warn(noop_method_call)] + | ^^^^^^^^^^^^^^^^ warning: using `.clone()` on a double reference, which returns `&CloneType` instead of cloning the inner type - --> $DIR/noop-method-call.rs:22:63 + --> $DIR/noop-method-call.rs:23:63 | LL | let clone_type_ref_clone: &CloneType = clone_type_ref.clone(); | ^^^^^^^^ @@ -16,7 +20,7 @@ LL | let clone_type_ref_clone: &CloneType = clone_type_ref.clone(); = note: `#[warn(suspicious_double_ref_op)]` on by default warning: call to `.deref()` on a reference in this situation does nothing - --> $DIR/noop-method-call.rs:26:63 + --> $DIR/noop-method-call.rs:27:63 | LL | let non_deref_type_deref: &PlainType = non_deref_type.deref(); | ^^^^^^^^ unnecessary method call @@ -24,13 +28,13 @@ LL | let non_deref_type_deref: &PlainType = non_deref_type.deref(); = note: the type `PlainType` does not implement `Deref`, so calling `deref` on `&PlainType` copies the reference, which does not do anything and can be removed warning: using `.deref()` on a double reference, which returns `&PlainType` instead of dereferencing the inner type - --> $DIR/noop-method-call.rs:30:63 + --> $DIR/noop-method-call.rs:31:63 | LL | let non_deref_type_deref: &PlainType = non_deref_type.deref(); | ^^^^^^^^ warning: call to `.borrow()` on a reference in this situation does nothing - --> $DIR/noop-method-call.rs:34:66 + --> $DIR/noop-method-call.rs:35:66 | LL | let non_borrow_type_borrow: &PlainType = non_borrow_type.borrow(); | ^^^^^^^^^ unnecessary method call @@ -38,13 +42,13 @@ LL | let non_borrow_type_borrow: &PlainType = non_borrow_type.borrow(); = note: the type `PlainType` does not implement `Borrow`, so calling `borrow` on `&PlainType` copies the reference, which does not do anything and can be removed warning: using `.clone()` on a double reference, which returns `&str` instead of cloning the inner type - --> $DIR/noop-method-call.rs:42:44 + --> $DIR/noop-method-call.rs:43:44 | LL | let _v: Vec<&str> = xs.iter().map(|x| x.clone()).collect(); // could use `*x` instead | ^^^^^^^^ warning: call to `.clone()` on a reference in this situation does nothing - --> $DIR/noop-method-call.rs:47:19 + --> $DIR/noop-method-call.rs:48:19 | LL | non_clone_type.clone(); | ^^^^^^^^ unnecessary method call @@ -52,7 +56,7 @@ LL | non_clone_type.clone(); = note: the type `PlainType` does not implement `Clone`, so calling `clone` on `&PlainType` copies the reference, which does not do anything and can be removed warning: call to `.clone()` on a reference in this situation does nothing - --> $DIR/noop-method-call.rs:52:19 + --> $DIR/noop-method-call.rs:53:19 | LL | non_clone_type.clone(); | ^^^^^^^^ unnecessary method call