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

Add new Lint/LiteralAssignmentInCondition cop #12420

Conversation

koic
Copy link
Member

@koic koic commented Nov 29, 2023

Summary

This PR adds a new Lint/LiteralAssignmentInCondition cop, which checks for literal assignments in the conditions of if, while, and until. It emulates the following Ruby warning:

$ ruby -we 'if x = true; end'
-e:1: warning: found `= literal' in conditional, should be ==

As a lint cop, it cannot be determined if == is appropriate as intended, therefore this cop does not provide autocorrection.

# bad
if x = 42
  do_something
end

# good
if x == 42
  do_something
end

# good
if x = y
  do_something
end

Literal assignment used within a condition always triggers a Ruby warning, regardless of parentheses. This PR prevents the Ruby warning from being overlooked due to the ruby -w option.

Additional Information

Here, the differences between similar cops, Lint/LiteralAssignmentInCondition cop and Lint/LiteralAsCondition cop are discussed.

Lint/LiteralAssignmentInCondition cop

A similar Lint/LiteralAssignmentInCondition cop already exists, but as mentioned above, literal assignments are Ruby's warned about regardless of the presence of parentheses:

$ ruby -we 'if (x = 42); end'
-e:1: warning: found `= literal' in conditional, should be ==

Therefore, a new cop has been created separate from Lint/LiteralAssignmentInCondition.

Furthermore, as documented for Lint/LiteralAssignmentInCondition cop, literal assignments are never considered good case, so = true has been replaced with = value.

Lint/LiteralAsCondition cop

Additionally, the existing Lint/LiteralAsCondition cop addresses a slightly different Ruby warning:

$ ruby -we 'if 42; end'
-e:1: warning: literal in condition

This is another reason for choosing to implement a new cop.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@koic koic force-pushed the add_new_lint_literal_assignment_in_condition_cop branch 3 times, most recently from a8a04da to ffc37f3 Compare November 29, 2023 03:36
## Summary

This PR adds a new `Lint/LiteralAssignmentInCondition` cop, which checks for
literal assignments in the conditions of `if`, `while`, and `until`.
It emulates the following Ruby warning:

```console
$ ruby -we 'if x = true; end'
-e:1: warning: found `= literal' in conditional, should be ==
```

As a lint cop, it cannot be determined if `==` is appropriate as intended,
therefore this cop does not provide autocorrection.

```ruby
# bad
if x = 42
  do_something
end

# good
if x == 42
  do_something
end

# good
if x = y
  do_something
end
```

Literal assignment used within a condition always triggers a Ruby warning, regardless of parentheses.
This PR prevents the Ruby warning from being overlooked due to the `ruby -w` option.

## Additional Information

Here, the differences between similar cops, `Lint/LiteralAssignmentInCondition` cop and
`Lint/LiteralAsCondition` cop are discussed.

### `Lint/LiteralAssignmentInCondition` cop

A similar `Lint/LiteralAssignmentInCondition` cop already exists, but as mentioned above,
literal assignments are Ruby's warned about regardless of the presence of parentheses:

```ruby
$ ruby -we 'if (x = 42); end'
-e:1: warning: found `= literal' in conditional, should be ==
```

Therefore, a new cop has been created separate from `Lint/LiteralAssignmentInCondition`.

Furthermore, as documented for `Lint/LiteralAssignmentInCondition` cop, literal assignments
are never considered `good` case, so `= true` has been replaced with `= value`.

### `Lint/LiteralAsCondition` cop

Additionally, the existing `Lint/LiteralAsCondition` cop addresses a slightly different Ruby warning:

```ruby
$ ruby -we 'if 42; end'
-e:1: warning: literal in condition
```

This is another reason for choosing to implement a new cop.
@koic koic force-pushed the add_new_lint_literal_assignment_in_condition_cop branch from ffc37f3 to 0ca08b3 Compare November 29, 2023 08:46
@bbatsov bbatsov merged commit 19daa1c into rubocop:master Dec 1, 2023
29 checks passed
@bbatsov
Copy link
Collaborator

bbatsov commented Dec 1, 2023

Looks good to me. Thanks!

@koic koic deleted the add_new_lint_literal_assignment_in_condition_cop branch December 1, 2023 14:20
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