diff --git a/CHANGELOG.md b/CHANGELOG.md index 1585a4a8780..f0084652329 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ * [#6854](https://github.com/rubocop-hq/rubocop/pull/6854): Mark Rails/LexicallyScopedActionFilter as unsafe and document risks. ([@urbanautomaton][]) * [#5977](https://github.com/rubocop-hq/rubocop/issues/5977): Warn for Performance Cops. ([@koic][]) * [#6637](https://github.com/rubocop-hq/rubocop/issues/6637): Move `LstripRstrip` from `Performance` to `Style` department and rename it to `Strip`. ([@anuja-joshi][]) +* [#6875](https://github.com/rubocop-hq/rubocop/pull/6875): Mention block form of `Struct.new` in ` Style/StructInheritance`. ([@XrXr][]) ## 0.66.0 (2019-03-18) @@ -3903,4 +3904,5 @@ [@ericsullivan]: https://github.com/ericsullivan [@aeroastro]: https://github.com/aeroastro [@anuja-joshi]: https://github.com/anuja-joshi -[@thomthom]: https://github.com/thomthom +[@XrXr]: https://github.com/XrXr +[@thomthom]: https://github.com/thomthom \ No newline at end of file diff --git a/lib/rubocop/cop/style/struct_inheritance.rb b/lib/rubocop/cop/style/struct_inheritance.rb index 9ff05a9ecea..71520b61efa 100644 --- a/lib/rubocop/cop/style/struct_inheritance.rb +++ b/lib/rubocop/cop/style/struct_inheritance.rb @@ -8,12 +8,20 @@ module Style # @example # # bad # class Person < Struct.new(:first_name, :last_name) + # def age + # 42 + # end # end # # # good - # Person = Struct.new(:first_name, :last_name) + # Person = Struct.new(:first_name, :last_name) do + # def age + # 42 + # end + # end class StructInheritance < Cop - MSG = "Don't extend an instance initialized by `Struct.new`.".freeze + MSG = "Don't extend an instance initialized by `Struct.new`. " \ + 'Use a block to customize the struct.'.freeze def on_class(node) _name, superclass, _body = *node diff --git a/manual/cops_style.md b/manual/cops_style.md index ac22685c123..00b3cdb329f 100644 --- a/manual/cops_style.md +++ b/manual/cops_style.md @@ -5955,10 +5955,17 @@ This cop checks for inheritance from Struct.new. ```ruby # bad class Person < Struct.new(:first_name, :last_name) + def age + 42 + end end # good -Person = Struct.new(:first_name, :last_name) +Person = Struct.new(:first_name, :last_name) do + def age + 42 + end +end ``` ### References diff --git a/spec/rubocop/cop/style/struct_inheritance_spec.rb b/spec/rubocop/cop/style/struct_inheritance_spec.rb index e1451a6e10d..592203c6c45 100644 --- a/spec/rubocop/cop/style/struct_inheritance_spec.rb +++ b/spec/rubocop/cop/style/struct_inheritance_spec.rb @@ -6,7 +6,7 @@ it 'registers an offense when extending instance of Struct' do expect_offense(<<-RUBY.strip_indent) class Person < Struct.new(:first_name, :last_name) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't extend an instance initialized by `Struct.new`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't extend an instance initialized by `Struct.new`. Use a block to customize the struct. end RUBY end @@ -14,7 +14,7 @@ class Person < Struct.new(:first_name, :last_name) it 'registers an offense when extending instance of Struct with do ... end' do expect_offense(<<-RUBY.strip_indent) class Person < Struct.new(:first_name, :last_name) do end - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't extend an instance initialized by `Struct.new`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't extend an instance initialized by `Struct.new`. Use a block to customize the struct. end RUBY end @@ -36,4 +36,14 @@ class Person < DelegateClass(Animal) it 'accepts assignment to Struct.new' do expect_no_offenses('Person = Struct.new(:first_name, :last_name)') end + + it 'accepts assignment to block form of Struct.new' do + expect_no_offenses(<<-RUBY.strip_indent) + Person = Struct.new(:first_name, :last_name) do + def age + 42 + end + end + RUBY + end end