-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
ZeroLengthPredicate: gives false warning on File.stat #2841
Comments
Thanks for the good example. Do you have any ideas how to make the cop smarter? |
Good question. I looked a bit at the code in https://github.com/bbatsov/rubocop/blob/master/lib/rubocop/cop/style/zero_length_predicate.rb and it's obviously a whole lot more work to do it "properly" here, i.e. trying to check types/etc. A simple change for now would be to remove the lint for |
You can follow some of the discussion that preceded the implementation of the cop in this issue. Because of the nature of dynamically typed, object oriented languages, and the use of duck typing in Ruby, it is impossible to avoid false positives. Attempting to do things like type checking (except for literals) would be outside the scope of what's possible with static code analysis. If I correctly understand the intention of @bbatsov, Rubocop is meant to be tailored to the Ruby standard library, and false positives should be handled through disabling the cop, either inline for a certain piece of code, or altogether. (Feel free to correct me on this. 😄) Within those constraints, however, the cops should obviously be as "smart" as possible, as to minimize the number of duds. 😄 |
Absolutely. If these cops prod someone to make their library follow the conventions of stdlib more closely, that's not a bad thing either. |
So do we want to stop checking for |
I'm fine with making this configurable, but I'm not fine with checking only for |
I'm also hitting this with checking the size of an uploaded file (where
|
|
It seems like a lot to ask, but it would be great if these classes implemented |
@mikegee You can try to contribute patches to MRI Ruby. They accept PRs through GitHub now. |
Rubocop prefers `empty?` over `length == 0` and `size == 0`, which is great for String, Array, Hash, etc. It would be nice if more classes implemented `#empty?` for consistancy. See related discussion at rubocop/rubocop#2841
Rubocop prefers `empty?` over `length == 0` and `size == 0`, which is great for String, Array, Hash, etc. It would be nice if more classes implemented `#empty?` for consistancy. See related discussion at rubocop/rubocop#2841
Rubocop prefers `empty?` over `length == 0` and `size == 0`, which is great for String, Array, Hash, etc. It would be nice if more classes implemented `#empty?` for consistancy. See related discussion at rubocop/rubocop#2841
I’m doing a bad job arguing for |
I've added a few comments myself, but I'm not holding my breath. Generally for this specific problem it seems to me we should discourage people from using |
Something having a |
Some collection like classes in the Ruby standard library implement `#size` but not `#empty`. This change has the cop ignore known cases with `Tempfile`, `StringIO`, and `File::Stat` objects to reduce false positives.
To get somewhere with this, I created a patch to reduce known false positives that were reported in this thread. I think that's about the extent to what we can go with static analysis. For those who disagree with this cop in general, there's always the option to disable it. 🙂 |
The
ZeroLengthPredicate
is a bit too aggressive in its heuristics. The code below gives a warning:The warning is this:
This is a problem since the
File::Stat
class doesn't even have anempty?
method defined. 😄I can of course easily disable this on the line in question. This issue exists to remind us that the detection code in this cop should be made a bit smarter (if possible), to avoid treating this line as a Rubocop offense.
The text was updated successfully, but these errors were encountered: