Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change AssertEmptyLiteral cop to check for assert_equal #118

Merged
merged 1 commit into from
Feb 21, 2021

Conversation

cstyles
Copy link
Contributor

@cstyles cstyles commented Feb 21, 2021

While this cop is correct that we should be using assert_empty(list)
over assert([], list), it's right for the wrong reasons. That example
is bad because it's using assert and not assert_equal. An empty
list/hash is truthy so the assertion will always succeed. That is,
assert([], [1, 2, 3]) will succeed even though at a glance it looks
like it shouldn't. I believe that what this cop is intending to check
for is assert_equal([], list) instead so I've updated the references
to assert to assert_equal.

Issue #117 proposes that we try to detect when assert is used where
the user likely wanted assert_equal so while this cop will no longer
register a violation for assert([], list), if that proposal is
accepted then a future cop will register a violation.

edit: #120 adds such a cop.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

@andyw8
Copy link
Contributor

andyw8 commented Feb 21, 2021

cc @tejasbubane who authored this in #38

@koic
Copy link
Member

koic commented Feb 21, 2021

This change will be complemented by #120 and will be acceptable.

CHANGELOG.md Outdated
@@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#118](https://github.com/rubocop-hq/rubocop-minitest/pull/118): Fix `Minitest/AssertEmptyLiteral` by making it check for `assert_equal([], array)` instead of `assert([], array)`. ([@cstyles][])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change. Can you mark **(Breaking)** and squash your commits into one?

Suggested change
* [#118](https://github.com/rubocop-hq/rubocop-minitest/pull/118): Fix `Minitest/AssertEmptyLiteral` by making it check for `assert_equal([], array)` instead of `assert([], array)`. ([@cstyles][])
* [#118](https://github.com/rubocop-hq/rubocop-minitest/pull/118): **(Breaking)** Fix `Minitest/AssertEmptyLiteral` by making it check for `assert_equal([], array)` instead of `assert([], array)`. ([@cstyles][])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 70f02e0

While this cop is correct that we should be using `assert_empty(list)`
over `assert([], list)`, it's right for the wrong reasons. That example
is bad because it's using `assert` and not `assert_equal`. An empty
list/hash is truthy so the assertion will always succeed. That is,
`assert([], [1, 2, 3])` will succeed even though at a glance it looks
like it shouldn't. I believe that what this cop is intending to check
for is `assert_equal([], list)` instead so I've updated the references
to `assert` to `assert_equal`.

Issue rubocop#117 proposes that we try to detect when `assert` is used where
the user likely wanted `assert_equal` so while this cop will no longer
register a violation for `assert([], list)`, if that proposal is
accepted then a future cop *will* register a violation.
@koic koic merged commit 8c3a670 into rubocop:master Feb 21, 2021
@koic
Copy link
Member

koic commented Feb 21, 2021

Nicely done!

@cstyles cstyles deleted the fix-assert-empty-literal branch February 21, 2021 17:13
koic added a commit that referenced this pull request Feb 21, 2021
koic added a commit to koic/rubocop-minitest that referenced this pull request Oct 2, 2021
This PR marks `Minitest/AssertEmptyLiteral` as safe auto-correction.
Introduced as unsafe auto-correction in rubocop#85, but now safe auto-correction
with changes in rubocop#118.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants