Do not lint reachable enums and enum variants used as functions in the same crate

This commit is contained in:
Aneesh K 2025-04-17 21:29:11 +05:30
parent 26f43ff346
commit fa9254feaf
No known key found for this signature in database
GPG key ID: F6C3344B1C2E8A29
5 changed files with 389 additions and 72 deletions

View file

@ -6,8 +6,7 @@ pub enum PublicTestEnum {
NonEmptyParentheses(i32, i32), // No error
EmptyBraces,
//~^ empty_enum_variants_with_brackets
EmptyParentheses,
//~^ empty_enum_variants_with_brackets
EmptyParentheses(), // No error as enum is pub
}
enum TestEnum {
@ -20,6 +19,67 @@ enum TestEnum {
AnotherEnum, // No error
}
mod issue12551 {
enum EvenOdd {
// Used as functions -> no error
Even(),
Odd(),
// Not used as a function
Unknown,
//~^ empty_enum_variants_with_brackets
}
fn even_odd(x: i32) -> EvenOdd {
(x % 2 == 0).then(EvenOdd::Even).unwrap_or_else(EvenOdd::Odd)
}
fn natural_number(x: i32) -> NaturalOrNot {
(x > 0)
.then(NaturalOrNot::Natural)
.unwrap_or_else(NaturalOrNot::NotNatural)
}
enum NaturalOrNot {
// Used as functions -> no error
Natural(),
NotNatural(),
// Not used as a function
Unknown,
//~^ empty_enum_variants_with_brackets
}
enum RedundantParenthesesFunctionCall {
// Used as a function call but with redundant parentheses
Parentheses,
//~^ empty_enum_variants_with_brackets
// Not used as a function
NoParentheses,
}
#[allow(clippy::no_effect)]
fn redundant_parentheses_function_call() {
// The parentheses in the below line are redundant.
RedundantParenthesesFunctionCall::Parentheses;
RedundantParenthesesFunctionCall::NoParentheses;
}
// Same test as above but with usage of the enum occurring before the definition.
#[allow(clippy::no_effect)]
fn redundant_parentheses_function_call_2() {
// The parentheses in the below line are redundant.
RedundantParenthesesFunctionCall2::Parentheses;
RedundantParenthesesFunctionCall2::NoParentheses;
}
enum RedundantParenthesesFunctionCall2 {
// Used as a function call but with redundant parentheses
Parentheses,
//~^ empty_enum_variants_with_brackets
// Not used as a function
NoParentheses,
}
}
enum TestEnumWithFeatures {
NonEmptyBraces {
#[cfg(feature = "thisisneverenabled")]
@ -28,4 +88,18 @@ enum TestEnumWithFeatures {
NonEmptyParentheses(#[cfg(feature = "thisisneverenabled")] i32), // No error
}
#[derive(Clone)]
enum Foo {
Variant1(i32),
Variant2,
Variant3, //~ ERROR: enum variant has empty brackets
}
#[derive(Clone)]
pub enum PubFoo {
Variant1(i32),
Variant2,
Variant3(),
}
fn main() {}