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 "check used in conditional" false positive #120

Closed
jas14 opened this issue Aug 27, 2019 · 1 comment · Fixed by #121
Closed

Rails/SaveBang "check used in conditional" false positive #120

jas14 opened this issue Aug 27, 2019 · 1 comment · Fixed by #121
Labels
bug Something isn't working

Comments

@jas14
Copy link
Contributor

jas14 commented Aug 27, 2019

The Rails/SaveBang cop in v0.74.0 emits a false positive for the "check used in conditional" rule, where the save-like call is actually in the if-body, not the condition.


Expected behavior

I expect Rails/SaveBang to suggest I use create! instead of create, like so:

test.rb:1:12: C: Rails/SaveBang: Use create! instead of create if the return value is not checked.
some_model.create if condition
           ^^^^^^

Actual behavior

I get a message about the save-like call being used as a conditional:

test.rb:1:12: C: Rails/SaveBang: create returns a model which is always truthy.
some_model.create if condition
           ^^^^^^

Steps to reproduce the problem

  1. Create a file (I named it test.rb) with the following minimum reproducible case code:
some_model.create if condition
  1. Execute rubocop test.rb
  2. Observe incorrect error message

RuboCop version

$ bundle exec rubocop -V
0.74.0 (using Parser 2.6.3.0, running on ruby 2.6.3 x86_64-darwin18)
@Drenmi Drenmi added the bug Something isn't working label Aug 28, 2019
jas14 added a commit to jas14/rubocop-rails that referenced this issue Aug 28, 2019
Fixes rubocop#120.

The Rails/SaveBang rule emitted incorrect errors about using the return
value of save-like calls in conditional nodes. It did not differentiate
between such calls in the body versus the condition, where only the
latter is necessarily a boolean expression.
jas14 added a commit to jas14/rubocop-rails that referenced this issue Aug 28, 2019
Fixes rubocop#120.

The Rails/SaveBang rule emitted incorrect errors about using the return
value of save-like calls in conditional nodes. It did not differentiate
between such calls in the body versus the condition, where only the
latter is necessarily a boolean expression.
jas14 added a commit to jas14/rubocop-rails that referenced this issue Aug 28, 2019
Fixes rubocop#120.

The Rails/SaveBang rule emitted incorrect errors about using the return
value of save-like calls in conditional nodes. It did not differentiate
between such calls in the body versus the condition, where only the
latter is necessarily a boolean expression.
@jas14
Copy link
Contributor Author

jas14 commented Aug 29, 2019

FYI this applies to save-like calls in any branch body of a conditional. Examples:

if condition
  model.create
        ^^^^^^
elsif
  model.create
        ^^^^^^
else
  model.create
        ^^^^^^
end

jas14 added a commit to jas14/rubocop-rails that referenced this issue Sep 11, 2019
Fixes rubocop#120.

The Rails/SaveBang rule emitted incorrect errors about using the return
value of save-like calls in conditional nodes. It did not differentiate
between such calls in the body versus the condition, where only the
latter is necessarily a boolean expression.
jas14 added a commit to jas14/rubocop-rails that referenced this issue Sep 13, 2019
Fixes rubocop#120.

The Rails/SaveBang rule emitted incorrect errors about using the return
value of save-like calls in conditional nodes. It did not differentiate
between such calls in the body versus the condition, where only the
latter is necessarily a boolean expression.
jas14 added a commit to jas14/rubocop-rails that referenced this issue Sep 16, 2019
Fixes rubocop#120.

The Rails/SaveBang rule emitted incorrect errors about using the return
value of save-like calls in conditional nodes. It did not differentiate
between such calls in the body versus the condition, where only the
latter is necessarily a boolean expression.
@koic koic closed this as completed in #121 Sep 17, 2019
koic added a commit that referenced this issue Sep 17, 2019
[Fix #120] Fix save-in-conditional checking in `Rails/SaveBang`
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

Successfully merging a pull request may close this issue.

2 participants