Skip to content

Commit

Permalink
Add ui tests for unconditional_recursion lint
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Dec 10, 2023
1 parent 5e13a86 commit 9a1a431
Show file tree
Hide file tree
Showing 2 changed files with 430 additions and 0 deletions.
128 changes: 128 additions & 0 deletions tests/ui/unconditional_recursion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
//@no-rustfix

#![warn(clippy::unconditional_recursion)]

enum Foo {
A,
B,
}

impl PartialEq for Foo {
fn ne(&self, other: &Self) -> bool {
//~^ ERROR: function cannot return without recursing
self != other
}
fn eq(&self, other: &Self) -> bool {
//~^ ERROR: function cannot return without recursing
self == other
}
}

enum Foo2 {
A,
B,
}

impl PartialEq for Foo2 {
fn ne(&self, other: &Self) -> bool {
self != &Foo2::B // no error here
}
fn eq(&self, other: &Self) -> bool {
self == &Foo2::B // no error here
}
}

enum Foo3 {
A,
B,
}

impl PartialEq for Foo3 {
fn ne(&self, other: &Self) -> bool {
//~^ ERROR: function cannot return without recursing
self.ne(other)
}
fn eq(&self, other: &Self) -> bool {
//~^ ERROR: function cannot return without recursing
self.eq(other)
}
}

enum Foo4 {
A,
B,
}

impl PartialEq for Foo4 {
fn ne(&self, other: &Self) -> bool {
self.eq(other) // no error
}
fn eq(&self, other: &Self) -> bool {
self.ne(other) // no error
}
}

enum Foo5 {
A,
B,
}

impl Foo5 {
fn a(&self) -> bool {
true
}
}

impl PartialEq for Foo5 {
fn ne(&self, other: &Self) -> bool {
self.a() // no error
}
fn eq(&self, other: &Self) -> bool {
self.a() // no error
}
}

struct S;

// Check the order doesn't matter.
impl PartialEq for S {
fn ne(&self, other: &Self) -> bool {
//~^ ERROR: function cannot return without recursing
other != self
}
fn eq(&self, other: &Self) -> bool {
//~^ ERROR: function cannot return without recursing
other == self
}
}

struct S2;

// Check that if the same element is compared, it's also triggering the lint.
impl PartialEq for S2 {
fn ne(&self, other: &Self) -> bool {
//~^ ERROR: function cannot return without recursing
other != other
}
fn eq(&self, other: &Self) -> bool {
//~^ ERROR: function cannot return without recursing
other == other
}
}

struct S3;

impl PartialEq for S3 {
fn ne(&self, _other: &Self) -> bool {
//~^ ERROR: function cannot return without recursing
self != self
}
fn eq(&self, _other: &Self) -> bool {
//~^ ERROR: function cannot return without recursing
self == self
}
}

fn main() {
// test code goes here
}
Loading

0 comments on commit 9a1a431

Please sign in to comment.