Skip to content

Commit a3ed806

Browse files
committed
changes after review
1 parent 5b8fc45 commit a3ed806

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

clippy_lints/src/unit_like_struct_brackets.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ declare_clippy_lint! {
2121
/// ```rust
2222
/// struct Cookie;
2323
/// ```
24-
#[clippy::version = "1.61.0"]
24+
#[clippy::version = "1.62.0"]
2525
pub UNIT_LIKE_STRUCT_BRACKETS,
2626
style,
2727
"finds struct declarations with empty brackets"
@@ -32,7 +32,9 @@ impl EarlyLintPass for UnitLikeStructBrackets {
3232
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
3333
let span_after_ident = item.span.with_lo(item.ident.span.hi());
3434

35-
if let ItemKind::Struct(var_data, _) = &item.kind && has_no_fields(cx, var_data, span_after_ident) {
35+
if let ItemKind::Struct(var_data, _) = &item.kind
36+
&& !is_unit_like_struct(var_data)
37+
&& has_no_fields(cx, var_data, span_after_ident) {
3638
span_lint_and_then(
3739
cx,
3840
UNIT_LIKE_STRUCT_BRACKETS,
@@ -50,23 +52,20 @@ impl EarlyLintPass for UnitLikeStructBrackets {
5052
}
5153
}
5254

53-
fn has_fields_in_hir(var_data: &VariantData) -> bool {
54-
match var_data {
55-
VariantData::Struct(defs, _) | VariantData::Tuple(defs, _) => !defs.is_empty(),
56-
VariantData::Unit(_) => true,
57-
}
58-
}
59-
6055
fn has_no_ident_token(braces_span_str: &str) -> bool {
6156
!rustc_lexer::tokenize(braces_span_str).any(|t| t.kind == TokenKind::Ident)
6257
}
6358

59+
fn is_unit_like_struct(var_data: &VariantData) -> bool {
60+
matches!(var_data, VariantData::Unit(_))
61+
}
62+
6463
fn has_no_fields(cx: &EarlyContext<'_>, var_data: &VariantData, braces_span: Span) -> bool {
65-
if has_fields_in_hir(var_data) {
64+
if !var_data.fields().is_empty() {
6665
return false;
6766
}
6867

69-
// there might still be field declarations hidden from HIR
68+
// there might still be field declarations hidden from the AST
7069
// (conditionaly compiled code using #[cfg(..)])
7170

7271
let Some(braces_span_str) = snippet_opt(cx, braces_span) else {

tests/ui/unit_like_struct_brackets.fixed

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,7 @@ struct MyStruct {
1919
field: u8,
2020
}
2121
struct MyTupleStruct(usize, String); // should not trigger lint
22+
struct MySingleTupleStruct(usize); // should not trigger lint
23+
struct MyUnitLikeStruct; // should not trigger lint
2224

2325
fn main() {}

tests/ui/unit_like_struct_brackets.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,7 @@ struct MyStruct {
1919
field: u8,
2020
}
2121
struct MyTupleStruct(usize, String); // should not trigger lint
22+
struct MySingleTupleStruct(usize); // should not trigger lint
23+
struct MyUnitLikeStruct; // should not trigger lint
2224

2325
fn main() {}

0 commit comments

Comments
 (0)