Skip to content

Commit 528ada9

Browse files
committed
add unit_like_struct_brackets
1 parent c0a5693 commit 528ada9

9 files changed

+101
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3636,6 +3636,7 @@ Released 2018-09-13
36363636
[`unit_arg`]: https://rust-lang.github.io/rust-clippy/master/index.html#unit_arg
36373637
[`unit_cmp`]: https://rust-lang.github.io/rust-clippy/master/index.html#unit_cmp
36383638
[`unit_hash`]: https://rust-lang.github.io/rust-clippy/master/index.html#unit_hash
3639+
[`unit_like_struct_brackets`]: https://rust-lang.github.io/rust-clippy/master/index.html#unit_like_struct_brackets
36393640
[`unit_return_expecting_ord`]: https://rust-lang.github.io/rust-clippy/master/index.html#unit_return_expecting_ord
36403641
[`unnecessary_cast`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast
36413642
[`unnecessary_filter_map`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_filter_map

clippy_lints/src/lib.register_all.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
301301
LintId::of(unicode::INVISIBLE_CHARACTERS),
302302
LintId::of(uninit_vec::UNINIT_VEC),
303303
LintId::of(unit_hash::UNIT_HASH),
304+
LintId::of(unit_like_struct_brackets::UNIT_LIKE_STRUCT_BRACKETS),
304305
LintId::of(unit_return_expecting_ord::UNIT_RETURN_EXPECTING_ORD),
305306
LintId::of(unit_types::UNIT_ARG),
306307
LintId::of(unit_types::UNIT_CMP),

clippy_lints/src/lib.register_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ store.register_lints(&[
512512
unicode::UNICODE_NOT_NFC,
513513
uninit_vec::UNINIT_VEC,
514514
unit_hash::UNIT_HASH,
515+
unit_like_struct_brackets::UNIT_LIKE_STRUCT_BRACKETS,
515516
unit_return_expecting_ord::UNIT_RETURN_EXPECTING_ORD,
516517
unit_types::LET_UNIT_VALUE,
517518
unit_types::UNIT_ARG,

clippy_lints/src/lib.register_style.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ store.register_group(true, "clippy::style", Some("clippy_style"), vec![
105105
LintId::of(single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS),
106106
LintId::of(tabs_in_doc_comments::TABS_IN_DOC_COMMENTS),
107107
LintId::of(to_digit_is_some::TO_DIGIT_IS_SOME),
108+
LintId::of(unit_like_struct_brackets::UNIT_LIKE_STRUCT_BRACKETS),
108109
LintId::of(unsafe_removed_from_name::UNSAFE_REMOVED_FROM_NAME),
109110
LintId::of(unused_unit::UNUSED_UNIT),
110111
LintId::of(upper_case_acronyms::UPPER_CASE_ACRONYMS),

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ mod undropped_manually_drops;
380380
mod unicode;
381381
mod uninit_vec;
382382
mod unit_hash;
383+
mod unit_like_struct_brackets;
383384
mod unit_return_expecting_ord;
384385
mod unit_types;
385386
mod unnamed_address;
@@ -869,6 +870,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
869870
})
870871
});
871872
store.register_early_pass(|| Box::new(crate_in_macro_def::CrateInMacroDef));
873+
store.register_early_pass(|| Box::new(unit_like_struct_brackets::UnitLikeStructBrackets));
872874
// add lints here, do not remove this comment, it's used in `new_lint`
873875
}
874876

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use rustc_ast::ast::*;
3+
use rustc_errors::Applicability;
4+
use rustc_lint::{EarlyContext, EarlyLintPass};
5+
use rustc_session::{declare_lint_pass, declare_tool_lint};
6+
7+
declare_clippy_lint! {
8+
/// ### What it does
9+
/// Finds structs without fields ("unit-like structs") that are declared with brackets.
10+
///
11+
/// ### Why is this bad?
12+
/// Empty brackets after a struct declaration can be omitted.
13+
///
14+
/// ### Example
15+
/// ```rust
16+
/// struct Cookie {}
17+
/// ```
18+
/// Use instead:
19+
/// ```rust
20+
/// struct Cookie;
21+
/// ```
22+
#[clippy::version = "1.61.0"]
23+
pub UNIT_LIKE_STRUCT_BRACKETS,
24+
style,
25+
"finds struct declarations with empty brackets"
26+
}
27+
declare_lint_pass!(UnitLikeStructBrackets => [UNIT_LIKE_STRUCT_BRACKETS]);
28+
29+
impl EarlyLintPass for UnitLikeStructBrackets {
30+
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
31+
if let ItemKind::Struct(var_data, _) = &item.kind && has_no_fields(var_data) {
32+
let span_after_ident = item.span.with_lo(item.ident.span.hi());
33+
34+
span_lint_and_sugg(
35+
cx,
36+
UNIT_LIKE_STRUCT_BRACKETS,
37+
span_after_ident,
38+
"found empty brackets on struct declaration",
39+
"remove the brackets",
40+
";".to_string(),
41+
Applicability::MachineApplicable
42+
);
43+
}
44+
}
45+
}
46+
47+
fn has_no_fields(var_data: &VariantData) -> bool {
48+
match var_data {
49+
VariantData::Struct(defs, _) => defs.is_empty(),
50+
VariantData::Tuple(defs, _) => defs.is_empty(),
51+
VariantData::Unit(_) => false,
52+
}
53+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// run-rustfix
2+
#![warn(clippy::unit_like_struct_brackets)]
3+
#![allow(dead_code)]
4+
5+
pub struct MyEmptyStruct; // should trigger lint
6+
struct MyEmptyTupleStruct; // should trigger lint
7+
8+
struct MyStruct { // should not trigger lint
9+
field: u8,
10+
}
11+
struct MyTupleStruct(usize, String); // should not trigger lint
12+
13+
fn main() {}

tests/ui/unit_like_struct_brackets.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// run-rustfix
2+
#![warn(clippy::unit_like_struct_brackets)]
3+
#![allow(dead_code)]
4+
5+
pub struct MyEmptyStruct {} // should trigger lint
6+
struct MyEmptyTupleStruct(); // should trigger lint
7+
8+
struct MyStruct { // should not trigger lint
9+
field: u8,
10+
}
11+
struct MyTupleStruct(usize, String); // should not trigger lint
12+
13+
fn main() {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: found empty brackets on struct declaration
2+
--> $DIR/unit_like_struct_brackets.rs:5:25
3+
|
4+
LL | pub struct MyEmptyStruct {} // should trigger lint
5+
| ^^^ help: remove the brackets: `;`
6+
|
7+
= note: `-D clippy::unit-like-struct-brackets` implied by `-D warnings`
8+
9+
error: found empty brackets on struct declaration
10+
--> $DIR/unit_like_struct_brackets.rs:6:26
11+
|
12+
LL | struct MyEmptyTupleStruct(); // should trigger lint
13+
| ^^^ help: remove the brackets: `;`
14+
15+
error: aborting due to 2 previous errors
16+

0 commit comments

Comments
 (0)