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 doesn't work well with postfix if #1212

Open
hoshinotsuyoshi opened this issue Dec 12, 2023 · 1 comment
Open

Rails/SaveBang doesn't work well with postfix if #1212

hoshinotsuyoshi opened this issue Dec 12, 2023 · 1 comment

Comments

@hoshinotsuyoshi
Copy link
Contributor

Expected behavior

./x.rb :

def x
  instance.save if @flag
end

$ bundle exec rubocop x.rb --only Rails/SaveBang should be pass.

Actual behavior

./x.rb :

def x
  instance.save if @flag
end
$ bundle exec rubocop x.rb --only Rails/SaveBang
Inspecting 1 file
C

Offenses:

x.rb:2:12: C: [Correctable] Rails/SaveBang: Use save! instead of save if the return value is not checked.
  instance.save if @flag
           ^^^^

1 file inspected, 1 offense detected, 1 offense autocorrectable

note:

def x
  @flag && instance.save 
end

is ok.

Steps to reproduce the problem

see above

RuboCop version

Include the output of rubocop -V or bundle exec rubocop -V if using Bundler. Here's an example:
You can see extension cop versions (e.g. rubocop-rails, rubocop-performance, and others) output by rubocop -V,
include them as well. Here's an example:

$ bundle exec rubocop -V
1.58.0 (using Parser 3.2.2.4, rubocop-ast 1.30.0, running on ruby 3.2.2) [arm64-darwin22]
  - rubocop-rails 2.22.2
@vinita000
Copy link

vinita000 commented Dec 28, 2023

👍 Agreeing with the Proposed Solution: #1212 @hoshinotsuyoshi

I've reviewed the suggested solution:

def x
  @flag && instance.save 
end

I agree that this alternative is a suitable and clean approach. It addresses the reported issue and aligns with best practices. If no one has objections, I believe we can consider this as a valid resolution.

Looking forward to any further discussions or feedback from the community!

🚀 Thanks!

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