Detect Enum Objects with empty attribute body using `{}`
What it does
The proposed lint feature aims to improve code readability and consistency by discouraging the use of empty attribute objects {} succeeding structures without attributes. In Rust, empty attribute objects are often used to represent variants of an enum that carry no additional information. However, this usage can be misleading and may result in less expressive and clear code.
This lint feature shall check for the presence of empty attribute objects {} immediately following enum variants without associated data. If such a pattern is detected, the lint will emit a warning. This warning encourages developers to reconsider the necessity of the empty attribute object and suggests using the enum variant directly without the unnecessary attribute object.
Advantage
- Prevent Code Blow, simplify code.
Drawbacks
No response
Example
pub enum Decoder {
Absent {},
Symbol(u8)
}
#[derive(Debug, thiserror::Error)]
pub enum CustomError {
#[error("This is a custom error without attributes.")]
CustomError { },
}
type Result<T, E = CustomError> = std::result::Result<T, E>;
impl Decoder {
/// # Errors
///
/// May return an error..
pub fn decode(&mut self, input: u64) -> Result<u8> {
if input > 1 { return Err(CustomError::CustomError { })};
match self {
Decoder::Symbol(symbol) => {
Ok(*symbol)
}
Decoder::Absent{} => {
Ok(b'i')
}
}
}
}
Could be written as:
pub enum Decoder {
Absent,
Symbol(u8)
}
#[derive(Debug, thiserror::Error)]
pub enum CustomError {
#[error("This is a custom error without attributes.")]
CustomError,
}
type Result<T, E = CustomError> = std::result::Result<T, E>;
impl Decoder {
/// # Errors
///
/// May return an error..
pub fn decode(&mut self, input: u64) -> Result<u8> {
if input > 1 { return Err(CustomError::CustomError)};
match self {
Decoder::Symbol(symbol) => {
Ok(*symbol)
}
Decoder::Absent => {
Ok(b'i')
}
}
}
}
In both cases, the nursery and the pedantic mode of clippy do not output any warnings nor errors. To prevent code blow, structures without attributes shall not be allowed to be succeeded by an empty attribute object, {}. The example shall result in a warning, while the second one should not.
### Tasks
@rustbot claim
This is already handled by empty_structs_with_brackets ฅ*•ω•*ฅ♡
EDIT: It seems like it doesn't, it just focuses on structs. I think it'd be better to mix the current open PR with that lint instead of making a whole new lint. (Maybe with a name change to the current lint)
A new lint called brackets_without_fields that does the work of both empty_structs_with_brackets and the lint added in the PR would do the trick. However, I can't find the guidelines in the Clippy book on how to proceed while moving a lint. Can someone please point me in the right direction?
A merged lint would mean deprecating the previous one.
I think a better way to handle this is to have both lints in the same file with a sufficiently descriptive name, and issuing the right one.
This pattern already exists in a few places, so it's established.
Changing
pub enum Decoder {
Absent {},
Symbol(u8)
}
into
pub enum Decoder {
Absent,
Symbol(u8)
}
is a breaking change.
Can we determine if the user intends to do this? Also, we're not enabling this lint by default, correct?
@Xuanwo
Also, we're not enabling this lint by default, correct?
The lint would be placed in the "Restriction" Group which seems to have lints that are allow by default.
I have opened a new PR incorporating the changes suggested by @PartiallyTyped.