-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Enable Style/InvertibleUnlessCondition cop #15265
Enable Style/InvertibleUnlessCondition cop #15265
Conversation
else | ||
remaining_root_files << pn unless pn.basename.to_s == ".DS_Store" | ||
elsif pn.basename.to_s != ".DS_Store" | ||
remaining_root_files << pn |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an especially nice fix, imo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, else
... unless
was weird!
Currently failing CI due to upstream violations, will hold off on fixing until initial feedback is in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @dduugg, nice discovery. I have some (opinionated) comments about the corrections here.
Enabled: true | ||
InverseMethods: | ||
# Favor `if a != b` over `unless a == b` | ||
:==: :!= |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm very surprised this isn't a default.
@@ -526,7 +526,7 @@ def audit_livecheck_version | |||
def audit_livecheck_min_os | |||
return unless online? | |||
return unless cask.livecheckable? | |||
return unless cask.livecheck.strategy == :sparkle | |||
return if cask.livecheck.strategy != :sparkle |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mmm, not sure how I feel about this when there's mixed unless
and if
in a row.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no easy general fix here that i can think of. Sometimes I chain these (return unless a && b && c
), but i generally only use unless
when rubocop makes me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep it the way the cop wants it then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also prefer the cop's approach here.
Library/Homebrew/cmd/cleanup.rb
Outdated
@@ -57,7 +57,7 @@ def cleanup | |||
|
|||
cleanup.clean! | |||
|
|||
unless cleanup.disk_cleanup_size.zero? | |||
if cleanup.disk_cleanup_size.nonzero? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I prefer this how it was? (And below in all these .nonzero?
cases.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a reasonable objection, i'll unset the entry that does this and re-apply the cop
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
@@ -473,7 +473,7 @@ def table_output(category, days, results, os_version: false, cask_install: false | |||
"#{formatted_count} | #{formatted_percent}%" | |||
next if index > 10 | |||
end | |||
return unless results.length > 1 | |||
return if results.length <= 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I prefer unless
in all the cases where otherwise we have to replace >
or <
with the equals variant, the "unless" is easier to read (!) than "which way is that angle bracket going paired with that equals".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not so for me (mostly bc i still have to mentally convert unless
to if not
, which makes comparisons like this a 🤕), but we could also unset these in the config, like for the zero?
case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll let a third person weigh in and maybe we'll get a majority vote one way or the other.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also prefer the way the cop handles this.
else | ||
remaining_root_files << pn unless pn.basename.to_s == ".DS_Store" | ||
elsif pn.basename.to_s != ".DS_Store" | ||
remaining_root_files << pn |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, else
... unless
was weird!
f5d70e9
to
6a7e67d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love this cop, enabling it on personal projects too, thanks @dduugg!
As a general note: I have on my "one day" TODO list to go through literally every disabled by default cop and try to enable them all and remove the ones I disagree with. Here's a list from a work project I've done this on, if it's useful:
|
@MikeMcQuaid Don't tempt me to do your TODO list items for you. It's a good idea. |
6a7e67d
to
4801d7c
Compare
return if count <= 1 | ||
|
||
add_error "only a single postflight stanza is allowed" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return if count <= 1 | |
add_error "only a single postflight stanza is allowed" | |
add_error "only a single postflight stanza is allowed" if count > 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless RuboCop wants a guard-clause here.
Note that per comment on the rubocop PR, rubocop will be enabling fewer of these kinds of cops going forward. If you'd prefer to stay on top of these as they come in, it may be worth exploring the |
Good idea. I knew about |
As long as it's not |
I'm not too surprised by that. It might be more worthwhile to attempt it by department (e.g. |
4801d7c
to
bf8ac8a
Compare
brew style
with your changes locally?brew typecheck
with your changes locally?brew tests
with your changes locally?This is a new cop introduced in rubocop v1.44.0. It's disabled by default but i think it's enough of a readability improvement to enable. (I also added a rule to favor
if a != b
overunless a == b
.)It is invalid in at least one case – when comparing class hierarchies that do not share inheritance (see the disable below).
cc @issyl0