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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not omit parentheses for calls in assignments in conditional branches #10982

Merged
merged 1 commit into from Sep 16, 2022

Conversation

gsamokovarov
Copy link
Contributor

@gsamokovarov gsamokovarov commented Aug 30, 2022

Parentheses are required in assignments that call methods in conditional branches. Removing them is a syntax error and the Style/MethodCallWithArgsParentheses cop with the style of omit_parentheses has been doing just that during autocorrection.

Here is an example that needs the parentheses but is improperly autocorrected:

if success = destroy_remote(server)
  delete server
end

Removing them, as the autocorrection does, is a syntax error:

if success = destroy_remote server # 馃挘
  delete server
end

Such is the case for any assignment or shorthand assignment in any Ruby conditional.

@@ -202,6 +203,17 @@ def assigned_before?(node, target)
def inside_string_interpolation?(node)
node.ancestors.drop_while { |a| !a.begin_type? }.any?(&:dstr_type?)
end

def assignment_in_condition?(node)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be better to implement this in terms of pattern matching, as logic that goes through 2 levels of parents is both brittle and hard to understand.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot use def_node_pattern here because of the way this cop is organized. This is a module that is mixed in into Style/MethodCallWithArgsParentheses. I can invoke NodePattern by itself, but I am yet to find a good pattern that looks for a grandparent.

I did had a redundant grandparent check, though. The new implementation is simpler.

@gsamokovarov gsamokovarov force-pushed the cond-assign-omit-parentheses branch 2 times, most recently from 501309f to f749701 Compare September 1, 2022 10:50
Parentheses are required in assignments that call methods in conditional
branches. Removing them is a syntax error and the
`Style/MethodCallWithArgsParentheses` cop with the style of
`omit_parentheses` has been doing just that.

Here is an example that needs the parentheses:

```ruby
if success = destroy_remote(server)
  delete server
end
```

Removing them is a syntax error:

```ruby
if success = destroy_remote server
  delete server
end
```

Such is the case for any assignment or shorthand assignment in any Ruby
conditional.
@bbatsov bbatsov merged commit 583c1ff into rubocop:master Sep 16, 2022
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

Successfully merging this pull request may close these issues.

None yet

2 participants