-
-
Notifications
You must be signed in to change notification settings - Fork 277
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
Mark RSpec/RedundantPredicateMatcher
as Safe: false
#1772
Mark RSpec/RedundantPredicateMatcher
as Safe: false
#1772
Conversation
c7b61f9
to
7690c34
Compare
`RSpec/RedundantPredicateMatcher` cop is marked as unsafe because false positives occur when `be_start_with` is used as a legitimate predicate matcher for `start_with?` method. e.g., rubocop/rubocop-ast@f9e6682
7690c34
to
e6b9bfc
Compare
We’ve had this discussion recently with @bquorning and I still think that we should not mark cops as unsafe for any period of time for a reason of a bug causing incorrect inspection or autocorrection. See #1687. I don’t think we’ll ever fix those issues by muting them. |
Maybe I misunderstood the issue. But we have a spec for correcting If you don’t want |
The problem as I understand it is that rspec-expectations’ start_with/end_with matcher won’t work for ProcessedSource custom implementation of start_with?/end_with? Why doesn’t the ProcessedSource’s implementation work with the regular matcher? |
The regular matcher seems to not call def [](*args)
lines[*args]
end
def start_with?(string)
return false if self[0].nil?
self[0].start_with?(string)
end It seems that And yes, we should probably consider if other of the I would suggest changing the specs to e.g. |
Speaking in defence of the RSpec matchers in wuestion, they support types beyond just strings. The decision to (manually) delegate the start_with? to the first line is justified. Even though it may be surprising if used with a multi-line string/regexp. There’s yet another thing that makes me suspicious regarding |
I was coming across another unsafe autocorrection of this cop, typically in the following case: class BusinessRule < ApplicationRecord
def match?(business_object)
# check if the rule is applicable to the object
end
end
describe BusinessRule do
describe '#matches?' do
context 'when the object is relevant' do
it 'returns true' do
expect(business_rule).to be_match(business_object)
end
end
end
end which gets wrongly autocorrected. Since people may define any kind of method like |
@pirj could you please elaborate a bit on the decision to close this while there seem to be known unsafe cases? This one seems definitely unsafe by design and not because of a bug or improper handling of edge-cases. |
We can trick cops into suggesting contextually undafe corrections by using the near-infinite flexibility of Ruby. I admit that I am biased against the example in question, and the code looks sub-par. Also marking cops unsafe is my pet peeve. |
I admit I have better things to apply my time than to start a RuboCop-wide discussion on the unsafeness topic and to refactor rubocop-ast. I wish someone did that for me. In the meanwhile I’ll try to defend the RSpec department from being disabled one by one. I hope noone will think I’m challenging them 😄 |
Agreed that you can probably make every cop unsafe if you try to figure out the specific counter-example that will break things.
But we're several people having ran into that without trying at all
I gladly admit that the advantage with invalid autocorrections of RSpec cops is that the tests break instead of production code 😅 |
This happens with existing code. I would prefer fixing the implementation of ProcessedSource.
Good point. Sometime tests may become pass-all deceptive, though. |
RSpec/RedundantPredicateMatcher
cop is marked as unsafe because false positives occur whenbe_start_with
is used as a legitimate predicate matcher forstart_with?
method.e.g., rubocop/rubocop-ast@f9e6682
Before submitting the PR make sure the following are checked:
master
(if not - rebase it).CHANGELOG.md
if the new code introduces user-observable changes.bundle exec rake
) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).If you have created a new cop:
config/default.yml
.Enabled: pending
inconfig/default.yml
.Enabled: true
in.rubocop.yml
.VersionAdded: "<<next>>"
indefault/config.yml
.If you have modified an existing cop's configuration options:
VersionChanged: "<<next>>"
inconfig/default.yml
.