2019-01-22 14:16:54 +00:00
|
|
|
// run-rustfix
|
2021-02-25 10:25:22 +00:00
|
|
|
// aux-build:proc_macro_derive.rs
|
2019-01-22 14:16:54 +00:00
|
|
|
|
2018-07-28 15:34:52 +00:00
|
|
|
#![warn(clippy::use_self)]
|
2022-02-21 22:38:22 +00:00
|
|
|
#![allow(dead_code, unreachable_code)]
|
2021-06-25 20:34:45 +00:00
|
|
|
#![allow(
|
|
|
|
clippy::should_implement_trait,
|
|
|
|
clippy::upper_case_acronyms,
|
|
|
|
clippy::from_over_into,
|
2021-07-29 10:10:18 +00:00
|
|
|
clippy::self_named_constructors
|
2021-06-25 20:34:45 +00:00
|
|
|
)]
|
2021-02-25 10:25:22 +00:00
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate proc_macro_derive;
|
2017-07-28 11:28:07 +00:00
|
|
|
|
|
|
|
fn main() {}
|
|
|
|
|
|
|
|
mod use_self {
|
2022-03-27 12:41:09 +00:00
|
|
|
struct Foo;
|
2017-07-28 11:28:07 +00:00
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn new() -> Foo {
|
|
|
|
Foo {}
|
|
|
|
}
|
|
|
|
fn test() -> Foo {
|
|
|
|
Foo::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Foo {
|
|
|
|
fn default() -> Foo {
|
|
|
|
Foo::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod better {
|
2022-03-27 12:41:09 +00:00
|
|
|
struct Foo;
|
2017-07-28 11:28:07 +00:00
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn new() -> Self {
|
|
|
|
Self {}
|
|
|
|
}
|
|
|
|
fn test() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Foo {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-21 22:18:37 +00:00
|
|
|
|
|
|
|
mod lifetimes {
|
2018-12-09 22:26:16 +00:00
|
|
|
struct Foo<'a> {
|
|
|
|
foo_str: &'a str,
|
|
|
|
}
|
2017-08-21 22:18:37 +00:00
|
|
|
|
|
|
|
impl<'a> Foo<'a> {
|
2018-12-09 22:26:16 +00:00
|
|
|
// Cannot use `Self` as return type, because the function is actually `fn foo<'b>(s: &'b str) ->
|
|
|
|
// Foo<'b>`
|
2017-08-21 22:18:37 +00:00
|
|
|
fn foo(s: &str) -> Foo {
|
|
|
|
Foo { foo_str: s }
|
|
|
|
}
|
|
|
|
// cannot replace with `Self`, because that's `Foo<'a>`
|
|
|
|
fn bar() -> Foo<'static> {
|
2018-12-09 22:26:16 +00:00
|
|
|
Foo { foo_str: "foo" }
|
2017-08-21 22:18:37 +00:00
|
|
|
}
|
|
|
|
|
2018-12-28 12:41:33 +00:00
|
|
|
// FIXME: the lint does not handle lifetimed struct
|
2018-12-27 08:54:19 +00:00
|
|
|
// `Self` should be applicable here
|
2017-08-21 22:18:37 +00:00
|
|
|
fn clone(&self) -> Foo<'a> {
|
2018-12-09 22:26:16 +00:00
|
|
|
Foo { foo_str: self.foo_str }
|
2017-08-21 22:18:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-14 10:18:50 +00:00
|
|
|
|
|
|
|
mod issue2894 {
|
|
|
|
trait IntoBytes {
|
2021-03-18 09:27:55 +00:00
|
|
|
fn to_bytes(self) -> Vec<u8>;
|
2018-07-14 10:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This should not be linted
|
|
|
|
impl IntoBytes for u8 {
|
2021-03-18 09:27:55 +00:00
|
|
|
fn to_bytes(self) -> Vec<u8> {
|
|
|
|
vec![self]
|
2018-07-14 10:18:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-25 03:28:54 +00:00
|
|
|
|
|
|
|
mod existential {
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl Foo {
|
2021-02-25 10:25:22 +00:00
|
|
|
fn bad(foos: &[Foo]) -> impl Iterator<Item = &Foo> {
|
2018-10-25 03:28:54 +00:00
|
|
|
foos.iter()
|
|
|
|
}
|
|
|
|
|
2018-12-09 22:26:16 +00:00
|
|
|
fn good(foos: &[Self]) -> impl Iterator<Item = &Self> {
|
2018-10-25 03:28:54 +00:00
|
|
|
foos.iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-10 08:46:21 +00:00
|
|
|
|
2018-12-27 16:27:42 +00:00
|
|
|
mod tuple_structs {
|
|
|
|
pub struct TS(i32);
|
|
|
|
|
|
|
|
impl TS {
|
|
|
|
pub fn ts() -> Self {
|
|
|
|
TS(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-04 10:31:28 +00:00
|
|
|
mod macros {
|
|
|
|
macro_rules! use_self_expand {
|
|
|
|
() => {
|
|
|
|
fn new() -> Foo {
|
|
|
|
Foo {}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-03-27 12:41:09 +00:00
|
|
|
struct Foo;
|
2019-01-04 10:31:28 +00:00
|
|
|
|
|
|
|
impl Foo {
|
2021-02-25 10:25:22 +00:00
|
|
|
use_self_expand!(); // Should not lint in local macros
|
2019-01-04 10:31:28 +00:00
|
|
|
}
|
2021-02-25 10:25:22 +00:00
|
|
|
|
|
|
|
#[derive(StructAUseSelf)] // Should not lint in derives
|
|
|
|
struct A;
|
2019-01-04 10:31:28 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 14:05:04 +00:00
|
|
|
mod nesting {
|
2022-03-27 12:41:09 +00:00
|
|
|
struct Foo;
|
2019-01-06 14:05:04 +00:00
|
|
|
impl Foo {
|
|
|
|
fn foo() {
|
2019-04-01 05:19:05 +00:00
|
|
|
#[allow(unused_imports)]
|
2019-01-06 14:05:04 +00:00
|
|
|
use self::Foo; // Can't use Self here
|
|
|
|
struct Bar {
|
|
|
|
foo: Foo, // Foo != Self
|
|
|
|
}
|
2019-01-07 13:11:53 +00:00
|
|
|
|
|
|
|
impl Bar {
|
|
|
|
fn bar() -> Bar {
|
2019-01-07 13:38:01 +00:00
|
|
|
Bar { foo: Foo {} }
|
2019-01-07 13:11:53 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-11 14:24:49 +00:00
|
|
|
|
|
|
|
// Can't use Self here
|
|
|
|
fn baz() -> Foo {
|
|
|
|
Foo {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should lint here
|
|
|
|
fn baz() -> Foo {
|
|
|
|
Foo {}
|
2019-01-06 14:05:04 +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-06 14:05:04 +00:00
|
|
|
}
|
|
|
|
impl Enum {
|
|
|
|
fn method() {
|
2019-01-22 14:16:54 +00:00
|
|
|
#[allow(unused_imports)]
|
2019-01-07 13:11:53 +00:00
|
|
|
use self::Enum::*; // Issue 3425
|
2019-01-06 14:05:04 +00:00
|
|
|
static STATIC: Enum = Enum::A; // Can't use Self as type
|
|
|
|
}
|
2019-07-31 00:24:28 +00:00
|
|
|
|
|
|
|
fn method2() {
|
|
|
|
let _ = Enum::B(42);
|
|
|
|
let _ = Enum::C { field: true };
|
|
|
|
let _ = Enum::A;
|
|
|
|
}
|
2019-01-06 14:05:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-10 08:46:21 +00:00
|
|
|
mod issue3410 {
|
|
|
|
|
|
|
|
struct A;
|
|
|
|
struct B;
|
|
|
|
|
2018-11-13 04:15:33 +00:00
|
|
|
trait Trait<T> {
|
2021-02-25 10:25:22 +00:00
|
|
|
fn a(v: T) -> Self;
|
2018-11-10 08:46:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Trait<Vec<A>> for Vec<B> {
|
2021-02-25 10:25:22 +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()
|
|
|
|
}
|
2018-11-10 08:46:21 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-21 12:06:32 +00:00
|
|
|
|
2019-01-22 14:16:54 +00:00
|
|
|
#[allow(clippy::no_effect, path_statements)]
|
2019-01-21 12:06:32 +00:00
|
|
|
mod rustfix {
|
|
|
|
mod nested {
|
2022-03-27 12:41:09 +00:00
|
|
|
pub struct A;
|
2019-01-21 12:06:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl nested::A {
|
|
|
|
const A: bool = true;
|
|
|
|
|
|
|
|
fn fun_1() {}
|
|
|
|
|
|
|
|
fn fun_2() {
|
|
|
|
nested::A::fun_1();
|
|
|
|
nested::A::A;
|
|
|
|
|
|
|
|
nested::A {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-12 06:47:11 +00:00
|
|
|
|
|
|
|
mod issue3567 {
|
2022-03-27 12:41:09 +00:00
|
|
|
struct TestStruct;
|
2019-09-12 06:47:11 +00:00
|
|
|
impl TestStruct {
|
|
|
|
fn from_something() -> Self {
|
|
|
|
Self {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trait Test {
|
|
|
|
fn test() -> TestStruct;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Test for TestStruct {
|
|
|
|
fn test() -> TestStruct {
|
|
|
|
TestStruct::from_something()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
|
2022-03-27 12:41:09 +00:00
|
|
|
struct S;
|
2019-11-06 05:13:43 +00:00
|
|
|
|
|
|
|
impl S {
|
|
|
|
const A: usize = 0;
|
|
|
|
const B: usize = 1;
|
|
|
|
|
2019-11-07 03:59:13 +00:00
|
|
|
async fn g() -> S {
|
|
|
|
S {}
|
|
|
|
}
|
|
|
|
|
2019-11-06 05:13:43 +00:00
|
|
|
fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
|
|
|
|
&p[S::A..S::B]
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
2021-02-25 10:25:22 +00:00
|
|
|
|
|
|
|
// reused from #1997
|
|
|
|
mod generics {
|
|
|
|
struct Foo<T> {
|
|
|
|
value: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Foo<T> {
|
|
|
|
// `Self` is applicable here
|
|
|
|
fn foo(value: T) -> Foo<T> {
|
2021-05-27 21:25:37 +00:00
|
|
|
Foo::<T> { value }
|
2021-02-25 10:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// `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>>;
|
|
|
|
}
|
|
|
|
|
2021-03-15 20:52:57 +00:00
|
|
|
// FIXME: Suggested fix results in infinite recursion.
|
|
|
|
// impl<F, T> TryFrom<F> for T
|
|
|
|
// where
|
|
|
|
// T: From<F>,
|
|
|
|
// {
|
|
|
|
// type From = Self::From;
|
|
|
|
// type To = Self::To;
|
|
|
|
|
|
|
|
// fn try_from(value: F) -> Result<Self, Error<Self::From, Self::To>> {
|
|
|
|
// Ok(From::from(value))
|
|
|
|
// }
|
|
|
|
// }
|
2021-02-25 10:25:22 +00:00
|
|
|
|
|
|
|
impl From<bool> for i64 {
|
|
|
|
type From = bool;
|
|
|
|
type To = Self;
|
|
|
|
|
|
|
|
fn from(value: bool) -> Self {
|
2021-03-12 14:30:50 +00:00
|
|
|
if value { 100 } else { 0 }
|
2021-02-25 10:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
Box::new(t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod lint_at_item_level {
|
2022-03-27 12:41:09 +00:00
|
|
|
struct Foo;
|
2021-02-25 10:25:22 +00:00
|
|
|
|
|
|
|
#[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 {
|
2022-03-27 12:41:09 +00:00
|
|
|
struct Foo;
|
2021-02-25 10:25:22 +00:00
|
|
|
|
|
|
|
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 {
|
2022-03-27 12:41:09 +00:00
|
|
|
pub struct B;
|
|
|
|
pub struct C;
|
2021-02-25 10:25:22 +00:00
|
|
|
|
|
|
|
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 {
|
|
|
|
A::new::<submod::B>(submod::B {})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-12 14:30:50 +00:00
|
|
|
|
|
|
|
mod issue6818 {
|
|
|
|
#[derive(serde::Deserialize)]
|
|
|
|
struct A {
|
|
|
|
a: i32,
|
|
|
|
}
|
|
|
|
}
|
2021-05-13 19:40:20 +00:00
|
|
|
|
|
|
|
mod issue7206 {
|
|
|
|
struct MyStruct<const C: char>;
|
|
|
|
impl From<MyStruct<'a'>> for MyStruct<'b'> {
|
|
|
|
fn from(_s: MyStruct<'a'>) -> Self {
|
|
|
|
Self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// keep linting non-`Const` generic args
|
|
|
|
struct S<'a> {
|
|
|
|
inner: &'a str,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct S2<T> {
|
|
|
|
inner: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> S2<T> {
|
|
|
|
fn new() -> Self {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> S2<S<'a>> {
|
|
|
|
fn new_again() -> Self {
|
|
|
|
S2::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-27 21:25:37 +00:00
|
|
|
|
|
|
|
mod self_is_ty_param {
|
|
|
|
trait Trait {
|
|
|
|
type Type;
|
|
|
|
type Hi;
|
|
|
|
|
|
|
|
fn test();
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<I> Trait for I
|
|
|
|
where
|
|
|
|
I: Iterator,
|
|
|
|
I::Item: Trait, // changing this to Self would require <Self as Iterator>
|
|
|
|
{
|
|
|
|
type Type = I;
|
|
|
|
type Hi = I::Item;
|
|
|
|
|
|
|
|
fn test() {
|
|
|
|
let _: I::Item;
|
|
|
|
let _: I; // this could lint, but is questionable
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-21 22:38:22 +00:00
|
|
|
|
|
|
|
mod use_self_in_pat {
|
|
|
|
enum Foo {
|
|
|
|
Bar,
|
|
|
|
Baz,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn do_stuff(self) {
|
|
|
|
match self {
|
|
|
|
Foo::Bar => unimplemented!(),
|
|
|
|
Foo::Baz => unimplemented!(),
|
|
|
|
}
|
|
|
|
match Some(1) {
|
|
|
|
Some(_) => unimplemented!(),
|
|
|
|
None => unimplemented!(),
|
|
|
|
}
|
|
|
|
if let Foo::Bar = self {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|