|
1 | 1 | // run-rustfix
|
2 | 2 | // edition:2018
|
| 3 | +// aux-build:proc_macro_derive.rs |
3 | 4 |
|
4 | 5 | #![warn(clippy::use_self)]
|
5 | 6 | #![allow(dead_code)]
|
6 |
| -#![allow(clippy::should_implement_trait)] |
| 7 | +#![allow(clippy::should_implement_trait, clippy::from_over_into)] |
| 8 | + |
| 9 | +#[macro_use] |
| 10 | +extern crate proc_macro_derive; |
7 | 11 |
|
8 | 12 | fn main() {}
|
9 | 13 |
|
@@ -71,13 +75,12 @@ mod lifetimes {
|
71 | 75 |
|
72 | 76 | mod issue2894 {
|
73 | 77 | trait IntoBytes {
|
74 |
| - #[allow(clippy::wrong_self_convention)] |
75 |
| - fn into_bytes(&self) -> Vec<u8>; |
| 78 | + fn to_bytes(&self) -> Vec<u8>; |
76 | 79 | }
|
77 | 80 |
|
78 | 81 | // This should not be linted
|
79 | 82 | impl IntoBytes for u8 {
|
80 |
| - fn into_bytes(&self) -> Vec<u8> { |
| 83 | + fn to_bytes(&self) -> Vec<u8> { |
81 | 84 | vec![*self]
|
82 | 85 | }
|
83 | 86 | }
|
@@ -110,17 +113,20 @@ mod tuple_structs {
|
110 | 113 | mod macros {
|
111 | 114 | macro_rules! use_self_expand {
|
112 | 115 | () => {
|
113 |
| - fn new() -> Self { |
114 |
| - Self {} |
| 116 | + fn new() -> Foo { |
| 117 | + Foo {} |
115 | 118 | }
|
116 | 119 | };
|
117 | 120 | }
|
118 | 121 |
|
119 | 122 | struct Foo {}
|
120 | 123 |
|
121 | 124 | impl Foo {
|
122 |
| - use_self_expand!(); // Should lint in local macros |
| 125 | + use_self_expand!(); // Should not lint in local macros |
123 | 126 | }
|
| 127 | + |
| 128 | + #[derive(StructAUseSelf)] // Should not lint in derives |
| 129 | + struct A; |
124 | 130 | }
|
125 | 131 |
|
126 | 132 | mod nesting {
|
@@ -177,11 +183,22 @@ mod issue3410 {
|
177 | 183 | struct B;
|
178 | 184 |
|
179 | 185 | trait Trait<T> {
|
180 |
| - fn a(v: T); |
| 186 | + fn a(v: T) -> Self; |
181 | 187 | }
|
182 | 188 |
|
183 | 189 | impl Trait<Vec<A>> for Vec<B> {
|
184 |
| - fn a(_: Vec<A>) {} |
| 190 | + fn a(_: Vec<A>) -> Self { |
| 191 | + unimplemented!() |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + impl<T> Trait<Vec<A>> for Vec<T> |
| 196 | + where |
| 197 | + T: Trait<B>, |
| 198 | + { |
| 199 | + fn a(v: Vec<A>) -> Self { |
| 200 | + <Vec<B>>::a(v).into_iter().map(Trait::a).collect() |
| 201 | + } |
185 | 202 | }
|
186 | 203 | }
|
187 | 204 |
|
@@ -252,3 +269,192 @@ mod paths_created_by_lowering {
|
252 | 269 | }
|
253 | 270 | }
|
254 | 271 | }
|
| 272 | + |
| 273 | +// reused from #1997 |
| 274 | +mod generics { |
| 275 | + struct Foo<T> { |
| 276 | + value: T, |
| 277 | + } |
| 278 | + |
| 279 | + impl<T> Foo<T> { |
| 280 | + // `Self` is applicable here |
| 281 | + fn foo(value: T) -> Self { |
| 282 | + Self { value } |
| 283 | + } |
| 284 | + |
| 285 | + // `Cannot` use `Self` as a return type as the generic types are different |
| 286 | + fn bar(value: i32) -> Foo<i32> { |
| 287 | + Foo { value } |
| 288 | + } |
| 289 | + } |
| 290 | +} |
| 291 | + |
| 292 | +mod issue4140 { |
| 293 | + pub struct Error<From, To> { |
| 294 | + _from: From, |
| 295 | + _too: To, |
| 296 | + } |
| 297 | + |
| 298 | + pub trait From<T> { |
| 299 | + type From; |
| 300 | + type To; |
| 301 | + |
| 302 | + fn from(value: T) -> Self; |
| 303 | + } |
| 304 | + |
| 305 | + pub trait TryFrom<T> |
| 306 | + where |
| 307 | + Self: Sized, |
| 308 | + { |
| 309 | + type From; |
| 310 | + type To; |
| 311 | + |
| 312 | + fn try_from(value: T) -> Result<Self, Error<Self::From, Self::To>>; |
| 313 | + } |
| 314 | + |
| 315 | + impl<F, T> TryFrom<F> for T |
| 316 | + where |
| 317 | + T: From<F>, |
| 318 | + { |
| 319 | + type From = Self; |
| 320 | + type To = Self; |
| 321 | + |
| 322 | + fn try_from(value: F) -> Result<Self, Error<Self::From, Self::To>> { |
| 323 | + Ok(From::from(value)) |
| 324 | + } |
| 325 | + } |
| 326 | + |
| 327 | + impl From<bool> for i64 { |
| 328 | + type From = bool; |
| 329 | + type To = Self; |
| 330 | + |
| 331 | + fn from(value: bool) -> Self { |
| 332 | + if value { |
| 333 | + 100 |
| 334 | + } else { |
| 335 | + 0 |
| 336 | + } |
| 337 | + } |
| 338 | + } |
| 339 | +} |
| 340 | + |
| 341 | +mod issue2843 { |
| 342 | + trait Foo { |
| 343 | + type Bar; |
| 344 | + } |
| 345 | + |
| 346 | + impl Foo for usize { |
| 347 | + type Bar = u8; |
| 348 | + } |
| 349 | + |
| 350 | + impl<T: Foo> Foo for Option<T> { |
| 351 | + type Bar = Option<T::Bar>; |
| 352 | + } |
| 353 | +} |
| 354 | + |
| 355 | +mod issue3859 { |
| 356 | + pub struct Foo; |
| 357 | + pub struct Bar([usize; 3]); |
| 358 | + |
| 359 | + impl Foo { |
| 360 | + pub const BAR: usize = 3; |
| 361 | + |
| 362 | + pub fn foo() { |
| 363 | + const _X: usize = Foo::BAR; |
| 364 | + // const _Y: usize = Self::BAR; |
| 365 | + } |
| 366 | + } |
| 367 | +} |
| 368 | + |
| 369 | +mod issue4305 { |
| 370 | + trait Foo: 'static {} |
| 371 | + |
| 372 | + struct Bar; |
| 373 | + |
| 374 | + impl Foo for Bar {} |
| 375 | + |
| 376 | + impl<T: Foo> From<T> for Box<dyn Foo> { |
| 377 | + fn from(t: T) -> Self { |
| 378 | + Box::new(t) |
| 379 | + } |
| 380 | + } |
| 381 | +} |
| 382 | + |
| 383 | +mod lint_at_item_level { |
| 384 | + struct Foo {} |
| 385 | + |
| 386 | + #[allow(clippy::use_self)] |
| 387 | + impl Foo { |
| 388 | + fn new() -> Foo { |
| 389 | + Foo {} |
| 390 | + } |
| 391 | + } |
| 392 | + |
| 393 | + #[allow(clippy::use_self)] |
| 394 | + impl Default for Foo { |
| 395 | + fn default() -> Foo { |
| 396 | + Foo::new() |
| 397 | + } |
| 398 | + } |
| 399 | +} |
| 400 | + |
| 401 | +mod lint_at_impl_item_level { |
| 402 | + struct Foo {} |
| 403 | + |
| 404 | + impl Foo { |
| 405 | + #[allow(clippy::use_self)] |
| 406 | + fn new() -> Foo { |
| 407 | + Foo {} |
| 408 | + } |
| 409 | + } |
| 410 | + |
| 411 | + impl Default for Foo { |
| 412 | + #[allow(clippy::use_self)] |
| 413 | + fn default() -> Foo { |
| 414 | + Foo::new() |
| 415 | + } |
| 416 | + } |
| 417 | +} |
| 418 | + |
| 419 | +mod issue4734 { |
| 420 | + #[repr(C, packed)] |
| 421 | + pub struct X { |
| 422 | + pub x: u32, |
| 423 | + } |
| 424 | + |
| 425 | + impl From<X> for u32 { |
| 426 | + fn from(c: X) -> Self { |
| 427 | + unsafe { core::mem::transmute(c) } |
| 428 | + } |
| 429 | + } |
| 430 | +} |
| 431 | + |
| 432 | +mod nested_paths { |
| 433 | + use std::convert::Into; |
| 434 | + mod submod { |
| 435 | + pub struct B {} |
| 436 | + pub struct C {} |
| 437 | + |
| 438 | + impl Into<C> for B { |
| 439 | + fn into(self) -> C { |
| 440 | + C {} |
| 441 | + } |
| 442 | + } |
| 443 | + } |
| 444 | + |
| 445 | + struct A<T> { |
| 446 | + t: T, |
| 447 | + } |
| 448 | + |
| 449 | + impl<T> A<T> { |
| 450 | + fn new<V: Into<T>>(v: V) -> Self { |
| 451 | + Self { t: Into::into(v) } |
| 452 | + } |
| 453 | + } |
| 454 | + |
| 455 | + impl A<submod::C> { |
| 456 | + fn test() -> Self { |
| 457 | + Self::new::<submod::B>(submod::B {}) |
| 458 | + } |
| 459 | + } |
| 460 | +} |
0 commit comments