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 partial autocorrect support for Lint/LiteralAsCondition #13117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Zopolis4
Copy link
Contributor

@Zopolis4 Zopolis4 commented Aug 14, 2024

Makes progress on #12840.

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.

@Zopolis4
Copy link
Contributor Author

Tests do not cover the post-loop while case as I was unable to prevent it from also checking post-loop until's, which do not autocorrect as all the literals tested by the spec are truthy literals.

Copy link
Contributor

@Earlopain Earlopain left a comment

Choose a reason for hiding this comment

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

I believe it makes sense to mark this one as Autocorrect: contextual in order to prevent autosave in editors removing the code. I do this relativly often while testing things and I don't want things to shift around during that.

@Zopolis4
Copy link
Contributor Author

Good point-- I've added that.

@Zopolis4
Copy link
Contributor Author

The current autocorrection for code which has a redundant conditional check is just to remove the check-- right now nothing is being done to code which has a conditional check that will never pass (i.e. if false, unless true).

Thinking about it, that is unreachable code, and so the autocorrection would be just to delete the code entirely. Or maybe it should fall under the purview of Lint/UnreachableCode?

@bbatsov
Copy link
Collaborator

bbatsov commented Aug 31, 2024

Thinking about it, that is unreachable code, and so the autocorrection would be just to delete the code entirely. Or maybe it should fall under the purview of Lint/UnreachableCode?

I'd say - Lint/UnreachableCode. It's good to keep cop focus relatively narrow.

@Zopolis4
Copy link
Contributor Author

Zopolis4 commented Sep 3, 2024

Ok, I'll handle that cop in a second PR.

@Zopolis4
Copy link
Contributor Author

Although, implementing this further, it does seem to make more sense to have the autocorrect logic in the same place-- right now I'm just raising an offense to maintain behaviour, but the testing and the logic setup are much better suited here than they are in Lint/UnreachableCode, even if the goals of the cops line up a little less.

As it stands I'll keep things as they are, but unless anyone has strong objections I'll probably add autocorrect for if false and the like in a few days here.

@Zopolis4 Zopolis4 force-pushed the 1500 branch 2 times, most recently from ab2cabc to 21cc43e Compare October 16, 2024 10:48
@Zopolis4 Zopolis4 force-pushed the 1500 branch 5 times, most recently from 6e04a68 to 89f0ad6 Compare November 1, 2024 12:04
@Zopolis4 Zopolis4 marked this pull request as ready for review November 1, 2024 12:12
Comment on lines 4 to +16
%w(1 2.0 [1] {} :sym :"#{a}").each do |lit|
it "registers an offense for literal #{lit} in if" do
it "registers an offense for truthy literal #{lit} in if" do
expect_offense(<<~RUBY, lit: lit)
if %{lit}
^{lit} Literal `#{lit}` appeared as a condition.
top
end
RUBY

expect_correction(<<~RUBY)
top
RUBY
end
Copy link
Member

@dvandersluis dvandersluis Dec 11, 2024

Choose a reason for hiding this comment

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

I think that "#{a}" should not be included in the correctable cases because unlike the other literals, removing it will potentially stop calling a method which could have side effects.

Comment on lines +87 to +91
expect_correction(<<~RUBY)
while true
top
end
RUBY
Copy link
Member

@dvandersluis dvandersluis Dec 11, 2024

Choose a reason for hiding this comment

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

Should the form of loop change here? I think that maybe should be the domain of a separate Style cop.

ie. I think this should remain as a post-while loop:

begin
  top
end while true

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.

5 participants