Rubocop: Bug of Style/NumericPredicate

Created on 1 Jan 2017  Â·  5Comments  Â·  Source: rubocop-hq/rubocop

String#index can return an instance of NilClass, so the rubocop output is not valid.


Expected behavior

Rubocop should not treat the comparison expression as invalid.

Actual behavior

Rubocop treats the comparison expression as invalid.

Steps to reproduce the problem

Rubocop output for source code as follows is inappropriate.

'a'.index(/b/) == 0
#=> C: Use 'a'.index(/b/).zero? instead of 'a'.index(/b/) == 0.

RuboCop version

local [-_-] : ~/workspace/private/ruby/rubocop-bug [master] âš¡  be rubocop -v                                                                                                                      [2.3.1]
0.46.0
bug

Most helpful comment

A possible approach could be to have an exclude list, and ignore comparisons to that method call. This would need to be configurable, though, as it could cause false negatives for some code bases.

Yeah, that's an obvious approach, but we typically employ it when a small number of methods would be problematic and here the potential number of methods that can return nil is huge. :-)

All 5 comments

Btw, that's a problem with every expression that could return nil on the left side. I imagine in practice this is not a big issue as few people have complained about this so far, but there's not way to make this reliable and useful at the same time... At least I can't think of such a way...

(I guess I'd just use the safe navigation operator here)

A possible approach could be to have an exclude list, and ignore comparisons to that method call. This would need to be configurable, though, as it could cause false negatives for some code bases. False positives are unavoidable, and we have a lot of mechanisms to deal with them. False negatives, on the other hand, are very bad, as there are no ways of addressing them as an end user, even if you became aware of them.

A possible approach could be to have an exclude list, and ignore comparisons to that method call. This would need to be configurable, though, as it could cause false negatives for some code bases.

Yeah, that's an obvious approach, but we typically employ it when a small number of methods would be problematic and here the potential number of methods that can return nil is huge. :-)

The cop will be marked unsafe in #5978 so I'm closing this issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

benoittgt picture benoittgt  Â·  3Comments

Aqualon picture Aqualon  Â·  3Comments

millisami picture millisami  Â·  3Comments

bquorning picture bquorning  Â·  3Comments

bbugh picture bbugh  Â·  3Comments