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.
13 #![warn(clippy::use_self)]
15 #![allow(clippy::should_implement_trait)]
31 impl Default for Foo {
50 impl Default for Foo {
51 fn default() -> Self {
57 //todo the lint does not handle lifetimed struct
58 //the following module should trigger the lint on the third method only
60 struct Foo<'a>{foo_str: &'a str}
63 // Cannot use `Self` as return type, because the function is actually `fn foo<'b>(s: &'b str) -> Foo<'b>`
64 fn foo(s: &str) -> Foo {
67 // cannot replace with `Self`, because that's `Foo<'a>`
68 fn bar() -> Foo<'static> {
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)) {
111 fn vals(_: Bad) -> Bad {
119 fn mul(self, rhs: Bad) -> Bad {
127 impl SelfTrait for Good {
128 fn refs(p1: &Self) -> &Self {
132 fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self {
136 fn mut_refs(p1: &mut Self) -> &mut Self {
140 fn nested(_p1: Box<Self>, _p2: (&u8, &Self)) {
143 fn vals(_: Self) -> Self {
151 fn mul(self, rhs: Self) -> Self {
157 fn refs(p1: &u8) -> &u8;
158 fn ref_refs<'a>(p1: &'a &'a u8) -> &'a &'a u8;
159 fn mut_refs(p1: &mut u8) -> &mut u8;
160 fn nested(p1: Box<u8>, p2: (&u8, &u8));
161 fn vals(p1: u8) -> u8;
164 // Using `Self` instead of the type name is OK
165 impl NameTrait for u8 {
166 fn refs(p1: &Self) -> &Self {
170 fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self {
174 fn mut_refs(p1: &mut Self) -> &mut Self {
178 fn nested(_p1: Box<Self>, _p2: (&Self, &Self)) {
181 fn vals(_: Self) -> Self {
186 // Check that self arg isn't linted
187 impl Clone for Good {
188 fn clone(&self) -> Self {
189 // Note: Not linted and it wouldn't be valid
190 // because "can't use `Self` as a constructor`"
198 fn into_bytes(&self) -> Vec<u8>;
201 // This should not be linted
202 impl IntoBytes for u8 {
203 fn into_bytes(&self) -> Vec<u8> {
213 fn bad(foos: &[Self]) -> impl Iterator<Item=&Foo> {
217 fn good(foos: &[Self]) -> impl Iterator<Item=&Self> {
228 trait Trait<T>: Sized {
232 impl Trait<Vec<A>> for Vec<B> {