Skip to content

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

Closed
@mvz

Description

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions