From 528ada958bf51b6a48430f357f6ae77a9e81c650 Mon Sep 17 00:00:00 2001 From: Max Baumann Date: Sun, 27 Mar 2022 14:16:08 +0200 Subject: [PATCH 01/12] add unit_like_struct_brackets --- CHANGELOG.md | 1 + clippy_lints/src/lib.register_all.rs | 1 + clippy_lints/src/lib.register_lints.rs | 1 + clippy_lints/src/lib.register_style.rs | 1 + clippy_lints/src/lib.rs | 2 + clippy_lints/src/unit_like_struct_brackets.rs | 53 +++++++++++++++++++ tests/ui/unit_like_struct_brackets.fixed | 13 +++++ tests/ui/unit_like_struct_brackets.rs | 13 +++++ tests/ui/unit_like_struct_brackets.stderr | 16 ++++++ 9 files changed, 101 insertions(+) create mode 100644 clippy_lints/src/unit_like_struct_brackets.rs create mode 100644 tests/ui/unit_like_struct_brackets.fixed create mode 100644 tests/ui/unit_like_struct_brackets.rs create mode 100644 tests/ui/unit_like_struct_brackets.stderr diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d1e081e839aa..49a44d988139b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3636,6 +3636,7 @@ Released 2018-09-13 [`unit_arg`]: https://rust-lang.github.io/rust-clippy/master/index.html#unit_arg [`unit_cmp`]: https://rust-lang.github.io/rust-clippy/master/index.html#unit_cmp [`unit_hash`]: https://rust-lang.github.io/rust-clippy/master/index.html#unit_hash +[`unit_like_struct_brackets`]: https://rust-lang.github.io/rust-clippy/master/index.html#unit_like_struct_brackets [`unit_return_expecting_ord`]: https://rust-lang.github.io/rust-clippy/master/index.html#unit_return_expecting_ord [`unnecessary_cast`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast [`unnecessary_filter_map`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_filter_map diff --git a/clippy_lints/src/lib.register_all.rs b/clippy_lints/src/lib.register_all.rs index 1fb3ca1fd9b2b..f488aab46eccd 100644 --- a/clippy_lints/src/lib.register_all.rs +++ b/clippy_lints/src/lib.register_all.rs @@ -301,6 +301,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![ LintId::of(unicode::INVISIBLE_CHARACTERS), LintId::of(uninit_vec::UNINIT_VEC), LintId::of(unit_hash::UNIT_HASH), + LintId::of(unit_like_struct_brackets::UNIT_LIKE_STRUCT_BRACKETS), LintId::of(unit_return_expecting_ord::UNIT_RETURN_EXPECTING_ORD), LintId::of(unit_types::UNIT_ARG), LintId::of(unit_types::UNIT_CMP), diff --git a/clippy_lints/src/lib.register_lints.rs b/clippy_lints/src/lib.register_lints.rs index e3161795139df..a1e6ca76a16b5 100644 --- a/clippy_lints/src/lib.register_lints.rs +++ b/clippy_lints/src/lib.register_lints.rs @@ -512,6 +512,7 @@ store.register_lints(&[ unicode::UNICODE_NOT_NFC, uninit_vec::UNINIT_VEC, unit_hash::UNIT_HASH, + unit_like_struct_brackets::UNIT_LIKE_STRUCT_BRACKETS, unit_return_expecting_ord::UNIT_RETURN_EXPECTING_ORD, unit_types::LET_UNIT_VALUE, unit_types::UNIT_ARG, diff --git a/clippy_lints/src/lib.register_style.rs b/clippy_lints/src/lib.register_style.rs index dcf399cf9562f..89657cfb7842a 100644 --- a/clippy_lints/src/lib.register_style.rs +++ b/clippy_lints/src/lib.register_style.rs @@ -105,6 +105,7 @@ store.register_group(true, "clippy::style", Some("clippy_style"), vec![ LintId::of(single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS), LintId::of(tabs_in_doc_comments::TABS_IN_DOC_COMMENTS), LintId::of(to_digit_is_some::TO_DIGIT_IS_SOME), + LintId::of(unit_like_struct_brackets::UNIT_LIKE_STRUCT_BRACKETS), LintId::of(unsafe_removed_from_name::UNSAFE_REMOVED_FROM_NAME), LintId::of(unused_unit::UNUSED_UNIT), LintId::of(upper_case_acronyms::UPPER_CASE_ACRONYMS), diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index c8b57956b1b62..af8d9b3655c56 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -380,6 +380,7 @@ mod undropped_manually_drops; mod unicode; mod uninit_vec; mod unit_hash; +mod unit_like_struct_brackets; mod unit_return_expecting_ord; mod unit_types; mod unnamed_address; @@ -869,6 +870,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: }) }); store.register_early_pass(|| Box::new(crate_in_macro_def::CrateInMacroDef)); + store.register_early_pass(|| Box::new(unit_like_struct_brackets::UnitLikeStructBrackets)); // add lints here, do not remove this comment, it's used in `new_lint` } diff --git a/clippy_lints/src/unit_like_struct_brackets.rs b/clippy_lints/src/unit_like_struct_brackets.rs new file mode 100644 index 0000000000000..0eeb765be8518 --- /dev/null +++ b/clippy_lints/src/unit_like_struct_brackets.rs @@ -0,0 +1,53 @@ +use clippy_utils::diagnostics::span_lint_and_sugg; +use rustc_ast::ast::*; +use rustc_errors::Applicability; +use rustc_lint::{EarlyContext, EarlyLintPass}; +use rustc_session::{declare_lint_pass, declare_tool_lint}; + +declare_clippy_lint! { + /// ### What it does + /// Finds structs without fields ("unit-like structs") that are declared with brackets. + /// + /// ### Why is this bad? + /// Empty brackets after a struct declaration can be omitted. + /// + /// ### Example + /// ```rust + /// struct Cookie {} + /// ``` + /// Use instead: + /// ```rust + /// struct Cookie; + /// ``` + #[clippy::version = "1.61.0"] + pub UNIT_LIKE_STRUCT_BRACKETS, + style, + "finds struct declarations with empty brackets" +} +declare_lint_pass!(UnitLikeStructBrackets => [UNIT_LIKE_STRUCT_BRACKETS]); + +impl EarlyLintPass for UnitLikeStructBrackets { + fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { + if let ItemKind::Struct(var_data, _) = &item.kind && has_no_fields(var_data) { + let span_after_ident = item.span.with_lo(item.ident.span.hi()); + + span_lint_and_sugg( + cx, + UNIT_LIKE_STRUCT_BRACKETS, + span_after_ident, + "found empty brackets on struct declaration", + "remove the brackets", + ";".to_string(), + Applicability::MachineApplicable + ); + } + } +} + +fn has_no_fields(var_data: &VariantData) -> bool { + match var_data { + VariantData::Struct(defs, _) => defs.is_empty(), + VariantData::Tuple(defs, _) => defs.is_empty(), + VariantData::Unit(_) => false, + } +} diff --git a/tests/ui/unit_like_struct_brackets.fixed b/tests/ui/unit_like_struct_brackets.fixed new file mode 100644 index 0000000000000..a69c310e97361 --- /dev/null +++ b/tests/ui/unit_like_struct_brackets.fixed @@ -0,0 +1,13 @@ +// run-rustfix +#![warn(clippy::unit_like_struct_brackets)] +#![allow(dead_code)] + +pub struct MyEmptyStruct; // should trigger lint +struct MyEmptyTupleStruct; // should trigger lint + +struct MyStruct { // should not trigger lint + field: u8, +} +struct MyTupleStruct(usize, String); // should not trigger lint + +fn main() {} diff --git a/tests/ui/unit_like_struct_brackets.rs b/tests/ui/unit_like_struct_brackets.rs new file mode 100644 index 0000000000000..8697a24f1d77a --- /dev/null +++ b/tests/ui/unit_like_struct_brackets.rs @@ -0,0 +1,13 @@ +// run-rustfix +#![warn(clippy::unit_like_struct_brackets)] +#![allow(dead_code)] + +pub struct MyEmptyStruct {} // should trigger lint +struct MyEmptyTupleStruct(); // should trigger lint + +struct MyStruct { // should not trigger lint + field: u8, +} +struct MyTupleStruct(usize, String); // should not trigger lint + +fn main() {} diff --git a/tests/ui/unit_like_struct_brackets.stderr b/tests/ui/unit_like_struct_brackets.stderr new file mode 100644 index 0000000000000..146ede19c0719 --- /dev/null +++ b/tests/ui/unit_like_struct_brackets.stderr @@ -0,0 +1,16 @@ +error: found empty brackets on struct declaration + --> $DIR/unit_like_struct_brackets.rs:5:25 + | +LL | pub struct MyEmptyStruct {} // should trigger lint + | ^^^ help: remove the brackets: `;` + | + = note: `-D clippy::unit-like-struct-brackets` implied by `-D warnings` + +error: found empty brackets on struct declaration + --> $DIR/unit_like_struct_brackets.rs:6:26 + | +LL | struct MyEmptyTupleStruct(); // should trigger lint + | ^^^ help: remove the brackets: `;` + +error: aborting due to 2 previous errors + From 9be3945be740b12c333a68db6fa33b8878acdeb9 Mon Sep 17 00:00:00 2001 From: Max Baumann Date: Sun, 27 Mar 2022 14:41:09 +0200 Subject: [PATCH 02/12] fix existing clippy tests --- clippy_lints/src/use_self.rs | 4 ++-- tests/ui-toml/struct_excessive_bools/test.rs | 2 +- ...se_sensitive_file_extension_comparisons.rs | 2 +- tests/ui/crashes/ice-2774.rs | 2 +- tests/ui/crashes/ice-6179.rs | 2 +- tests/ui/crashes/ice-6792.rs | 2 +- .../crashes/needless_lifetimes_impl_trait.rs | 2 +- tests/ui/crashes/regressions.rs | 2 +- tests/ui/default_numeric_fallback_f64.fixed | 2 +- tests/ui/default_numeric_fallback_f64.rs | 2 +- tests/ui/default_numeric_fallback_i32.fixed | 2 +- tests/ui/default_numeric_fallback_i32.rs | 2 +- tests/ui/drop_forget_copy.rs | 2 +- tests/ui/fn_params_excessive_bools.rs | 2 +- tests/ui/implicit_clone.rs | 4 ++-- tests/ui/iter_nth_zero.fixed | 2 +- tests/ui/iter_nth_zero.rs | 2 +- tests/ui/large_types_passed_by_value.rs | 2 +- tests/ui/let_and_return.rs | 2 +- tests/ui/let_underscore_must_use.rs | 2 +- tests/ui/manual_async_fn.fixed | 2 +- tests/ui/manual_async_fn.rs | 2 +- tests/ui/manual_unwrap_or.fixed | 2 +- tests/ui/manual_unwrap_or.rs | 2 +- tests/ui/map_unit_fn.rs | 2 +- tests/ui/min_rust_version_attr.rs | 2 +- tests/ui/missing_inline.rs | 6 ++--- tests/ui/module_name_repetitions.rs | 2 +- .../needless_arbitrary_self_type_unfixable.rs | 4 ++-- tests/ui/needless_lifetimes.rs | 2 +- tests/ui/no_effect.rs | 4 ++-- tests/ui/or_then_unwrap.fixed | 4 ++-- tests/ui/or_then_unwrap.rs | 4 ++-- tests/ui/ptr_arg.rs | 2 +- tests/ui/recursive_format_impl.rs | 24 +++++++++---------- tests/ui/redundant_allocation.rs | 2 +- tests/ui/redundant_allocation_fixable.fixed | 2 +- tests/ui/redundant_static_lifetimes.fixed | 2 +- tests/ui/redundant_static_lifetimes.rs | 2 +- tests/ui/same_item_push.rs | 2 +- tests/ui/trait_duplication_in_bounds.rs | 4 ++-- tests/ui/unit_like_struct_brackets.fixed | 9 +++---- tests/ui/unit_like_struct_brackets.rs | 9 +++---- tests/ui/unsafe_derive_deserialize.rs | 14 +++++------ tests/ui/unsafe_removed_from_name.rs | 4 ++-- tests/ui/unused_self.rs | 10 ++++---- tests/ui/use_self.fixed | 22 ++++++++--------- tests/ui/use_self.rs | 22 ++++++++--------- tests/ui/useless_attribute.fixed | 2 +- tests/ui/useless_attribute.rs | 2 +- 50 files changed, 109 insertions(+), 107 deletions(-) diff --git a/clippy_lints/src/use_self.rs b/clippy_lints/src/use_self.rs index 09d671e11184d..f8e1021af0ea1 100644 --- a/clippy_lints/src/use_self.rs +++ b/clippy_lints/src/use_self.rs @@ -34,7 +34,7 @@ declare_clippy_lint! { /// /// ### Example /// ```rust - /// struct Foo {} + /// struct Foo; /// impl Foo { /// fn new() -> Foo { /// Foo {} @@ -43,7 +43,7 @@ declare_clippy_lint! { /// ``` /// could be /// ```rust - /// struct Foo {} + /// struct Foo; /// impl Foo { /// fn new() -> Self { /// Self {} diff --git a/tests/ui-toml/struct_excessive_bools/test.rs b/tests/ui-toml/struct_excessive_bools/test.rs index 242984680e163..32dd80246fab4 100644 --- a/tests/ui-toml/struct_excessive_bools/test.rs +++ b/tests/ui-toml/struct_excessive_bools/test.rs @@ -4,6 +4,6 @@ struct S { a: bool, } -struct Foo {} +struct Foo; fn main() {} diff --git a/tests/ui/case_sensitive_file_extension_comparisons.rs b/tests/ui/case_sensitive_file_extension_comparisons.rs index 68719c2bc6d05..0d65071af15ed 100644 --- a/tests/ui/case_sensitive_file_extension_comparisons.rs +++ b/tests/ui/case_sensitive_file_extension_comparisons.rs @@ -2,7 +2,7 @@ use std::string::String; -struct TestStruct {} +struct TestStruct; impl TestStruct { fn ends_with(self, arg: &str) {} diff --git a/tests/ui/crashes/ice-2774.rs b/tests/ui/crashes/ice-2774.rs index d44b0fae82001..88cfa1f923c0b 100644 --- a/tests/ui/crashes/ice-2774.rs +++ b/tests/ui/crashes/ice-2774.rs @@ -8,7 +8,7 @@ pub struct Bar { } #[derive(Eq, PartialEq, Debug, Hash)] -pub struct Foo {} +pub struct Foo; #[allow(clippy::implicit_hasher)] // This should not cause a "cannot relate bound region" ICE. diff --git a/tests/ui/crashes/ice-6179.rs b/tests/ui/crashes/ice-6179.rs index 8d9a1af8ff118..4fe92d356c44d 100644 --- a/tests/ui/crashes/ice-6179.rs +++ b/tests/ui/crashes/ice-6179.rs @@ -4,7 +4,7 @@ #![warn(clippy::use_self)] #![allow(dead_code)] -struct Foo {} +struct Foo; impl Foo { fn new() -> Self { diff --git a/tests/ui/crashes/ice-6792.rs b/tests/ui/crashes/ice-6792.rs index 0e2ab1a39b82f..9cbafc716b500 100644 --- a/tests/ui/crashes/ice-6792.rs +++ b/tests/ui/crashes/ice-6792.rs @@ -7,7 +7,7 @@ trait Trait { fn broken() -> Self::Ty; } -struct Foo {} +struct Foo; impl Trait for Foo { type Ty = Foo; diff --git a/tests/ui/crashes/needless_lifetimes_impl_trait.rs b/tests/ui/crashes/needless_lifetimes_impl_trait.rs index 676564b2445d5..376ff97ba6036 100644 --- a/tests/ui/crashes/needless_lifetimes_impl_trait.rs +++ b/tests/ui/crashes/needless_lifetimes_impl_trait.rs @@ -3,7 +3,7 @@ trait Foo {} -struct Bar {} +struct Bar; struct Baz<'a> { bar: &'a Bar, diff --git a/tests/ui/crashes/regressions.rs b/tests/ui/crashes/regressions.rs index a41bcb33b4460..6f9d98bbfe7f3 100644 --- a/tests/ui/crashes/regressions.rs +++ b/tests/ui/crashes/regressions.rs @@ -6,6 +6,6 @@ pub fn foo(bar: *const u8) { // Regression test for https://github.com/rust-lang/rust-clippy/issues/4917 /// , _: Vec) {} fn t(_: S, _: S, _: Box, _: Vec, _: bool, _: bool, _: bool, _: bool) {} -struct S {} +struct S; trait Trait { fn f(_: bool, _: bool, _: bool, _: bool); fn g(_: bool, _: bool, _: bool, _: Vec); diff --git a/tests/ui/implicit_clone.rs b/tests/ui/implicit_clone.rs index 639fecb8927bd..2549c9f32f904 100644 --- a/tests/ui/implicit_clone.rs +++ b/tests/ui/implicit_clone.rs @@ -30,7 +30,7 @@ where } #[derive(Copy, Clone)] -struct Kitten {} +struct Kitten; impl Kitten { // badly named method fn to_vec(self) -> Kitten { @@ -44,7 +44,7 @@ impl Borrow for Kitten { } } -struct BorrowedKitten {} +struct BorrowedKitten; impl ToOwned for BorrowedKitten { type Owned = Kitten; fn to_owned(&self) -> Kitten { diff --git a/tests/ui/iter_nth_zero.fixed b/tests/ui/iter_nth_zero.fixed index b54147c94d192..f23671c26e4cc 100644 --- a/tests/ui/iter_nth_zero.fixed +++ b/tests/ui/iter_nth_zero.fixed @@ -3,7 +3,7 @@ #![warn(clippy::iter_nth_zero)] use std::collections::HashSet; -struct Foo {} +struct Foo; impl Foo { fn nth(&self, index: usize) -> usize { diff --git a/tests/ui/iter_nth_zero.rs b/tests/ui/iter_nth_zero.rs index b92c7d18adb4f..7c968d4984571 100644 --- a/tests/ui/iter_nth_zero.rs +++ b/tests/ui/iter_nth_zero.rs @@ -3,7 +3,7 @@ #![warn(clippy::iter_nth_zero)] use std::collections::HashSet; -struct Foo {} +struct Foo; impl Foo { fn nth(&self, index: usize) -> usize { diff --git a/tests/ui/large_types_passed_by_value.rs b/tests/ui/large_types_passed_by_value.rs index e4a2e9df4d7ba..7601b5c66fa35 100644 --- a/tests/ui/large_types_passed_by_value.rs +++ b/tests/ui/large_types_passed_by_value.rs @@ -37,7 +37,7 @@ pub trait PubLargeTypeDevourer { fn devoure_array_in_public(&self, array: [u8; 6666]); } -struct S {} +struct S; impl LargeTypeDevourer for S { fn devoure_array(&self, array: [u8; 6666]) { todo!(); diff --git a/tests/ui/let_and_return.rs b/tests/ui/let_and_return.rs index e3561863c1e1f..bb162adc9adb2 100644 --- a/tests/ui/let_and_return.rs +++ b/tests/ui/let_and_return.rs @@ -88,7 +88,7 @@ mod no_lint_if_stmt_borrows { ret } - struct Bar {} + struct Bar; impl Bar { fn new() -> Self { diff --git a/tests/ui/let_underscore_must_use.rs b/tests/ui/let_underscore_must_use.rs index a842e872a37b1..1edb77c748bfb 100644 --- a/tests/ui/let_underscore_must_use.rs +++ b/tests/ui/let_underscore_must_use.rs @@ -26,7 +26,7 @@ fn h() -> u32 { 0 } -struct S {} +struct S; impl S { #[must_use] diff --git a/tests/ui/manual_async_fn.fixed b/tests/ui/manual_async_fn.fixed index 136cc96be70ca..b7e46a4a8ccc2 100644 --- a/tests/ui/manual_async_fn.fixed +++ b/tests/ui/manual_async_fn.fixed @@ -38,7 +38,7 @@ async fn already_async() -> impl Future { async { 42 } } -struct S {} +struct S; impl S { async fn inh_fut() -> i32 { // NOTE: this code is here just to check that the indentation is correct in the suggested fix diff --git a/tests/ui/manual_async_fn.rs b/tests/ui/manual_async_fn.rs index ddc453ffdb750..b05429da66225 100644 --- a/tests/ui/manual_async_fn.rs +++ b/tests/ui/manual_async_fn.rs @@ -52,7 +52,7 @@ async fn already_async() -> impl Future { async { 42 } } -struct S {} +struct S; impl S { fn inh_fut() -> impl Future { async { diff --git a/tests/ui/manual_unwrap_or.fixed b/tests/ui/manual_unwrap_or.fixed index 05d6c56f2aca0..7d68978216c9c 100644 --- a/tests/ui/manual_unwrap_or.fixed +++ b/tests/ui/manual_unwrap_or.fixed @@ -78,7 +78,7 @@ fn result_unwrap_or() { (Ok(1) as Result).unwrap_or(42); // method call case, suggestion must not surround Result expr `s.method()` with parentheses - struct S {} + struct S; impl S { fn method(self) -> Option { Some(42) diff --git a/tests/ui/manual_unwrap_or.rs b/tests/ui/manual_unwrap_or.rs index 09f62c69b71de..b937fe6f977e5 100644 --- a/tests/ui/manual_unwrap_or.rs +++ b/tests/ui/manual_unwrap_or.rs @@ -102,7 +102,7 @@ fn result_unwrap_or() { }; // method call case, suggestion must not surround Result expr `s.method()` with parentheses - struct S {} + struct S; impl S { fn method(self) -> Option { Some(42) diff --git a/tests/ui/map_unit_fn.rs b/tests/ui/map_unit_fn.rs index 9a74da4e3b8b6..e7f07b50f3ab1 100644 --- a/tests/ui/map_unit_fn.rs +++ b/tests/ui/map_unit_fn.rs @@ -1,5 +1,5 @@ #![allow(unused)] -struct Mappable {} +struct Mappable; impl Mappable { pub fn map(&self) {} diff --git a/tests/ui/min_rust_version_attr.rs b/tests/ui/min_rust_version_attr.rs index c5f221220ece7..72e9bf9eb3689 100644 --- a/tests/ui/min_rust_version_attr.rs +++ b/tests/ui/min_rust_version_attr.rs @@ -99,7 +99,7 @@ pub fn manual_range_contains() { } pub fn use_self() { - struct Foo {} + struct Foo; impl Foo { fn new() -> Foo { diff --git a/tests/ui/missing_inline.rs b/tests/ui/missing_inline.rs index b73b24b8e0a3b..07f8e3888c998 100644 --- a/tests/ui/missing_inline.rs +++ b/tests/ui/missing_inline.rs @@ -7,8 +7,8 @@ type Typedef = String; pub type PubTypedef = String; -struct Foo {} // ok -pub struct PubFoo {} // ok +struct Foo; // ok +pub struct PubFoo; // ok enum FooE {} // ok pub enum PubFooE {} // ok @@ -63,4 +63,4 @@ impl PubFoo { // do not lint this since users cannot control the external code #[derive(Debug)] -pub struct S {} +pub struct S; diff --git a/tests/ui/module_name_repetitions.rs b/tests/ui/module_name_repetitions.rs index f5908cb5701fb..ebaa77cc283e0 100644 --- a/tests/ui/module_name_repetitions.rs +++ b/tests/ui/module_name_repetitions.rs @@ -7,7 +7,7 @@ mod foo { pub fn foo() {} pub fn foo_bar() {} pub fn bar_foo() {} - pub struct FooCake {} + pub struct FooCake; pub enum CakeFoo {} pub struct Foo7Bar; diff --git a/tests/ui/needless_arbitrary_self_type_unfixable.rs b/tests/ui/needless_arbitrary_self_type_unfixable.rs index ad0d694a2174a..02b43cce2bd4c 100644 --- a/tests/ui/needless_arbitrary_self_type_unfixable.rs +++ b/tests/ui/needless_arbitrary_self_type_unfixable.rs @@ -14,7 +14,7 @@ mod issue_6089 { fn test(self: &Self); } - struct S1 {} + struct S1; impl T1 for S1 { fn test(self: &Self) {} @@ -32,7 +32,7 @@ mod issue_6089 { fn call_with_mut_self(&mut self); } - struct S2 {} + struct S2; // The method's signature will be expanded to: // fn call_with_mut_self<'life0>(self: &'life0 mut Self) {} diff --git a/tests/ui/needless_lifetimes.rs b/tests/ui/needless_lifetimes.rs index f3eafe8e9279e..1456204ca8692 100644 --- a/tests/ui/needless_lifetimes.rs +++ b/tests/ui/needless_lifetimes.rs @@ -268,7 +268,7 @@ mod issue4291 { mod issue2944 { trait Foo {} - struct Bar {} + struct Bar; struct Baz<'a> { bar: &'a Bar, } diff --git a/tests/ui/no_effect.rs b/tests/ui/no_effect.rs index 5427c88faf348..0a67fb72044c0 100644 --- a/tests/ui/no_effect.rs +++ b/tests/ui/no_effect.rs @@ -68,7 +68,7 @@ impl FnOnce<(&str,)> for GreetStruct1 { } } -struct GreetStruct2(); +struct GreetStruct2; impl FnOnce<(&str,)> for GreetStruct2 { type Output = (); @@ -78,7 +78,7 @@ impl FnOnce<(&str,)> for GreetStruct2 { } } -struct GreetStruct3 {} +struct GreetStruct3; impl FnOnce<(&str,)> for GreetStruct3 { type Output = (); diff --git a/tests/ui/or_then_unwrap.fixed b/tests/ui/or_then_unwrap.fixed index 27d4b795a5eeb..6e0d5a87f6807 100644 --- a/tests/ui/or_then_unwrap.fixed +++ b/tests/ui/or_then_unwrap.fixed @@ -3,7 +3,7 @@ #![warn(clippy::or_then_unwrap)] #![allow(clippy::map_identity)] -struct SomeStruct {} +struct SomeStruct; impl SomeStruct { fn or(self, _: Option) -> Self { self @@ -11,7 +11,7 @@ impl SomeStruct { fn unwrap(&self) {} } -struct SomeOtherStruct {} +struct SomeOtherStruct; impl SomeOtherStruct { fn or(self) -> Self { self diff --git a/tests/ui/or_then_unwrap.rs b/tests/ui/or_then_unwrap.rs index 0dab5ae2f1c04..e406a71d46d00 100644 --- a/tests/ui/or_then_unwrap.rs +++ b/tests/ui/or_then_unwrap.rs @@ -3,7 +3,7 @@ #![warn(clippy::or_then_unwrap)] #![allow(clippy::map_identity)] -struct SomeStruct {} +struct SomeStruct; impl SomeStruct { fn or(self, _: Option) -> Self { self @@ -11,7 +11,7 @@ impl SomeStruct { fn unwrap(&self) {} } -struct SomeOtherStruct {} +struct SomeOtherStruct; impl SomeOtherStruct { fn or(self) -> Self { self diff --git a/tests/ui/ptr_arg.rs b/tests/ui/ptr_arg.rs index 03dd938a2339e..814bbc7af713b 100644 --- a/tests/ui/ptr_arg.rs +++ b/tests/ui/ptr_arg.rs @@ -112,7 +112,7 @@ mod issue_5644 { ) { } - struct S {} + struct S; impl S { fn allowed( #[allow(clippy::ptr_arg)] _v: &Vec, diff --git a/tests/ui/recursive_format_impl.rs b/tests/ui/recursive_format_impl.rs index 9241bf7ed7402..f72fc77ab9977 100644 --- a/tests/ui/recursive_format_impl.rs +++ b/tests/ui/recursive_format_impl.rs @@ -66,7 +66,7 @@ impl std::fmt::Display for D { // Check for use of self as Display, in Display impl // Triggers on direct use of self -struct G {} +struct G; impl std::fmt::Display for G { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { @@ -75,7 +75,7 @@ impl std::fmt::Display for G { } // Triggers on reference to self -struct H {} +struct H; impl std::fmt::Display for H { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { @@ -90,7 +90,7 @@ impl std::fmt::Debug for H { } // Triggers on multiple reference to self -struct H2 {} +struct H2; impl std::fmt::Display for H2 { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { @@ -99,7 +99,7 @@ impl std::fmt::Display for H2 { } // Doesn't trigger on correct deref -struct I {} +struct I; impl std::ops::Deref for I { type Target = str; @@ -122,7 +122,7 @@ impl std::fmt::Debug for I { } // Doesn't trigger on multiple correct deref -struct I2 {} +struct I2; impl std::ops::Deref for I2 { type Target = str; @@ -139,7 +139,7 @@ impl std::fmt::Display for I2 { } // Doesn't trigger on multiple correct deref -struct I3 {} +struct I3; impl std::ops::Deref for I3 { type Target = str; @@ -156,7 +156,7 @@ impl std::fmt::Display for I3 { } // Does trigger when deref resolves to self -struct J {} +struct J; impl std::ops::Deref for J { type Target = str; @@ -178,7 +178,7 @@ impl std::fmt::Debug for J { } } -struct J2 {} +struct J2; impl std::ops::Deref for J2 { type Target = str; @@ -194,7 +194,7 @@ impl std::fmt::Display for J2 { } } -struct J3 {} +struct J3; impl std::ops::Deref for J3 { type Target = str; @@ -210,7 +210,7 @@ impl std::fmt::Display for J3 { } } -struct J4 {} +struct J4; impl std::ops::Deref for J4 { type Target = str; @@ -227,7 +227,7 @@ impl std::fmt::Display for J4 { } // Doesn't trigger on Debug from Display -struct K {} +struct K; impl std::fmt::Debug for K { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { @@ -242,7 +242,7 @@ impl std::fmt::Display for K { } // Doesn't trigger on Display from Debug -struct K2 {} +struct K2; impl std::fmt::Debug for K2 { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { diff --git a/tests/ui/redundant_allocation.rs b/tests/ui/redundant_allocation.rs index 52fbc91e32555..80f94e5f3cbbb 100644 --- a/tests/ui/redundant_allocation.rs +++ b/tests/ui/redundant_allocation.rs @@ -3,7 +3,7 @@ #![allow(clippy::blacklisted_name, unused_variables, dead_code)] #![allow(unused_imports)] -pub struct MyStruct {} +pub struct MyStruct; pub struct SubT { foo: T, diff --git a/tests/ui/redundant_allocation_fixable.fixed b/tests/ui/redundant_allocation_fixable.fixed index ef089b25f47fd..e7ed84731c02e 100644 --- a/tests/ui/redundant_allocation_fixable.fixed +++ b/tests/ui/redundant_allocation_fixable.fixed @@ -4,7 +4,7 @@ #![allow(clippy::blacklisted_name, unused_variables, dead_code)] #![allow(unused_imports)] -pub struct MyStruct {} +pub struct MyStruct; pub struct SubT { foo: T, diff --git a/tests/ui/redundant_static_lifetimes.fixed b/tests/ui/redundant_static_lifetimes.fixed index 921249606ad27..acc8f1e25b6ed 100644 --- a/tests/ui/redundant_static_lifetimes.fixed +++ b/tests/ui/redundant_static_lifetimes.fixed @@ -3,7 +3,7 @@ #![allow(unused)] #[derive(Debug)] -struct Foo {} +struct Foo; const VAR_ONE: &str = "Test constant #1"; // ERROR Consider removing 'static. diff --git a/tests/ui/redundant_static_lifetimes.rs b/tests/ui/redundant_static_lifetimes.rs index 4d4b249d076ff..f2f0f78659c93 100644 --- a/tests/ui/redundant_static_lifetimes.rs +++ b/tests/ui/redundant_static_lifetimes.rs @@ -3,7 +3,7 @@ #![allow(unused)] #[derive(Debug)] -struct Foo {} +struct Foo; const VAR_ONE: &'static str = "Test constant #1"; // ERROR Consider removing 'static. diff --git a/tests/ui/same_item_push.rs b/tests/ui/same_item_push.rs index 9d420ec672a26..99964f0de075c 100644 --- a/tests/ui/same_item_push.rs +++ b/tests/ui/same_item_push.rs @@ -120,7 +120,7 @@ fn main() { } // Fix #5979 #[derive(Clone)] - struct S {} + struct S; trait T {} impl T for S {} diff --git a/tests/ui/trait_duplication_in_bounds.rs b/tests/ui/trait_duplication_in_bounds.rs index 21de19a260147..f5ca91143af25 100644 --- a/tests/ui/trait_duplication_in_bounds.rs +++ b/tests/ui/trait_duplication_in_bounds.rs @@ -62,7 +62,7 @@ trait BadTrait: Default + Clone { } #[derive(Default, Clone)] -struct Life {} +struct Life; impl T for Life { // this should not warn @@ -85,7 +85,7 @@ trait Iter: Iterator { } } -struct Foo {} +struct Foo; trait FooIter: Iterator { fn bar() diff --git a/tests/ui/unit_like_struct_brackets.fixed b/tests/ui/unit_like_struct_brackets.fixed index a69c310e97361..e6cf4a0c0b465 100644 --- a/tests/ui/unit_like_struct_brackets.fixed +++ b/tests/ui/unit_like_struct_brackets.fixed @@ -2,12 +2,13 @@ #![warn(clippy::unit_like_struct_brackets)] #![allow(dead_code)] -pub struct MyEmptyStruct; // should trigger lint -struct MyEmptyTupleStruct; // should trigger lint +pub struct MyEmptyStruct; // should trigger lint +struct MyEmptyTupleStruct; // should trigger lint -struct MyStruct { // should not trigger lint +struct MyStruct { + // should not trigger lint field: u8, } -struct MyTupleStruct(usize, String); // should not trigger lint +struct MyTupleStruct(usize, String); // should not trigger lint fn main() {} diff --git a/tests/ui/unit_like_struct_brackets.rs b/tests/ui/unit_like_struct_brackets.rs index 8697a24f1d77a..306e4c207d852 100644 --- a/tests/ui/unit_like_struct_brackets.rs +++ b/tests/ui/unit_like_struct_brackets.rs @@ -2,12 +2,13 @@ #![warn(clippy::unit_like_struct_brackets)] #![allow(dead_code)] -pub struct MyEmptyStruct {} // should trigger lint -struct MyEmptyTupleStruct(); // should trigger lint +pub struct MyEmptyStruct {} // should trigger lint +struct MyEmptyTupleStruct(); // should trigger lint -struct MyStruct { // should not trigger lint +struct MyStruct { + // should not trigger lint field: u8, } -struct MyTupleStruct(usize, String); // should not trigger lint +struct MyTupleStruct(usize, String); // should not trigger lint fn main() {} diff --git a/tests/ui/unsafe_derive_deserialize.rs b/tests/ui/unsafe_derive_deserialize.rs index 690d705573d3f..bafca91917aa4 100644 --- a/tests/ui/unsafe_derive_deserialize.rs +++ b/tests/ui/unsafe_derive_deserialize.rs @@ -6,7 +6,7 @@ extern crate serde; use serde::Deserialize; #[derive(Deserialize)] -pub struct A {} +pub struct A; impl A { pub unsafe fn new(_a: i32, _b: i32) -> Self { Self {} @@ -14,13 +14,13 @@ impl A { } #[derive(Deserialize)] -pub struct B {} +pub struct B; impl B { pub unsafe fn unsafe_method(&self) {} } #[derive(Deserialize)] -pub struct C {} +pub struct C; impl C { pub fn unsafe_block(&self) { unsafe {} @@ -28,7 +28,7 @@ impl C { } #[derive(Deserialize)] -pub struct D {} +pub struct D; impl D { pub fn inner_unsafe_fn(&self) { unsafe fn inner() {} @@ -36,7 +36,7 @@ impl D { } // Does not derive `Deserialize`, should be ignored -pub struct E {} +pub struct E; impl E { pub unsafe fn new(_a: i32, _b: i32) -> Self { Self {} @@ -55,12 +55,12 @@ impl E { // Does not have methods using `unsafe`, should be ignored #[derive(Deserialize)] -pub struct F {} +pub struct F; // Check that we honor the `allow` attribute on the ADT #[allow(clippy::unsafe_derive_deserialize)] #[derive(Deserialize)] -pub struct G {} +pub struct G; impl G { pub fn unsafe_block(&self) { unsafe {} diff --git a/tests/ui/unsafe_removed_from_name.rs b/tests/ui/unsafe_removed_from_name.rs index a1f616733bd92..cde4e96d668c2 100644 --- a/tests/ui/unsafe_removed_from_name.rs +++ b/tests/ui/unsafe_removed_from_name.rs @@ -14,8 +14,8 @@ use std::cell::UnsafeCell as Dangerunsafe; use std::cell::UnsafeCell as Bombsawayunsafe; mod mod_with_some_unsafe_things { - pub struct Safe {} - pub struct Unsafe {} + pub struct Safe; + pub struct Unsafe; } use mod_with_some_unsafe_things::Unsafe as LieAboutModSafety; diff --git a/tests/ui/unused_self.rs b/tests/ui/unused_self.rs index 7a4bbdda1ab27..08bf58fec7c3e 100644 --- a/tests/ui/unused_self.rs +++ b/tests/ui/unused_self.rs @@ -5,7 +5,7 @@ mod unused_self { use std::pin::Pin; use std::sync::{Arc, Mutex}; - struct A {} + struct A; impl A { fn unused_self_move(self) {} @@ -27,7 +27,7 @@ mod unused_self { } mod unused_self_allow { - struct A {} + struct A; impl A { // shouldn't trigger @@ -35,7 +35,7 @@ mod unused_self_allow { fn unused_self_move(self) {} } - struct B {} + struct B; // shouldn't trigger #[allow(clippy::unused_self)] @@ -43,7 +43,7 @@ mod unused_self_allow { fn unused_self_move(self) {} } - struct C {} + struct C; #[allow(clippy::unused_self)] impl C { @@ -120,7 +120,7 @@ mod used_self { mod not_applicable { use std::fmt; - struct A {} + struct A; impl fmt::Debug for A { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { diff --git a/tests/ui/use_self.fixed b/tests/ui/use_self.fixed index 9d216f56ae60c..3e62ffe74fedd 100644 --- a/tests/ui/use_self.fixed +++ b/tests/ui/use_self.fixed @@ -16,7 +16,7 @@ extern crate proc_macro_derive; fn main() {} mod use_self { - struct Foo {} + struct Foo; impl Foo { fn new() -> Self { @@ -35,7 +35,7 @@ mod use_self { } mod better { - struct Foo {} + struct Foo; impl Foo { fn new() -> Self { @@ -123,7 +123,7 @@ mod macros { }; } - struct Foo {} + struct Foo; impl Foo { use_self_expand!(); // Should not lint in local macros @@ -134,7 +134,7 @@ mod macros { } mod nesting { - struct Foo {} + struct Foo; impl Foo { fn foo() { #[allow(unused_imports)] @@ -209,7 +209,7 @@ mod issue3410 { #[allow(clippy::no_effect, path_statements)] mod rustfix { mod nested { - pub struct A {} + pub struct A; } impl nested::A { @@ -227,7 +227,7 @@ mod rustfix { } mod issue3567 { - struct TestStruct {} + struct TestStruct; impl TestStruct { fn from_something() -> Self { Self {} @@ -248,7 +248,7 @@ mod issue3567 { mod paths_created_by_lowering { use std::ops::Range; - struct S {} + struct S; impl S { const A: usize = 0; @@ -382,7 +382,7 @@ mod issue4305 { } mod lint_at_item_level { - struct Foo {} + struct Foo; #[allow(clippy::use_self)] impl Foo { @@ -400,7 +400,7 @@ mod lint_at_item_level { } mod lint_at_impl_item_level { - struct Foo {} + struct Foo; impl Foo { #[allow(clippy::use_self)] @@ -433,8 +433,8 @@ mod issue4734 { mod nested_paths { use std::convert::Into; mod submod { - pub struct B {} - pub struct C {} + pub struct B; + pub struct C; impl Into for B { fn into(self) -> C { diff --git a/tests/ui/use_self.rs b/tests/ui/use_self.rs index 5f604fe25e416..da2faddee12a7 100644 --- a/tests/ui/use_self.rs +++ b/tests/ui/use_self.rs @@ -16,7 +16,7 @@ extern crate proc_macro_derive; fn main() {} mod use_self { - struct Foo {} + struct Foo; impl Foo { fn new() -> Foo { @@ -35,7 +35,7 @@ mod use_self { } mod better { - struct Foo {} + struct Foo; impl Foo { fn new() -> Self { @@ -123,7 +123,7 @@ mod macros { }; } - struct Foo {} + struct Foo; impl Foo { use_self_expand!(); // Should not lint in local macros @@ -134,7 +134,7 @@ mod macros { } mod nesting { - struct Foo {} + struct Foo; impl Foo { fn foo() { #[allow(unused_imports)] @@ -209,7 +209,7 @@ mod issue3410 { #[allow(clippy::no_effect, path_statements)] mod rustfix { mod nested { - pub struct A {} + pub struct A; } impl nested::A { @@ -227,7 +227,7 @@ mod rustfix { } mod issue3567 { - struct TestStruct {} + struct TestStruct; impl TestStruct { fn from_something() -> Self { Self {} @@ -248,7 +248,7 @@ mod issue3567 { mod paths_created_by_lowering { use std::ops::Range; - struct S {} + struct S; impl S { const A: usize = 0; @@ -382,7 +382,7 @@ mod issue4305 { } mod lint_at_item_level { - struct Foo {} + struct Foo; #[allow(clippy::use_self)] impl Foo { @@ -400,7 +400,7 @@ mod lint_at_item_level { } mod lint_at_impl_item_level { - struct Foo {} + struct Foo; impl Foo { #[allow(clippy::use_self)] @@ -433,8 +433,8 @@ mod issue4734 { mod nested_paths { use std::convert::Into; mod submod { - pub struct B {} - pub struct C {} + pub struct B; + pub struct C; impl Into for B { fn into(self) -> C { diff --git a/tests/ui/useless_attribute.fixed b/tests/ui/useless_attribute.fixed index a5fcde768f183..ce58a80347b55 100644 --- a/tests/ui/useless_attribute.fixed +++ b/tests/ui/useless_attribute.fixed @@ -42,7 +42,7 @@ mod a { mod b { #[allow(dead_code)] #[allow(unreachable_pub)] - pub struct C {} + pub struct C; } #[allow(unreachable_pub)] diff --git a/tests/ui/useless_attribute.rs b/tests/ui/useless_attribute.rs index 0396d39e3d54e..c82bb9ba07fd7 100644 --- a/tests/ui/useless_attribute.rs +++ b/tests/ui/useless_attribute.rs @@ -42,7 +42,7 @@ mod a { mod b { #[allow(dead_code)] #[allow(unreachable_pub)] - pub struct C {} + pub struct C; } #[allow(unreachable_pub)] From 315521afc6a574eab8b9bbc6b14b412f9cdc0810 Mon Sep 17 00:00:00 2001 From: Max Baumann Date: Sun, 27 Mar 2022 15:26:36 +0200 Subject: [PATCH 03/12] fix uitests --- tests/ui/module_name_repetitions.stderr | 4 +- tests/ui/no_effect.rs | 3 +- tests/ui/no_effect.stderr | 60 +++++++++++------------ tests/ui/redundant_allocation_fixable.rs | 2 +- tests/ui/unit_like_struct_brackets.stderr | 4 +- 5 files changed, 37 insertions(+), 36 deletions(-) diff --git a/tests/ui/module_name_repetitions.stderr b/tests/ui/module_name_repetitions.stderr index bdd217a969c05..3f343a3e43018 100644 --- a/tests/ui/module_name_repetitions.stderr +++ b/tests/ui/module_name_repetitions.stderr @@ -15,8 +15,8 @@ LL | pub fn bar_foo() {} error: item name starts with its containing module's name --> $DIR/module_name_repetitions.rs:10:5 | -LL | pub struct FooCake {} - | ^^^^^^^^^^^^^^^^^^^^^ +LL | pub struct FooCake; + | ^^^^^^^^^^^^^^^^^^^ error: item name ends with its containing module's name --> $DIR/module_name_repetitions.rs:11:5 diff --git a/tests/ui/no_effect.rs b/tests/ui/no_effect.rs index 0a67fb72044c0..291dab4fb3144 100644 --- a/tests/ui/no_effect.rs +++ b/tests/ui/no_effect.rs @@ -68,7 +68,8 @@ impl FnOnce<(&str,)> for GreetStruct1 { } } -struct GreetStruct2; +#[allow(clippy::unit_like_struct_brackets)] +struct GreetStruct2(); impl FnOnce<(&str,)> for GreetStruct2 { type Output = (); diff --git a/tests/ui/no_effect.stderr b/tests/ui/no_effect.stderr index 06b88bb5bee7a..ee57e0d26df83 100644 --- a/tests/ui/no_effect.stderr +++ b/tests/ui/no_effect.stderr @@ -1,5 +1,5 @@ error: statement with no effect - --> $DIR/no_effect.rs:95:5 + --> $DIR/no_effect.rs:96:5 | LL | 0; | ^^ @@ -7,157 +7,157 @@ LL | 0; = note: `-D clippy::no-effect` implied by `-D warnings` error: statement with no effect - --> $DIR/no_effect.rs:96:5 + --> $DIR/no_effect.rs:97:5 | LL | s2; | ^^^ error: statement with no effect - --> $DIR/no_effect.rs:97:5 + --> $DIR/no_effect.rs:98:5 | LL | Unit; | ^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:98:5 + --> $DIR/no_effect.rs:99:5 | LL | Tuple(0); | ^^^^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:99:5 + --> $DIR/no_effect.rs:100:5 | LL | Struct { field: 0 }; | ^^^^^^^^^^^^^^^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:100:5 + --> $DIR/no_effect.rs:101:5 | LL | Struct { ..s }; | ^^^^^^^^^^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:101:5 + --> $DIR/no_effect.rs:102:5 | LL | Union { a: 0 }; | ^^^^^^^^^^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:102:5 + --> $DIR/no_effect.rs:103:5 | LL | Enum::Tuple(0); | ^^^^^^^^^^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:103:5 + --> $DIR/no_effect.rs:104:5 | LL | Enum::Struct { field: 0 }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:104:5 + --> $DIR/no_effect.rs:105:5 | LL | 5 + 6; | ^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:105:5 + --> $DIR/no_effect.rs:106:5 | LL | *&42; | ^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:106:5 + --> $DIR/no_effect.rs:107:5 | LL | &6; | ^^^ error: statement with no effect - --> $DIR/no_effect.rs:107:5 + --> $DIR/no_effect.rs:108:5 | LL | (5, 6, 7); | ^^^^^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:108:5 + --> $DIR/no_effect.rs:109:5 | LL | box 42; | ^^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:109:5 + --> $DIR/no_effect.rs:110:5 | LL | ..; | ^^^ error: statement with no effect - --> $DIR/no_effect.rs:110:5 + --> $DIR/no_effect.rs:111:5 | LL | 5..; | ^^^^ error: statement with no effect - --> $DIR/no_effect.rs:111:5 + --> $DIR/no_effect.rs:112:5 | LL | ..5; | ^^^^ error: statement with no effect - --> $DIR/no_effect.rs:112:5 + --> $DIR/no_effect.rs:113:5 | LL | 5..6; | ^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:113:5 + --> $DIR/no_effect.rs:114:5 | LL | 5..=6; | ^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:114:5 + --> $DIR/no_effect.rs:115:5 | LL | [42, 55]; | ^^^^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:115:5 + --> $DIR/no_effect.rs:116:5 | LL | [42, 55][1]; | ^^^^^^^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:116:5 + --> $DIR/no_effect.rs:117:5 | LL | (42, 55).1; | ^^^^^^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:117:5 + --> $DIR/no_effect.rs:118:5 | LL | [42; 55]; | ^^^^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:118:5 + --> $DIR/no_effect.rs:119:5 | LL | [42; 55][13]; | ^^^^^^^^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:120:5 + --> $DIR/no_effect.rs:121:5 | LL | || x += 5; | ^^^^^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:122:5 + --> $DIR/no_effect.rs:123:5 | LL | FooString { s: s }; | ^^^^^^^^^^^^^^^^^^^ error: binding to `_` prefixed variable with no side-effect - --> $DIR/no_effect.rs:123:5 + --> $DIR/no_effect.rs:124:5 | LL | let _unused = 1; | ^^^^^^^^^^^^^^^^ @@ -165,19 +165,19 @@ LL | let _unused = 1; = note: `-D clippy::no-effect-underscore-binding` implied by `-D warnings` error: binding to `_` prefixed variable with no side-effect - --> $DIR/no_effect.rs:124:5 + --> $DIR/no_effect.rs:125:5 | LL | let _penguin = || println!("Some helpful closure"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: binding to `_` prefixed variable with no side-effect - --> $DIR/no_effect.rs:125:5 + --> $DIR/no_effect.rs:126:5 | LL | let _duck = Struct { field: 0 }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: binding to `_` prefixed variable with no side-effect - --> $DIR/no_effect.rs:126:5 + --> $DIR/no_effect.rs:127:5 | LL | let _cat = [2, 4, 6, 8][2]; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/ui/redundant_allocation_fixable.rs b/tests/ui/redundant_allocation_fixable.rs index fefa87721d720..de763f98b5c89 100644 --- a/tests/ui/redundant_allocation_fixable.rs +++ b/tests/ui/redundant_allocation_fixable.rs @@ -4,7 +4,7 @@ #![allow(clippy::blacklisted_name, unused_variables, dead_code)] #![allow(unused_imports)] -pub struct MyStruct {} +pub struct MyStruct; pub struct SubT { foo: T, diff --git a/tests/ui/unit_like_struct_brackets.stderr b/tests/ui/unit_like_struct_brackets.stderr index 146ede19c0719..7e7ba11cc7198 100644 --- a/tests/ui/unit_like_struct_brackets.stderr +++ b/tests/ui/unit_like_struct_brackets.stderr @@ -1,7 +1,7 @@ error: found empty brackets on struct declaration --> $DIR/unit_like_struct_brackets.rs:5:25 | -LL | pub struct MyEmptyStruct {} // should trigger lint +LL | pub struct MyEmptyStruct {} // should trigger lint | ^^^ help: remove the brackets: `;` | = note: `-D clippy::unit-like-struct-brackets` implied by `-D warnings` @@ -9,7 +9,7 @@ LL | pub struct MyEmptyStruct {} // should trigger lint error: found empty brackets on struct declaration --> $DIR/unit_like_struct_brackets.rs:6:26 | -LL | struct MyEmptyTupleStruct(); // should trigger lint +LL | struct MyEmptyTupleStruct(); // should trigger lint | ^^^ help: remove the brackets: `;` error: aborting due to 2 previous errors From 1a5ff38f92971141d2f1d39cd0ddc80188f746de Mon Sep 17 00:00:00 2001 From: Max Baumann Date: Sun, 27 Mar 2022 15:33:31 +0200 Subject: [PATCH 04/12] fix godfood test --- clippy_lints/src/unit_like_struct_brackets.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/unit_like_struct_brackets.rs b/clippy_lints/src/unit_like_struct_brackets.rs index 0eeb765be8518..c4623df46bdaa 100644 --- a/clippy_lints/src/unit_like_struct_brackets.rs +++ b/clippy_lints/src/unit_like_struct_brackets.rs @@ -1,5 +1,5 @@ use clippy_utils::diagnostics::span_lint_and_sugg; -use rustc_ast::ast::*; +use rustc_ast::ast::{Item, ItemKind, VariantData}; use rustc_errors::Applicability; use rustc_lint::{EarlyContext, EarlyLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; @@ -46,8 +46,7 @@ impl EarlyLintPass for UnitLikeStructBrackets { fn has_no_fields(var_data: &VariantData) -> bool { match var_data { - VariantData::Struct(defs, _) => defs.is_empty(), - VariantData::Tuple(defs, _) => defs.is_empty(), + VariantData::Struct(defs, _) | VariantData::Tuple(defs, _) => defs.is_empty(), VariantData::Unit(_) => false, } } From 7192297c2878b22e44ca5bc58f5f7544b33cdf4a Mon Sep 17 00:00:00 2001 From: Max Baumann Date: Sun, 27 Mar 2022 19:58:23 +0200 Subject: [PATCH 05/12] additional checks for conditionally compiled code --- clippy_lints/src/unit_like_struct_brackets.rs | 56 +++++++++++++++++-- tests/ui/unit_like_struct_brackets.fixed | 11 +++- tests/ui/unit_like_struct_brackets.rs | 11 +++- 3 files changed, 70 insertions(+), 8 deletions(-) diff --git a/clippy_lints/src/unit_like_struct_brackets.rs b/clippy_lints/src/unit_like_struct_brackets.rs index c4623df46bdaa..cb6e2a44afbe3 100644 --- a/clippy_lints/src/unit_like_struct_brackets.rs +++ b/clippy_lints/src/unit_like_struct_brackets.rs @@ -1,8 +1,10 @@ -use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::{diagnostics::span_lint_and_sugg, source::snippet_opt}; use rustc_ast::ast::{Item, ItemKind, VariantData}; use rustc_errors::Applicability; +use rustc_lexer::TokenKind; use rustc_lint::{EarlyContext, EarlyLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; +use rustc_span::Span; declare_clippy_lint! { /// ### What it does @@ -28,9 +30,9 @@ declare_lint_pass!(UnitLikeStructBrackets => [UNIT_LIKE_STRUCT_BRACKETS]); impl EarlyLintPass for UnitLikeStructBrackets { fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { - if let ItemKind::Struct(var_data, _) = &item.kind && has_no_fields(var_data) { - let span_after_ident = item.span.with_lo(item.ident.span.hi()); + let span_after_ident = item.span.with_lo(item.ident.span.hi()); + if let ItemKind::Struct(var_data, _) = &item.kind && has_no_fields(cx, var_data, span_after_ident) { span_lint_and_sugg( cx, UNIT_LIKE_STRUCT_BRACKETS, @@ -44,9 +46,51 @@ impl EarlyLintPass for UnitLikeStructBrackets { } } -fn has_no_fields(var_data: &VariantData) -> bool { +fn has_fields_in_hir(var_data: &VariantData) -> bool { match var_data { - VariantData::Struct(defs, _) | VariantData::Tuple(defs, _) => defs.is_empty(), - VariantData::Unit(_) => false, + VariantData::Struct(defs, _) | VariantData::Tuple(defs, _) => !defs.is_empty(), + VariantData::Unit(_) => true, + } +} + +fn has_no_ident_token(braces_span_str: &str) -> bool { + !rustc_lexer::tokenize(braces_span_str).any(|t| t.kind == TokenKind::Ident) +} + +fn has_no_fields(cx: &EarlyContext<'_>, var_data: &VariantData, braces_span: Span) -> bool { + if has_fields_in_hir(var_data) { + return false; + } + + // there might still be field declarations hidden from HIR + // (conditionaly compiled code using #[cfg(..)]) + + let Some(braces_span_str) = snippet_opt(cx, braces_span) else { + return false; + }; + + has_no_ident_token(braces_span_str.as_ref()) +} + +#[cfg(test)] +mod unit_test { + use super::*; + + #[test] + fn test_has_no_ident_token() { + let input = "{ field: u8 }"; + assert!(!has_no_ident_token(input)); + + let input = "(u8, String);"; + assert!(!has_no_ident_token(input)); + + let input = " { + // test = 5 + } + "; + assert!(has_no_ident_token(input)); + + let input = " ();"; + assert!(has_no_ident_token(input)); } } diff --git a/tests/ui/unit_like_struct_brackets.fixed b/tests/ui/unit_like_struct_brackets.fixed index e6cf4a0c0b465..78764337ec0f3 100644 --- a/tests/ui/unit_like_struct_brackets.fixed +++ b/tests/ui/unit_like_struct_brackets.fixed @@ -5,8 +5,17 @@ pub struct MyEmptyStruct; // should trigger lint struct MyEmptyTupleStruct; // should trigger lint +// should not trigger lint +struct MyCfgStruct { + #[cfg(feature = "thisisneverenabled")] + field: u8, +} + +// should not trigger lint +struct MyCfgTupleStruct(#[cfg(feature = "thisisneverenabled")] u8); + +// should not trigger lint struct MyStruct { - // should not trigger lint field: u8, } struct MyTupleStruct(usize, String); // should not trigger lint diff --git a/tests/ui/unit_like_struct_brackets.rs b/tests/ui/unit_like_struct_brackets.rs index 306e4c207d852..c43f97adfe90f 100644 --- a/tests/ui/unit_like_struct_brackets.rs +++ b/tests/ui/unit_like_struct_brackets.rs @@ -5,8 +5,17 @@ pub struct MyEmptyStruct {} // should trigger lint struct MyEmptyTupleStruct(); // should trigger lint +// should not trigger lint +struct MyCfgStruct { + #[cfg(feature = "thisisneverenabled")] + field: u8, +} + +// should not trigger lint +struct MyCfgTupleStruct(#[cfg(feature = "thisisneverenabled")] u8); + +// should not trigger lint struct MyStruct { - // should not trigger lint field: u8, } struct MyTupleStruct(usize, String); // should not trigger lint From 33383a418d6e57995a35a08b6b3d74dc010b9189 Mon Sep 17 00:00:00 2001 From: Max Baumann Date: Sun, 27 Mar 2022 20:10:10 +0200 Subject: [PATCH 06/12] use span_suggestion_hidden --- clippy_lints/src/unit_like_struct_brackets.rs | 14 +++++++++----- tests/ui/unit_like_struct_brackets.stderr | 7 +++++-- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/clippy_lints/src/unit_like_struct_brackets.rs b/clippy_lints/src/unit_like_struct_brackets.rs index cb6e2a44afbe3..7c93aa57d9bf6 100644 --- a/clippy_lints/src/unit_like_struct_brackets.rs +++ b/clippy_lints/src/unit_like_struct_brackets.rs @@ -1,4 +1,4 @@ -use clippy_utils::{diagnostics::span_lint_and_sugg, source::snippet_opt}; +use clippy_utils::{diagnostics::span_lint_and_then, source::snippet_opt}; use rustc_ast::ast::{Item, ItemKind, VariantData}; use rustc_errors::Applicability; use rustc_lexer::TokenKind; @@ -33,14 +33,18 @@ impl EarlyLintPass for UnitLikeStructBrackets { let span_after_ident = item.span.with_lo(item.ident.span.hi()); if let ItemKind::Struct(var_data, _) = &item.kind && has_no_fields(cx, var_data, span_after_ident) { - span_lint_and_sugg( + span_lint_and_then( cx, UNIT_LIKE_STRUCT_BRACKETS, span_after_ident, "found empty brackets on struct declaration", - "remove the brackets", - ";".to_string(), - Applicability::MachineApplicable + |diagnostic| { + diagnostic.span_suggestion_hidden( + span_after_ident, + "remove the brackets", + ";".to_string(), + Applicability::MachineApplicable); + }, ); } } diff --git a/tests/ui/unit_like_struct_brackets.stderr b/tests/ui/unit_like_struct_brackets.stderr index 7e7ba11cc7198..d3037f5d350a4 100644 --- a/tests/ui/unit_like_struct_brackets.stderr +++ b/tests/ui/unit_like_struct_brackets.stderr @@ -2,15 +2,18 @@ error: found empty brackets on struct declaration --> $DIR/unit_like_struct_brackets.rs:5:25 | LL | pub struct MyEmptyStruct {} // should trigger lint - | ^^^ help: remove the brackets: `;` + | ^^^ | = note: `-D clippy::unit-like-struct-brackets` implied by `-D warnings` + = help: remove the brackets error: found empty brackets on struct declaration --> $DIR/unit_like_struct_brackets.rs:6:26 | LL | struct MyEmptyTupleStruct(); // should trigger lint - | ^^^ help: remove the brackets: `;` + | ^^^ + | + = help: remove the brackets error: aborting due to 2 previous errors From 37d5a6264c3ad22f878351267fbbfe47e62cd35b Mon Sep 17 00:00:00 2001 From: Max Baumann Date: Mon, 28 Mar 2022 11:18:20 +0200 Subject: [PATCH 07/12] changes after review --- clippy_lints/src/unit_like_struct_brackets.rs | 21 +++++++++---------- tests/ui/unit_like_struct_brackets.fixed | 2 ++ tests/ui/unit_like_struct_brackets.rs | 2 ++ 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/clippy_lints/src/unit_like_struct_brackets.rs b/clippy_lints/src/unit_like_struct_brackets.rs index 7c93aa57d9bf6..6269719e696c3 100644 --- a/clippy_lints/src/unit_like_struct_brackets.rs +++ b/clippy_lints/src/unit_like_struct_brackets.rs @@ -21,7 +21,7 @@ declare_clippy_lint! { /// ```rust /// struct Cookie; /// ``` - #[clippy::version = "1.61.0"] + #[clippy::version = "1.62.0"] pub UNIT_LIKE_STRUCT_BRACKETS, style, "finds struct declarations with empty brackets" @@ -32,7 +32,9 @@ impl EarlyLintPass for UnitLikeStructBrackets { fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { let span_after_ident = item.span.with_lo(item.ident.span.hi()); - if let ItemKind::Struct(var_data, _) = &item.kind && has_no_fields(cx, var_data, span_after_ident) { + if let ItemKind::Struct(var_data, _) = &item.kind + && !is_unit_like_struct(var_data) + && has_no_fields(cx, var_data, span_after_ident) { span_lint_and_then( cx, UNIT_LIKE_STRUCT_BRACKETS, @@ -50,23 +52,20 @@ impl EarlyLintPass for UnitLikeStructBrackets { } } -fn has_fields_in_hir(var_data: &VariantData) -> bool { - match var_data { - VariantData::Struct(defs, _) | VariantData::Tuple(defs, _) => !defs.is_empty(), - VariantData::Unit(_) => true, - } -} - fn has_no_ident_token(braces_span_str: &str) -> bool { !rustc_lexer::tokenize(braces_span_str).any(|t| t.kind == TokenKind::Ident) } +fn is_unit_like_struct(var_data: &VariantData) -> bool { + matches!(var_data, VariantData::Unit(_)) +} + fn has_no_fields(cx: &EarlyContext<'_>, var_data: &VariantData, braces_span: Span) -> bool { - if has_fields_in_hir(var_data) { + if !var_data.fields().is_empty() { return false; } - // there might still be field declarations hidden from HIR + // there might still be field declarations hidden from the AST // (conditionaly compiled code using #[cfg(..)]) let Some(braces_span_str) = snippet_opt(cx, braces_span) else { diff --git a/tests/ui/unit_like_struct_brackets.fixed b/tests/ui/unit_like_struct_brackets.fixed index 78764337ec0f3..2769920c5fe9c 100644 --- a/tests/ui/unit_like_struct_brackets.fixed +++ b/tests/ui/unit_like_struct_brackets.fixed @@ -19,5 +19,7 @@ struct MyStruct { field: u8, } struct MyTupleStruct(usize, String); // should not trigger lint +struct MySingleTupleStruct(usize); // should not trigger lint +struct MyUnitLikeStruct; // should not trigger lint fn main() {} diff --git a/tests/ui/unit_like_struct_brackets.rs b/tests/ui/unit_like_struct_brackets.rs index c43f97adfe90f..b20f8516bd117 100644 --- a/tests/ui/unit_like_struct_brackets.rs +++ b/tests/ui/unit_like_struct_brackets.rs @@ -19,5 +19,7 @@ struct MyStruct { field: u8, } struct MyTupleStruct(usize, String); // should not trigger lint +struct MySingleTupleStruct(usize); // should not trigger lint +struct MyUnitLikeStruct; // should not trigger lint fn main() {} From 2953cba1160cb4dd8fe4923a4e9166b3ca6867f1 Mon Sep 17 00:00:00 2001 From: Max Baumann Date: Mon, 28 Mar 2022 11:35:43 +0200 Subject: [PATCH 08/12] unit_like_struct_brackets -> empty_structs_with_brackets --- CHANGELOG.md | 2 +- ..._struct_brackets.rs => empty_structs_with_brackets.rs} | 8 ++++---- clippy_lints/src/lib.register_all.rs | 2 +- clippy_lints/src/lib.register_lints.rs | 2 +- clippy_lints/src/lib.register_style.rs | 2 +- clippy_lints/src/lib.rs | 4 ++-- ...t_brackets.fixed => empty_structs_with_brackets.fixed} | 2 +- ..._struct_brackets.rs => empty_structs_with_brackets.rs} | 2 +- ...brackets.stderr => empty_structs_with_brackets.stderr} | 6 +++--- tests/ui/no_effect.rs | 2 +- 10 files changed, 16 insertions(+), 16 deletions(-) rename clippy_lints/src/{unit_like_struct_brackets.rs => empty_structs_with_brackets.rs} (92%) rename tests/ui/{unit_like_struct_brackets.fixed => empty_structs_with_brackets.fixed} (92%) rename tests/ui/{unit_like_struct_brackets.rs => empty_structs_with_brackets.rs} (92%) rename tests/ui/{unit_like_struct_brackets.stderr => empty_structs_with_brackets.stderr} (70%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 49a44d988139b..0aa6a01976249 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3263,6 +3263,7 @@ Released 2018-09-13 [`empty_enum`]: https://rust-lang.github.io/rust-clippy/master/index.html#empty_enum [`empty_line_after_outer_attr`]: https://rust-lang.github.io/rust-clippy/master/index.html#empty_line_after_outer_attr [`empty_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#empty_loop +[`empty_structs_with_brackets`]: https://rust-lang.github.io/rust-clippy/master/index.html#empty_structs_with_brackets [`enum_clike_unportable_variant`]: https://rust-lang.github.io/rust-clippy/master/index.html#enum_clike_unportable_variant [`enum_glob_use`]: https://rust-lang.github.io/rust-clippy/master/index.html#enum_glob_use [`enum_variant_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#enum_variant_names @@ -3636,7 +3637,6 @@ Released 2018-09-13 [`unit_arg`]: https://rust-lang.github.io/rust-clippy/master/index.html#unit_arg [`unit_cmp`]: https://rust-lang.github.io/rust-clippy/master/index.html#unit_cmp [`unit_hash`]: https://rust-lang.github.io/rust-clippy/master/index.html#unit_hash -[`unit_like_struct_brackets`]: https://rust-lang.github.io/rust-clippy/master/index.html#unit_like_struct_brackets [`unit_return_expecting_ord`]: https://rust-lang.github.io/rust-clippy/master/index.html#unit_return_expecting_ord [`unnecessary_cast`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast [`unnecessary_filter_map`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_filter_map diff --git a/clippy_lints/src/unit_like_struct_brackets.rs b/clippy_lints/src/empty_structs_with_brackets.rs similarity index 92% rename from clippy_lints/src/unit_like_struct_brackets.rs rename to clippy_lints/src/empty_structs_with_brackets.rs index 6269719e696c3..e3f33c1a4a302 100644 --- a/clippy_lints/src/unit_like_struct_brackets.rs +++ b/clippy_lints/src/empty_structs_with_brackets.rs @@ -22,13 +22,13 @@ declare_clippy_lint! { /// struct Cookie; /// ``` #[clippy::version = "1.62.0"] - pub UNIT_LIKE_STRUCT_BRACKETS, + pub EMPTY_STRUCTS_WITH_BRACKETS, style, "finds struct declarations with empty brackets" } -declare_lint_pass!(UnitLikeStructBrackets => [UNIT_LIKE_STRUCT_BRACKETS]); +declare_lint_pass!(EmptyStructsWithBrackets => [EMPTY_STRUCTS_WITH_BRACKETS]); -impl EarlyLintPass for UnitLikeStructBrackets { +impl EarlyLintPass for EmptyStructsWithBrackets { fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { let span_after_ident = item.span.with_lo(item.ident.span.hi()); @@ -37,7 +37,7 @@ impl EarlyLintPass for UnitLikeStructBrackets { && has_no_fields(cx, var_data, span_after_ident) { span_lint_and_then( cx, - UNIT_LIKE_STRUCT_BRACKETS, + EMPTY_STRUCTS_WITH_BRACKETS, span_after_ident, "found empty brackets on struct declaration", |diagnostic| { diff --git a/clippy_lints/src/lib.register_all.rs b/clippy_lints/src/lib.register_all.rs index f488aab46eccd..bdad518a3e83c 100644 --- a/clippy_lints/src/lib.register_all.rs +++ b/clippy_lints/src/lib.register_all.rs @@ -54,6 +54,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![ LintId::of(drop_forget_ref::FORGET_COPY), LintId::of(drop_forget_ref::FORGET_REF), LintId::of(duration_subsec::DURATION_SUBSEC), + LintId::of(empty_structs_with_brackets::EMPTY_STRUCTS_WITH_BRACKETS), LintId::of(entry::MAP_ENTRY), LintId::of(enum_clike::ENUM_CLIKE_UNPORTABLE_VARIANT), LintId::of(enum_variants::ENUM_VARIANT_NAMES), @@ -301,7 +302,6 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![ LintId::of(unicode::INVISIBLE_CHARACTERS), LintId::of(uninit_vec::UNINIT_VEC), LintId::of(unit_hash::UNIT_HASH), - LintId::of(unit_like_struct_brackets::UNIT_LIKE_STRUCT_BRACKETS), LintId::of(unit_return_expecting_ord::UNIT_RETURN_EXPECTING_ORD), LintId::of(unit_types::UNIT_ARG), LintId::of(unit_types::UNIT_CMP), diff --git a/clippy_lints/src/lib.register_lints.rs b/clippy_lints/src/lib.register_lints.rs index a1e6ca76a16b5..bba3cae45f5e6 100644 --- a/clippy_lints/src/lib.register_lints.rs +++ b/clippy_lints/src/lib.register_lints.rs @@ -129,6 +129,7 @@ store.register_lints(&[ duration_subsec::DURATION_SUBSEC, else_if_without_else::ELSE_IF_WITHOUT_ELSE, empty_enum::EMPTY_ENUM, + empty_structs_with_brackets::EMPTY_STRUCTS_WITH_BRACKETS, entry::MAP_ENTRY, enum_clike::ENUM_CLIKE_UNPORTABLE_VARIANT, enum_variants::ENUM_VARIANT_NAMES, @@ -512,7 +513,6 @@ store.register_lints(&[ unicode::UNICODE_NOT_NFC, uninit_vec::UNINIT_VEC, unit_hash::UNIT_HASH, - unit_like_struct_brackets::UNIT_LIKE_STRUCT_BRACKETS, unit_return_expecting_ord::UNIT_RETURN_EXPECTING_ORD, unit_types::LET_UNIT_VALUE, unit_types::UNIT_ARG, diff --git a/clippy_lints/src/lib.register_style.rs b/clippy_lints/src/lib.register_style.rs index 89657cfb7842a..b2465d1a0cd95 100644 --- a/clippy_lints/src/lib.register_style.rs +++ b/clippy_lints/src/lib.register_style.rs @@ -20,6 +20,7 @@ store.register_group(true, "clippy::style", Some("clippy_style"), vec![ LintId::of(disallowed_types::DISALLOWED_TYPES), LintId::of(doc::MISSING_SAFETY_DOC), LintId::of(doc::NEEDLESS_DOCTEST_MAIN), + LintId::of(empty_structs_with_brackets::EMPTY_STRUCTS_WITH_BRACKETS), LintId::of(enum_variants::ENUM_VARIANT_NAMES), LintId::of(enum_variants::MODULE_INCEPTION), LintId::of(eq_op::OP_REF), @@ -105,7 +106,6 @@ store.register_group(true, "clippy::style", Some("clippy_style"), vec![ LintId::of(single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS), LintId::of(tabs_in_doc_comments::TABS_IN_DOC_COMMENTS), LintId::of(to_digit_is_some::TO_DIGIT_IS_SOME), - LintId::of(unit_like_struct_brackets::UNIT_LIKE_STRUCT_BRACKETS), LintId::of(unsafe_removed_from_name::UNSAFE_REMOVED_FROM_NAME), LintId::of(unused_unit::UNUSED_UNIT), LintId::of(upper_case_acronyms::UPPER_CASE_ACRONYMS), diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index af8d9b3655c56..3138ee9bce5c8 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -209,6 +209,7 @@ mod drop_forget_ref; mod duration_subsec; mod else_if_without_else; mod empty_enum; +mod empty_structs_with_brackets; mod entry; mod enum_clike; mod enum_variants; @@ -380,7 +381,6 @@ mod undropped_manually_drops; mod unicode; mod uninit_vec; mod unit_hash; -mod unit_like_struct_brackets; mod unit_return_expecting_ord; mod unit_types; mod unnamed_address; @@ -870,7 +870,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: }) }); store.register_early_pass(|| Box::new(crate_in_macro_def::CrateInMacroDef)); - store.register_early_pass(|| Box::new(unit_like_struct_brackets::UnitLikeStructBrackets)); + store.register_early_pass(|| Box::new(empty_structs_with_brackets::EmptyStructsWithBrackets)); // add lints here, do not remove this comment, it's used in `new_lint` } diff --git a/tests/ui/unit_like_struct_brackets.fixed b/tests/ui/empty_structs_with_brackets.fixed similarity index 92% rename from tests/ui/unit_like_struct_brackets.fixed rename to tests/ui/empty_structs_with_brackets.fixed index 2769920c5fe9c..80f07603b8d4f 100644 --- a/tests/ui/unit_like_struct_brackets.fixed +++ b/tests/ui/empty_structs_with_brackets.fixed @@ -1,5 +1,5 @@ // run-rustfix -#![warn(clippy::unit_like_struct_brackets)] +#![warn(clippy::empty_structs_with_brackets)] #![allow(dead_code)] pub struct MyEmptyStruct; // should trigger lint diff --git a/tests/ui/unit_like_struct_brackets.rs b/tests/ui/empty_structs_with_brackets.rs similarity index 92% rename from tests/ui/unit_like_struct_brackets.rs rename to tests/ui/empty_structs_with_brackets.rs index b20f8516bd117..1d1ed4c769025 100644 --- a/tests/ui/unit_like_struct_brackets.rs +++ b/tests/ui/empty_structs_with_brackets.rs @@ -1,5 +1,5 @@ // run-rustfix -#![warn(clippy::unit_like_struct_brackets)] +#![warn(clippy::empty_structs_with_brackets)] #![allow(dead_code)] pub struct MyEmptyStruct {} // should trigger lint diff --git a/tests/ui/unit_like_struct_brackets.stderr b/tests/ui/empty_structs_with_brackets.stderr similarity index 70% rename from tests/ui/unit_like_struct_brackets.stderr rename to tests/ui/empty_structs_with_brackets.stderr index d3037f5d350a4..0308cb5571af2 100644 --- a/tests/ui/unit_like_struct_brackets.stderr +++ b/tests/ui/empty_structs_with_brackets.stderr @@ -1,14 +1,14 @@ error: found empty brackets on struct declaration - --> $DIR/unit_like_struct_brackets.rs:5:25 + --> $DIR/empty_structs_with_brackets.rs:5:25 | LL | pub struct MyEmptyStruct {} // should trigger lint | ^^^ | - = note: `-D clippy::unit-like-struct-brackets` implied by `-D warnings` + = note: `-D clippy::empty-structs-with-brackets` implied by `-D warnings` = help: remove the brackets error: found empty brackets on struct declaration - --> $DIR/unit_like_struct_brackets.rs:6:26 + --> $DIR/empty_structs_with_brackets.rs:6:26 | LL | struct MyEmptyTupleStruct(); // should trigger lint | ^^^ diff --git a/tests/ui/no_effect.rs b/tests/ui/no_effect.rs index 291dab4fb3144..0847297ade79c 100644 --- a/tests/ui/no_effect.rs +++ b/tests/ui/no_effect.rs @@ -68,7 +68,7 @@ impl FnOnce<(&str,)> for GreetStruct1 { } } -#[allow(clippy::unit_like_struct_brackets)] +#[allow(clippy::empty_structs_with_brackets)] struct GreetStruct2(); impl FnOnce<(&str,)> for GreetStruct2 { From e552267db33377d913ca1c94c7377b1f5623cbf3 Mon Sep 17 00:00:00 2001 From: Max Baumann Date: Mon, 28 Mar 2022 11:45:06 +0200 Subject: [PATCH 09/12] style -> pedantic --- .../src/empty_structs_with_brackets.rs | 2 +- clippy_lints/src/lib.register_all.rs | 1 - clippy_lints/src/lib.register_pedantic.rs | 1 + clippy_lints/src/lib.register_style.rs | 1 - tests/ui/no_effect.rs | 1 - tests/ui/no_effect.stderr | 60 +++++++++---------- 6 files changed, 32 insertions(+), 34 deletions(-) diff --git a/clippy_lints/src/empty_structs_with_brackets.rs b/clippy_lints/src/empty_structs_with_brackets.rs index e3f33c1a4a302..0077cc5364fcf 100644 --- a/clippy_lints/src/empty_structs_with_brackets.rs +++ b/clippy_lints/src/empty_structs_with_brackets.rs @@ -23,7 +23,7 @@ declare_clippy_lint! { /// ``` #[clippy::version = "1.62.0"] pub EMPTY_STRUCTS_WITH_BRACKETS, - style, + pedantic, "finds struct declarations with empty brackets" } declare_lint_pass!(EmptyStructsWithBrackets => [EMPTY_STRUCTS_WITH_BRACKETS]); diff --git a/clippy_lints/src/lib.register_all.rs b/clippy_lints/src/lib.register_all.rs index bdad518a3e83c..1fb3ca1fd9b2b 100644 --- a/clippy_lints/src/lib.register_all.rs +++ b/clippy_lints/src/lib.register_all.rs @@ -54,7 +54,6 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![ LintId::of(drop_forget_ref::FORGET_COPY), LintId::of(drop_forget_ref::FORGET_REF), LintId::of(duration_subsec::DURATION_SUBSEC), - LintId::of(empty_structs_with_brackets::EMPTY_STRUCTS_WITH_BRACKETS), LintId::of(entry::MAP_ENTRY), LintId::of(enum_clike::ENUM_CLIKE_UNPORTABLE_VARIANT), LintId::of(enum_variants::ENUM_VARIANT_NAMES), diff --git a/clippy_lints/src/lib.register_pedantic.rs b/clippy_lints/src/lib.register_pedantic.rs index eb6534cb8cae7..3f0f438568fd5 100644 --- a/clippy_lints/src/lib.register_pedantic.rs +++ b/clippy_lints/src/lib.register_pedantic.rs @@ -27,6 +27,7 @@ store.register_group(true, "clippy::pedantic", Some("clippy_pedantic"), vec![ LintId::of(doc::MISSING_ERRORS_DOC), LintId::of(doc::MISSING_PANICS_DOC), LintId::of(empty_enum::EMPTY_ENUM), + LintId::of(empty_structs_with_brackets::EMPTY_STRUCTS_WITH_BRACKETS), LintId::of(enum_variants::MODULE_NAME_REPETITIONS), LintId::of(eta_reduction::REDUNDANT_CLOSURE_FOR_METHOD_CALLS), LintId::of(excessive_bools::FN_PARAMS_EXCESSIVE_BOOLS), diff --git a/clippy_lints/src/lib.register_style.rs b/clippy_lints/src/lib.register_style.rs index b2465d1a0cd95..dcf399cf9562f 100644 --- a/clippy_lints/src/lib.register_style.rs +++ b/clippy_lints/src/lib.register_style.rs @@ -20,7 +20,6 @@ store.register_group(true, "clippy::style", Some("clippy_style"), vec![ LintId::of(disallowed_types::DISALLOWED_TYPES), LintId::of(doc::MISSING_SAFETY_DOC), LintId::of(doc::NEEDLESS_DOCTEST_MAIN), - LintId::of(empty_structs_with_brackets::EMPTY_STRUCTS_WITH_BRACKETS), LintId::of(enum_variants::ENUM_VARIANT_NAMES), LintId::of(enum_variants::MODULE_INCEPTION), LintId::of(eq_op::OP_REF), diff --git a/tests/ui/no_effect.rs b/tests/ui/no_effect.rs index 0847297ade79c..7ece66a1ccb6f 100644 --- a/tests/ui/no_effect.rs +++ b/tests/ui/no_effect.rs @@ -68,7 +68,6 @@ impl FnOnce<(&str,)> for GreetStruct1 { } } -#[allow(clippy::empty_structs_with_brackets)] struct GreetStruct2(); impl FnOnce<(&str,)> for GreetStruct2 { diff --git a/tests/ui/no_effect.stderr b/tests/ui/no_effect.stderr index ee57e0d26df83..06b88bb5bee7a 100644 --- a/tests/ui/no_effect.stderr +++ b/tests/ui/no_effect.stderr @@ -1,5 +1,5 @@ error: statement with no effect - --> $DIR/no_effect.rs:96:5 + --> $DIR/no_effect.rs:95:5 | LL | 0; | ^^ @@ -7,157 +7,157 @@ LL | 0; = note: `-D clippy::no-effect` implied by `-D warnings` error: statement with no effect - --> $DIR/no_effect.rs:97:5 + --> $DIR/no_effect.rs:96:5 | LL | s2; | ^^^ error: statement with no effect - --> $DIR/no_effect.rs:98:5 + --> $DIR/no_effect.rs:97:5 | LL | Unit; | ^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:99:5 + --> $DIR/no_effect.rs:98:5 | LL | Tuple(0); | ^^^^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:100:5 + --> $DIR/no_effect.rs:99:5 | LL | Struct { field: 0 }; | ^^^^^^^^^^^^^^^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:101:5 + --> $DIR/no_effect.rs:100:5 | LL | Struct { ..s }; | ^^^^^^^^^^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:102:5 + --> $DIR/no_effect.rs:101:5 | LL | Union { a: 0 }; | ^^^^^^^^^^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:103:5 + --> $DIR/no_effect.rs:102:5 | LL | Enum::Tuple(0); | ^^^^^^^^^^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:104:5 + --> $DIR/no_effect.rs:103:5 | LL | Enum::Struct { field: 0 }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:105:5 + --> $DIR/no_effect.rs:104:5 | LL | 5 + 6; | ^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:106:5 + --> $DIR/no_effect.rs:105:5 | LL | *&42; | ^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:107:5 + --> $DIR/no_effect.rs:106:5 | LL | &6; | ^^^ error: statement with no effect - --> $DIR/no_effect.rs:108:5 + --> $DIR/no_effect.rs:107:5 | LL | (5, 6, 7); | ^^^^^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:109:5 + --> $DIR/no_effect.rs:108:5 | LL | box 42; | ^^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:110:5 + --> $DIR/no_effect.rs:109:5 | LL | ..; | ^^^ error: statement with no effect - --> $DIR/no_effect.rs:111:5 + --> $DIR/no_effect.rs:110:5 | LL | 5..; | ^^^^ error: statement with no effect - --> $DIR/no_effect.rs:112:5 + --> $DIR/no_effect.rs:111:5 | LL | ..5; | ^^^^ error: statement with no effect - --> $DIR/no_effect.rs:113:5 + --> $DIR/no_effect.rs:112:5 | LL | 5..6; | ^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:114:5 + --> $DIR/no_effect.rs:113:5 | LL | 5..=6; | ^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:115:5 + --> $DIR/no_effect.rs:114:5 | LL | [42, 55]; | ^^^^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:116:5 + --> $DIR/no_effect.rs:115:5 | LL | [42, 55][1]; | ^^^^^^^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:117:5 + --> $DIR/no_effect.rs:116:5 | LL | (42, 55).1; | ^^^^^^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:118:5 + --> $DIR/no_effect.rs:117:5 | LL | [42; 55]; | ^^^^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:119:5 + --> $DIR/no_effect.rs:118:5 | LL | [42; 55][13]; | ^^^^^^^^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:121:5 + --> $DIR/no_effect.rs:120:5 | LL | || x += 5; | ^^^^^^^^^^ error: statement with no effect - --> $DIR/no_effect.rs:123:5 + --> $DIR/no_effect.rs:122:5 | LL | FooString { s: s }; | ^^^^^^^^^^^^^^^^^^^ error: binding to `_` prefixed variable with no side-effect - --> $DIR/no_effect.rs:124:5 + --> $DIR/no_effect.rs:123:5 | LL | let _unused = 1; | ^^^^^^^^^^^^^^^^ @@ -165,19 +165,19 @@ LL | let _unused = 1; = note: `-D clippy::no-effect-underscore-binding` implied by `-D warnings` error: binding to `_` prefixed variable with no side-effect - --> $DIR/no_effect.rs:125:5 + --> $DIR/no_effect.rs:124:5 | LL | let _penguin = || println!("Some helpful closure"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: binding to `_` prefixed variable with no side-effect - --> $DIR/no_effect.rs:126:5 + --> $DIR/no_effect.rs:125:5 | LL | let _duck = Struct { field: 0 }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: binding to `_` prefixed variable with no side-effect - --> $DIR/no_effect.rs:127:5 + --> $DIR/no_effect.rs:126:5 | LL | let _cat = [2, 4, 6, 8][2]; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ From 49a9740b09448ac903a2a1a9b73f09e4ca7fc783 Mon Sep 17 00:00:00 2001 From: Max Baumann Date: Wed, 30 Mar 2022 13:08:39 +0200 Subject: [PATCH 10/12] update description Co-authored-by: giraffate --- clippy_lints/src/empty_structs_with_brackets.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clippy_lints/src/empty_structs_with_brackets.rs b/clippy_lints/src/empty_structs_with_brackets.rs index 0077cc5364fcf..15c06126fd5dd 100644 --- a/clippy_lints/src/empty_structs_with_brackets.rs +++ b/clippy_lints/src/empty_structs_with_brackets.rs @@ -8,7 +8,7 @@ use rustc_span::Span; declare_clippy_lint! { /// ### What it does - /// Finds structs without fields ("unit-like structs") that are declared with brackets. + /// Finds structs without fields (a so-called "empty struct") that are declared with brackets. /// /// ### Why is this bad? /// Empty brackets after a struct declaration can be omitted. From 0434b856ac4253364f2f622df41209412442bb75 Mon Sep 17 00:00:00 2001 From: Max Baumann Date: Wed, 30 Mar 2022 13:33:10 +0200 Subject: [PATCH 11/12] pedantic -> restriction --- clippy_lints/src/empty_structs_with_brackets.rs | 2 +- clippy_lints/src/lib.register_pedantic.rs | 1 - clippy_lints/src/lib.register_restriction.rs | 1 + 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/empty_structs_with_brackets.rs b/clippy_lints/src/empty_structs_with_brackets.rs index 15c06126fd5dd..8f2a0306aa054 100644 --- a/clippy_lints/src/empty_structs_with_brackets.rs +++ b/clippy_lints/src/empty_structs_with_brackets.rs @@ -23,7 +23,7 @@ declare_clippy_lint! { /// ``` #[clippy::version = "1.62.0"] pub EMPTY_STRUCTS_WITH_BRACKETS, - pedantic, + restriction, "finds struct declarations with empty brackets" } declare_lint_pass!(EmptyStructsWithBrackets => [EMPTY_STRUCTS_WITH_BRACKETS]); diff --git a/clippy_lints/src/lib.register_pedantic.rs b/clippy_lints/src/lib.register_pedantic.rs index 3f0f438568fd5..eb6534cb8cae7 100644 --- a/clippy_lints/src/lib.register_pedantic.rs +++ b/clippy_lints/src/lib.register_pedantic.rs @@ -27,7 +27,6 @@ store.register_group(true, "clippy::pedantic", Some("clippy_pedantic"), vec![ LintId::of(doc::MISSING_ERRORS_DOC), LintId::of(doc::MISSING_PANICS_DOC), LintId::of(empty_enum::EMPTY_ENUM), - LintId::of(empty_structs_with_brackets::EMPTY_STRUCTS_WITH_BRACKETS), LintId::of(enum_variants::MODULE_NAME_REPETITIONS), LintId::of(eta_reduction::REDUNDANT_CLOSURE_FOR_METHOD_CALLS), LintId::of(excessive_bools::FN_PARAMS_EXCESSIVE_BOOLS), diff --git a/clippy_lints/src/lib.register_restriction.rs b/clippy_lints/src/lib.register_restriction.rs index 6ab139b2fb67b..4802dd877e99d 100644 --- a/clippy_lints/src/lib.register_restriction.rs +++ b/clippy_lints/src/lib.register_restriction.rs @@ -16,6 +16,7 @@ store.register_group(true, "clippy::restriction", Some("clippy_restriction"), ve LintId::of(default_union_representation::DEFAULT_UNION_REPRESENTATION), LintId::of(disallowed_script_idents::DISALLOWED_SCRIPT_IDENTS), LintId::of(else_if_without_else::ELSE_IF_WITHOUT_ELSE), + LintId::of(empty_structs_with_brackets::EMPTY_STRUCTS_WITH_BRACKETS), LintId::of(exhaustive_items::EXHAUSTIVE_ENUMS), LintId::of(exhaustive_items::EXHAUSTIVE_STRUCTS), LintId::of(exit::EXIT), From 58833e58a643d1d3d4f2499d6395966245207e3d Mon Sep 17 00:00:00 2001 From: Max Baumann Date: Mon, 4 Apr 2022 08:48:49 +0200 Subject: [PATCH 12/12] is_unit_like_struct -> has_brackets --- clippy_lints/src/empty_structs_with_brackets.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/empty_structs_with_brackets.rs b/clippy_lints/src/empty_structs_with_brackets.rs index 8f2a0306aa054..fdeac8d82557f 100644 --- a/clippy_lints/src/empty_structs_with_brackets.rs +++ b/clippy_lints/src/empty_structs_with_brackets.rs @@ -33,7 +33,7 @@ impl EarlyLintPass for EmptyStructsWithBrackets { let span_after_ident = item.span.with_lo(item.ident.span.hi()); if let ItemKind::Struct(var_data, _) = &item.kind - && !is_unit_like_struct(var_data) + && has_brackets(var_data) && has_no_fields(cx, var_data, span_after_ident) { span_lint_and_then( cx, @@ -56,8 +56,8 @@ fn has_no_ident_token(braces_span_str: &str) -> bool { !rustc_lexer::tokenize(braces_span_str).any(|t| t.kind == TokenKind::Ident) } -fn is_unit_like_struct(var_data: &VariantData) -> bool { - matches!(var_data, VariantData::Unit(_)) +fn has_brackets(var_data: &VariantData) -> bool { + !matches!(var_data, VariantData::Unit(_)) } fn has_no_fields(cx: &EarlyContext<'_>, var_data: &VariantData, braces_span: Span) -> bool {