/src/tools/clippy/tests/ui/return_self_not_must_use.rs

https://gitlab.com/rust-lang/rust · Rust · 58 lines · 44 code · 6 blank · 8 comment · 1 complexity · 6d63adb29e382abb2d3ae472a6dadac8 MD5 · raw file

  1. #![crate_type = "lib"]
  2. #![warn(clippy::return_self_not_must_use)]
  3. #[derive(Clone)]
  4. pub struct Bar;
  5. pub trait Whatever {
  6. fn what(&self) -> Self;
  7. // There should be no warning here! (returns a reference)
  8. fn what2(&self) -> &Self;
  9. }
  10. impl Bar {
  11. // There should be no warning here! (note taking a self argument)
  12. pub fn not_new() -> Self {
  13. Self
  14. }
  15. pub fn foo(&self) -> Self {
  16. Self
  17. }
  18. pub fn bar(self) -> Self {
  19. self
  20. }
  21. // There should be no warning here! (private method)
  22. fn foo2(&self) -> Self {
  23. Self
  24. }
  25. // There should be no warning here! (returns a reference)
  26. pub fn foo3(&self) -> &Self {
  27. self
  28. }
  29. // There should be no warning here! (already a `must_use` attribute)
  30. #[must_use]
  31. pub fn foo4(&self) -> Self {
  32. Self
  33. }
  34. }
  35. impl Whatever for Bar {
  36. // There should be no warning here! (comes from the trait)
  37. fn what(&self) -> Self {
  38. self.foo2()
  39. }
  40. // There should be no warning here! (comes from the trait)
  41. fn what2(&self) -> &Self {
  42. self
  43. }
  44. }
  45. #[must_use]
  46. pub struct Foo;
  47. impl Foo {
  48. // There should be no warning here! (`Foo` already implements `#[must_use]`)
  49. fn foo(&self) -> Self {
  50. Self
  51. }
  52. }