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

Rails/Savebang fails when persisted? is checked on a broader expression #1230

Closed
searls opened this issue Jan 14, 2024 · 0 comments
Closed
Labels
bug Something isn't working

Comments

@searls
Copy link

searls commented Jan 14, 2024

Expected behavior

I'd expect this branch to be allowed by Rails/Savebang, wherein I assign the created post in a conditional and branch on persisted?

  def create(params, user)
    if (post = Post.create(attrs_for(params, user))).persisted?
      Result.new(post, true)
    else
      Result.new(post, false, post.errors.full_messages)
    end
  end

Actual behavior

The above will raise: Use create!instead ofcreateif the return value is not checked. Or checkpersisted?on model returned fromcreate``

RuboCop version

$ [bundle exec] rubocop -V
1.59.0 (using Parser 3.2.2.4, rubocop-ast 1.30.0, running on ruby 3.2.1) [arm64-darwin22]

Also using rubocop-rails 2.23.1

@koic koic added the bug Something isn't working label Jan 27, 2024
@koic koic closed this as completed in c568fa8 Mar 24, 2024
koic added a commit that referenced this issue Mar 24, 2024
…-bang

[Fix #1230] Fix a false positive for `Rails/SaveBang` if `persisted?` is checked on parenthesised expression.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants