1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 #![warn(clippy::use_self)]
12 #![allow(clippy::should_implement_trait)]
28 impl Default for Foo {
47 impl Default for Foo {
48 fn default() -> Self {
54 //todo the lint does not handle lifetimed struct
55 //the following module should trigger the lint on the third method only
62 // Cannot use `Self` as return type, because the function is actually `fn foo<'b>(s: &'b str) ->
64 fn foo(s: &str) -> Foo {
67 // cannot replace with `Self`, because that's `Foo<'a>`
68 fn bar() -> Foo<'static> {
69 Foo { foo_str: "foo" }
72 // `Self` is applicable here
73 fn clone(&self) -> Foo<'a> {
74 Foo { foo_str: self.foo_str }
79 #[allow(clippy::boxed_local)]
85 fn refs(p1: &Self) -> &Self;
86 fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self;
87 fn mut_refs(p1: &mut Self) -> &mut Self;
88 fn nested(p1: Box<Self>, p2: (&u8, &Self));
89 fn vals(r: Self) -> Self;
95 impl SelfTrait for Bad {
96 fn refs(p1: &Bad) -> &Bad {
100 fn ref_refs<'a>(p1: &'a &'a Bad) -> &'a &'a Bad {
104 fn mut_refs(p1: &mut Bad) -> &mut Bad {
108 fn nested(_p1: Box<Bad>, _p2: (&u8, &Bad)) {}
110 fn vals(_: Bad) -> Bad {
118 fn mul(self, rhs: Bad) -> Bad {
126 impl SelfTrait for Good {
127 fn refs(p1: &Self) -> &Self {
131 fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self {
135 fn mut_refs(p1: &mut Self) -> &mut Self {
139 fn nested(_p1: Box<Self>, _p2: (&u8, &Self)) {}
141 fn vals(_: Self) -> Self {
149 fn mul(self, rhs: Self) -> Self {
155 fn refs(p1: &u8) -> &u8;
156 fn ref_refs<'a>(p1: &'a &'a u8) -> &'a &'a u8;
157 fn mut_refs(p1: &mut u8) -> &mut u8;
158 fn nested(p1: Box<u8>, p2: (&u8, &u8));
159 fn vals(p1: u8) -> u8;
162 // Using `Self` instead of the type name is OK
163 impl NameTrait for u8 {
164 fn refs(p1: &Self) -> &Self {
168 fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self {
172 fn mut_refs(p1: &mut Self) -> &mut Self {
176 fn nested(_p1: Box<Self>, _p2: (&Self, &Self)) {}
178 fn vals(_: Self) -> Self {
183 // Check that self arg isn't linted
184 impl Clone for Good {
185 fn clone(&self) -> Self {
186 // Note: Not linted and it wouldn't be valid
187 // because "can't use `Self` as a constructor`"
195 fn into_bytes(&self) -> Vec<u8>;
198 // This should not be linted
199 impl IntoBytes for u8 {
200 fn into_bytes(&self) -> Vec<u8> {
210 fn bad(foos: &[Self]) -> impl Iterator<Item = &Foo> {
214 fn good(foos: &[Self]) -> impl Iterator<Item = &Self> {
229 impl Trait<Vec<A>> for Vec<B> {