String#index can return an instance of NilClass, so the rubocop output is not valid.
Rubocop should not treat the comparison expression as invalid.
Rubocop treats the comparison expression as invalid.
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.
local [-_-] : ~/workspace/private/ruby/rubocop-bug [master] âš¡ be rubocop -v [2.3.1]
0.46.0
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.
Most helpful comment
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. :-)