Skip to content

Commit

Permalink
Fix an error for Style/Copyright
Browse files Browse the repository at this point in the history
This PR fixes the following error for `Style/Copyright` when `AutocorrectNotice` is missing:

```console
$ cat .rubocop.yml
Style/Copyright:
  Enabled: true
  AutocorrectNotice: ~

$ bundle exec rubocop -d
(snip)

undefined method 'empty?' for nil
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/style/copyright.rb:61:
in 'RuboCop::Cop::Style::Copyright#verify_autocorrect_notice!'
```
  • Loading branch information
koic authored and bbatsov committed May 31, 2024
1 parent a920ba5 commit 80f619b
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/fix_an_error_for_style_copyright.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12951](https://github.com/rubocop/rubocop/pull/12951): Fix an error for `Style/Copyright` when `AutocorrectNotice` is missing. ([@koic][])
4 changes: 3 additions & 1 deletion lib/rubocop/cop/style/copyright.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ def autocorrect_notice
end

def verify_autocorrect_notice!
raise Warning, "#{cop_name}: #{AUTOCORRECT_EMPTY_WARNING}" if autocorrect_notice.empty?
if autocorrect_notice.nil? || autocorrect_notice.empty?
raise Warning, "#{cop_name}: #{AUTOCORRECT_EMPTY_WARNING}"
end

regex = Regexp.new(notice)
return if autocorrect_notice.gsub(/^# */, '').match?(regex)
Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/cop/style/copyright_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class Foo
end

it 'fails to autocorrect if no AutocorrectNotice is given' do
# cop_config['AutocorrectNotice'] = '# Copyleft (c) 2015 Acme Inc.'
cop_config['AutocorrectNotice'] = nil
expect { expect_offense(source) }.to raise_error(RuboCop::Warning, %r{Style/Copyright:})
end
end
Expand Down

0 comments on commit 80f619b

Please sign in to comment.