Skip to content

Rails/SaveBang does not account for control flow #53

Closed
@Malabarba

Description

Expected behavior

I expect the following code to be allowed by rubocop, because the value of the create method is (possibly) returned by the function, and AllowImplicitReturn is true by default.

def find_or_create(**opts)
  find(**opts) || create(**opts)
end

Actual behavior

The Rails/SaveBang cop reports that I should use create! instead of create.

my_model.rb:3:21: C: Rails/SaveBang: create returns a model which is always truthy.
    find(**opts) || create(**opts)
                    ^^^^^^

Steps to reproduce the problem

Assuming you have Rails cops enabled. Save the following file and run rubocop filename.rb.

class MyModel < ActiveRecord::Model
  def find_or_create(**opts)
    find(**opts) || create(**opts)
  end
end

RuboCop version

0.68.1 (using Parser 2.6.3.0, running on ruby 2.6.1 x86_64-linux)

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

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions