2019-01-22 14:16:54 +00:00
|
|
|
// run-rustfix
|
2020-03-10 21:35:07 +00:00
|
|
|
// edition:2018
|
2019-01-22 14:16:54 +00:00
|
|
|
|
|
|
|
#![warn(clippy::use_self)]
|
|
|
|
#![allow(dead_code)]
|
2020-12-19 13:50:45 +00:00
|
|
|
#![allow(clippy::should_implement_trait, clippy::upper_case_acronyms)]
|
2019-01-22 14:16:54 +00:00
|
|
|
|
|
|
|
fn main() {}
|
|
|
|
|
|
|
|
mod use_self {
|
|
|
|
struct Foo {}
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn new() -> Self {
|
|
|
|
Self {}
|
|
|
|
}
|
|
|
|
fn test() -> Self {
|
2020-10-09 22:26:12 +00:00
|
|
|
// FIXME: applicable here
|
2020-04-26 00:48:02 +00:00
|
|
|
Foo::new()
|
2019-01-22 14:16:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Foo {
|
2020-10-09 22:26:12 +00:00
|
|
|
// FIXME: applicable here
|
|
|
|
fn default() -> Foo {
|
2020-04-26 00:48:02 +00:00
|
|
|
// FIXME: applicable here
|
|
|
|
Foo::new()
|
2019-01-22 14:16:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod better {
|
|
|
|
struct Foo {}
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn new() -> Self {
|
|
|
|
Self {}
|
|
|
|
}
|
|
|
|
fn test() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Foo {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod lifetimes {
|
|
|
|
struct Foo<'a> {
|
|
|
|
foo_str: &'a str,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Foo<'a> {
|
|
|
|
// Cannot use `Self` as return type, because the function is actually `fn foo<'b>(s: &'b str) ->
|
|
|
|
// Foo<'b>`
|
|
|
|
fn foo(s: &str) -> Foo {
|
|
|
|
Foo { foo_str: s }
|
|
|
|
}
|
|
|
|
// cannot replace with `Self`, because that's `Foo<'a>`
|
|
|
|
fn bar() -> Foo<'static> {
|
|
|
|
Foo { foo_str: "foo" }
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: the lint does not handle lifetimed struct
|
|
|
|
// `Self` should be applicable here
|
|
|
|
fn clone(&self) -> Foo<'a> {
|
|
|
|
Foo { foo_str: self.foo_str }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod issue2894 {
|
|
|
|
trait IntoBytes {
|
2020-12-10 16:00:55 +00:00
|
|
|
#[allow(clippy::wrong_self_convention)]
|
2019-01-22 14:16:54 +00:00
|
|
|
fn into_bytes(&self) -> Vec<u8>;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should not be linted
|
|
|
|
impl IntoBytes for u8 {
|
|
|
|
fn into_bytes(&self) -> Vec<u8> {
|
|
|
|
vec![*self]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod existential {
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl Foo {
|
2020-04-26 00:48:02 +00:00
|
|
|
// FIXME:
|
|
|
|
// TyKind::Def (used for `impl Trait` types) does not include type parameters yet.
|
|
|
|
// See documentation in rustc_hir::hir::TyKind.
|
|
|
|
// The hir tree walk stops at `impl Iterator` level and does not inspect &Foo.
|
|
|
|
fn bad(foos: &[Self]) -> impl Iterator<Item = &Foo> {
|
2019-01-22 14:16:54 +00:00
|
|
|
foos.iter()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn good(foos: &[Self]) -> impl Iterator<Item = &Self> {
|
|
|
|
foos.iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod tuple_structs {
|
|
|
|
pub struct TS(i32);
|
|
|
|
|
|
|
|
impl TS {
|
|
|
|
pub fn ts() -> Self {
|
|
|
|
Self(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod macros {
|
|
|
|
macro_rules! use_self_expand {
|
|
|
|
() => {
|
|
|
|
fn new() -> Self {
|
|
|
|
Self {}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Foo {}
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
use_self_expand!(); // Should lint in local macros
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod nesting {
|
|
|
|
struct Foo {}
|
|
|
|
impl Foo {
|
|
|
|
fn foo() {
|
2019-04-01 05:19:05 +00:00
|
|
|
#[allow(unused_imports)]
|
2019-01-22 14:16:54 +00:00
|
|
|
use self::Foo; // Can't use Self here
|
|
|
|
struct Bar {
|
|
|
|
foo: Foo, // Foo != Self
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Bar {
|
|
|
|
fn bar() -> Self {
|
|
|
|
Self { foo: Foo {} }
|
|
|
|
}
|
|
|
|
}
|
2019-03-11 14:24:49 +00:00
|
|
|
|
|
|
|
// Can't use Self here
|
|
|
|
fn baz() -> Foo {
|
|
|
|
Foo {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should lint here
|
|
|
|
fn baz() -> Self {
|
|
|
|
Self {}
|
2019-01-22 14:16:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Enum {
|
|
|
|
A,
|
2019-07-31 00:24:28 +00:00
|
|
|
B(u64),
|
2019-08-01 05:09:57 +00:00
|
|
|
C { field: bool },
|
2019-01-22 14:16:54 +00:00
|
|
|
}
|
|
|
|
impl Enum {
|
|
|
|
fn method() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use self::Enum::*; // Issue 3425
|
|
|
|
static STATIC: Enum = Enum::A; // Can't use Self as type
|
|
|
|
}
|
2019-07-31 00:24:28 +00:00
|
|
|
|
|
|
|
fn method2() {
|
|
|
|
let _ = Self::B(42);
|
|
|
|
let _ = Self::C { field: true };
|
|
|
|
let _ = Self::A;
|
|
|
|
}
|
2019-01-22 14:16:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod issue3410 {
|
|
|
|
|
|
|
|
struct A;
|
|
|
|
struct B;
|
|
|
|
|
|
|
|
trait Trait<T> {
|
2020-04-26 00:48:02 +00:00
|
|
|
fn a(v: T) -> Self;
|
2019-01-22 14:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Trait<Vec<A>> for Vec<B> {
|
2020-04-26 00:48:02 +00:00
|
|
|
fn a(_: Vec<A>) -> Self {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Trait<Vec<A>> for Vec<T>
|
|
|
|
where
|
|
|
|
T: Trait<B>,
|
|
|
|
{
|
|
|
|
fn a(v: Vec<A>) -> Self {
|
|
|
|
<Vec<B>>::a(v).into_iter().map(Trait::a).collect()
|
|
|
|
}
|
2019-01-22 14:16:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::no_effect, path_statements)]
|
|
|
|
mod rustfix {
|
|
|
|
mod nested {
|
|
|
|
pub struct A {}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl nested::A {
|
|
|
|
const A: bool = true;
|
|
|
|
|
|
|
|
fn fun_1() {}
|
|
|
|
|
|
|
|
fn fun_2() {
|
2020-10-09 22:26:12 +00:00
|
|
|
// FIXME: applicable here
|
2020-04-26 00:48:02 +00:00
|
|
|
nested::A::fun_1();
|
2020-10-09 22:26:12 +00:00
|
|
|
// FIXME: applicable here
|
2020-04-26 00:48:02 +00:00
|
|
|
nested::A::A;
|
2019-01-22 14:16:54 +00:00
|
|
|
|
|
|
|
Self {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-12 06:47:11 +00:00
|
|
|
|
|
|
|
mod issue3567 {
|
|
|
|
struct TestStruct {}
|
|
|
|
impl TestStruct {
|
|
|
|
fn from_something() -> Self {
|
|
|
|
Self {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trait Test {
|
|
|
|
fn test() -> TestStruct;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Test for TestStruct {
|
|
|
|
fn test() -> TestStruct {
|
2020-04-26 00:48:02 +00:00
|
|
|
// FIXME: applicable here
|
|
|
|
TestStruct::from_something()
|
2019-09-12 06:47:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-06 05:13:43 +00:00
|
|
|
|
2019-11-07 03:59:13 +00:00
|
|
|
mod paths_created_by_lowering {
|
|
|
|
use std::ops::Range;
|
|
|
|
|
2019-11-06 05:13:43 +00:00
|
|
|
struct S {}
|
|
|
|
|
|
|
|
impl S {
|
|
|
|
const A: usize = 0;
|
|
|
|
const B: usize = 1;
|
|
|
|
|
2020-04-26 00:48:02 +00:00
|
|
|
// FIXME: applicable here
|
|
|
|
async fn g() -> S {
|
2019-11-07 03:59:13 +00:00
|
|
|
Self {}
|
|
|
|
}
|
|
|
|
|
2019-11-06 05:13:43 +00:00
|
|
|
fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
|
2020-04-26 00:48:02 +00:00
|
|
|
// FIXME: applicable here twice
|
|
|
|
&p[S::A..S::B]
|
2019-11-06 05:13:43 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-07 03:59:13 +00:00
|
|
|
|
|
|
|
trait T {
|
|
|
|
fn f<'a>(&self, p: &'a [u8]) -> &'a [u8];
|
|
|
|
}
|
|
|
|
|
|
|
|
impl T for Range<u8> {
|
|
|
|
fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
|
|
|
|
&p[0..1]
|
|
|
|
}
|
|
|
|
}
|
2019-11-06 05:13:43 +00:00
|
|
|
}
|
2020-04-26 00:48:02 +00:00
|
|
|
|
|
|
|
// reused from #1997
|
|
|
|
mod generics {
|
|
|
|
struct Foo<T> {
|
|
|
|
value: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Foo<T> {
|
|
|
|
// `Self` is applicable here
|
|
|
|
fn foo(value: T) -> Self {
|
|
|
|
Self { value }
|
|
|
|
}
|
|
|
|
|
|
|
|
// `Cannot` use `Self` as a return type as the generic types are different
|
|
|
|
fn bar(value: i32) -> Foo<i32> {
|
|
|
|
Foo { value }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod issue4140 {
|
|
|
|
pub struct Error<From, To> {
|
|
|
|
_from: From,
|
|
|
|
_too: To,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait From<T> {
|
|
|
|
type From;
|
|
|
|
type To;
|
|
|
|
|
|
|
|
fn from(value: T) -> Self;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait TryFrom<T>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
|
|
|
type From;
|
|
|
|
type To;
|
|
|
|
|
|
|
|
fn try_from(value: T) -> Result<Self, Error<Self::From, Self::To>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F, T> TryFrom<F> for T
|
|
|
|
where
|
|
|
|
T: From<F>,
|
|
|
|
{
|
|
|
|
type From = Self;
|
|
|
|
type To = Self;
|
|
|
|
|
|
|
|
fn try_from(value: F) -> Result<Self, Error<Self::From, Self::To>> {
|
|
|
|
Ok(From::from(value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<bool> for i64 {
|
|
|
|
type From = bool;
|
|
|
|
type To = Self;
|
|
|
|
|
|
|
|
fn from(value: bool) -> Self {
|
|
|
|
if value {
|
|
|
|
100
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod issue2843 {
|
|
|
|
trait Foo {
|
|
|
|
type Bar;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Foo for usize {
|
|
|
|
type Bar = u8;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Foo> Foo for Option<T> {
|
|
|
|
type Bar = Option<T::Bar>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod issue3859 {
|
|
|
|
pub struct Foo;
|
|
|
|
pub struct Bar([usize; 3]);
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
pub const BAR: usize = 3;
|
|
|
|
|
|
|
|
pub fn foo() {
|
|
|
|
const _X: usize = Foo::BAR;
|
|
|
|
// const _Y: usize = Self::BAR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod issue4305 {
|
|
|
|
trait Foo: 'static {}
|
|
|
|
|
|
|
|
struct Bar;
|
|
|
|
|
|
|
|
impl Foo for Bar {}
|
|
|
|
|
|
|
|
impl<T: Foo> From<T> for Box<dyn Foo> {
|
|
|
|
fn from(t: T) -> Self {
|
|
|
|
// FIXME: applicable here
|
|
|
|
Box::new(t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod lint_at_item_level {
|
|
|
|
struct Foo {}
|
|
|
|
|
|
|
|
#[allow(clippy::use_self)]
|
|
|
|
impl Foo {
|
|
|
|
fn new() -> Foo {
|
|
|
|
Foo {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::use_self)]
|
|
|
|
impl Default for Foo {
|
|
|
|
fn default() -> Foo {
|
|
|
|
Foo::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod lint_at_impl_item_level {
|
|
|
|
struct Foo {}
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
#[allow(clippy::use_self)]
|
|
|
|
fn new() -> Foo {
|
|
|
|
Foo {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Foo {
|
|
|
|
#[allow(clippy::use_self)]
|
|
|
|
fn default() -> Foo {
|
|
|
|
Foo::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod issue4734 {
|
|
|
|
#[repr(C, packed)]
|
|
|
|
pub struct X {
|
|
|
|
pub x: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<X> for u32 {
|
|
|
|
fn from(c: X) -> Self {
|
|
|
|
unsafe { core::mem::transmute(c) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod nested_paths {
|
|
|
|
use std::convert::Into;
|
|
|
|
mod submod {
|
|
|
|
pub struct B {}
|
|
|
|
pub struct C {}
|
|
|
|
|
|
|
|
impl Into<C> for B {
|
|
|
|
fn into(self) -> C {
|
|
|
|
C {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct A<T> {
|
|
|
|
t: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> A<T> {
|
|
|
|
fn new<V: Into<T>>(v: V) -> Self {
|
|
|
|
Self { t: Into::into(v) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl A<submod::C> {
|
|
|
|
fn test() -> Self {
|
|
|
|
// FIXME: applicable here
|
|
|
|
A::new::<submod::B>(submod::B {})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|