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

item.blank? is not a correct replacement for item.nil? || item.empty? #444

Closed
mvz opened this issue Mar 4, 2021 · 1 comment
Closed

Comments

@mvz
Copy link
Contributor

mvz commented Mar 4, 2021

The Rails/Blank cop autocorrects item.nil? || item.empty? to item.blank?, even with the 'safe' autocorrect mode (i.e., rubocop -a). However, a string consisting of just spaces is considered blank but not empty by Rails, which means this autocorrect causes a change in behavior.

Expected behavior

Given a file in Rails containing the line

  item.nil? || item.empty?

When running rubocop -a, RuboCop should not autocorrect this to item.blank?.

Actual behavior

RuboCorrect the code to item.blank?

Steps to reproduce the problem

This is extremely important! Providing us with a reliable way to reproduce
a problem will expedite its solution.

  • Create a file foo.rb containing:
# frozen_string_literal: true

def foo(item)
  item.nil? || item.empty?
end
  • Create a file .rubocop.yml containing:
require:
  - rubocop-rails

AllCops:
  NewCops: enable

Rails:
  Enabled: true

Run rubocop -a. The file foo.rb will have been changed as described above.

RuboCop version

$ rubocop -V
1.11.0 (using Parser 3.0.0.0, rubocop-ast 1.4.1, running on ruby 3.0.0 x86_64-linux)
  - rubocop-rails 2.9.1
@andyw8
Copy link
Contributor

andyw8 commented Mar 5, 2021

koic added a commit to koic/rubocop-rails that referenced this issue Mar 8, 2021
Fixes #rubocop#444.

This PR marks `Rails/Blank` as unsafe auto-correction because `' '.empty?` returns false,
but `' '.blank?` returns true. Therefore, auto-correction is not compatible if receiver is
a non-empty blank string, tab, or newline meta characters.
@koic koic closed this as completed in 3a7fafc Mar 10, 2021
koic added a commit that referenced this issue Mar 10, 2021
…rection

[Fix #444] Mark `Rails/Blank` as unsafe auto-correction
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

No branches or pull requests

2 participants