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 {
60 // Cannot use `Self` as return type, because the function is actually `fn foo<'b>(s: &'b str) ->
62 fn foo(s: &str) -> Foo {
65 // cannot replace with `Self`, because that's `Foo<'a>`
66 fn bar() -> Foo<'static> {
67 Foo { foo_str: "foo" }
70 // FIXME: the lint does not handle lifetimed struct
71 // `Self` should be applicable here
72 fn clone(&self) -> Foo<'a> {
73 Foo { foo_str: self.foo_str }
78 #[allow(clippy::boxed_local)]
84 fn refs(p1: &Self) -> &Self;
85 fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self;
86 fn mut_refs(p1: &mut Self) -> &mut Self;
87 fn nested(p1: Box<Self>, p2: (&u8, &Self));
88 fn vals(r: Self) -> Self;
94 impl SelfTrait for Bad {
95 fn refs(p1: &Bad) -> &Bad {
99 fn ref_refs<'a>(p1: &'a &'a Bad) -> &'a &'a Bad {
103 fn mut_refs(p1: &mut Bad) -> &mut Bad {
107 fn nested(_p1: Box<Bad>, _p2: (&u8, &Bad)) {}
109 fn vals(_: Bad) -> Bad {
117 fn mul(self, rhs: Bad) -> Bad {
125 impl SelfTrait for Good {
126 fn refs(p1: &Self) -> &Self {
130 fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self {
134 fn mut_refs(p1: &mut Self) -> &mut Self {
138 fn nested(_p1: Box<Self>, _p2: (&u8, &Self)) {}
140 fn vals(_: Self) -> Self {
148 fn mul(self, rhs: Self) -> Self {
154 fn refs(p1: &u8) -> &u8;
155 fn ref_refs<'a>(p1: &'a &'a u8) -> &'a &'a u8;
156 fn mut_refs(p1: &mut u8) -> &mut u8;
157 fn nested(p1: Box<u8>, p2: (&u8, &u8));
158 fn vals(p1: u8) -> u8;
161 // Using `Self` instead of the type name is OK
162 impl NameTrait for u8 {
163 fn refs(p1: &Self) -> &Self {
167 fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self {
171 fn mut_refs(p1: &mut Self) -> &mut Self {
175 fn nested(_p1: Box<Self>, _p2: (&Self, &Self)) {}
177 fn vals(_: Self) -> Self {
182 // Check that self arg isn't linted
183 impl Clone for Good {
184 fn clone(&self) -> Self {
185 // Note: Not linted and it wouldn't be valid
186 // because "can't use `Self` as a constructor`"
194 fn into_bytes(&self) -> Vec<u8>;
197 // This should not be linted
198 impl IntoBytes for u8 {
199 fn into_bytes(&self) -> Vec<u8> {
209 fn bad(foos: &[Self]) -> impl Iterator<Item = &Foo> {
213 fn good(foos: &[Self]) -> impl Iterator<Item = &Self> {
223 pub fn ts() -> Self {
230 macro_rules! use_self_expand {
241 use_self_expand!(); // Should lint in local macros
249 use self::Foo; // Can't use Self here
251 foo: Foo, // Foo != Self
267 use self::Enum::*; // Issue 3425
268 static STATIC: Enum = Enum::A; // Can't use Self as type
282 impl Trait<Vec<A>> for Vec<B> {