Skip to content
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

Add iterator? to deprecated methods and prefer block_given? #6714

Merged

Conversation

tejasbubane
Copy link
Contributor

Closes #6688

  • Wrote [good commit messages][1].
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

Copy link
Contributor

@rrosenblum rrosenblum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks good to me. I think we should add autocorrect support to the cop. I'm not a big fan of the name KernelIteratorPredicate. I don't think most people realize that iterator? even exists.

CHANGELOG.md Outdated Show resolved Hide resolved
@Drenmi
Copy link
Collaborator

Drenmi commented Jan 29, 2019

The code looks good to me. I think we should add autocorrect support to the cop.

Would you be open to adding auto-correct @tejasbubane? It involves defining an #autocorrect method on the cop. Something like this should do the trick:

def autocorrect(node)
  lambda do |corrector|
    corrector.replace(node.source_range, 'block_given?')
  end
end

I haven't tested it though. 🙂

@tejasbubane
Copy link
Contributor Author

@Drenmi yes I will add autocorrect and make all the changes today. Thanks!

@bbatsov
Copy link
Collaborator

bbatsov commented Jan 29, 2019

I don’t think this warrants a dedicated cop. Probably it should be part of some generic cop about preferred methods. I know we have some offenders in the current codebase, but we shouldn’t grow that list.

@tejasbubane
Copy link
Contributor Author

tejasbubane commented Jan 30, 2019

I don’t think this warrants a dedicated cop. Probably it should be part of some generic cop about preferred methods. I know we have some offenders in the current codebase, but we shouldn’t grow that list.

Which cop do you think this should go in? Create a new one called PreferredMethods?

@bquorning
Copy link
Contributor

We have an existing cop called DeprecatedClassMethods where this code could go.

@rrosenblum
Copy link
Contributor

DeprecatedClassMethods will need to be refactored for this to work. That cop was specifically designed to handle class methods so it looks for both the receiver and the method. iterator? has a nil receiver.

@tejasbubane tejasbubane force-pushed the new-cop-kernal-iterator-predicate branch from cd8affc to c9cf7f2 Compare February 21, 2019 10:42
@tejasbubane
Copy link
Contributor Author

I have made the changes to use DeprecatedClassMethods instead of adding a new cop.

@tejasbubane tejasbubane force-pushed the new-cop-kernal-iterator-predicate branch from c9cf7f2 to 67ca438 Compare February 21, 2019 12:38
@tejasbubane tejasbubane changed the title Add new Lint/KernalIteratorPredicate cop Add iterator? to deprecated methods and prefer block_given? Feb 21, 2019
@tejasbubane tejasbubane force-pushed the new-cop-kernal-iterator-predicate branch 2 times, most recently from 082cf25 to 4d66c8b Compare February 21, 2019 14:34
@tejasbubane tejasbubane force-pushed the new-cop-kernal-iterator-predicate branch from 4d66c8b to 841f095 Compare February 21, 2019 14:45
Copy link
Contributor

@bquorning bquorning left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you 🙏

@Drenmi
Copy link
Collaborator

Drenmi commented Feb 22, 2019

Looks good @tejasbubane! I'm ready to accept the code as is, but it would be cool, since it's a Kernel method, to only register an offense when there is no receiver. I.e. foo.iterator? shouldn't be an offense.

Maybe the code already does this, but the unit test seems to be missing. 🙂

@tejasbubane tejasbubane force-pushed the new-cop-kernal-iterator-predicate branch from 841f095 to 5c957cd Compare February 22, 2019 14:41
@tejasbubane
Copy link
Contributor Author

Maybe the code already does this, but the unit test seems to be missing. 🙂

Yes the code already checked for nil receiver. I added the test case.

@dgollahon
Copy link
Contributor

This is less about this specific change and might require further discussion outside of this PR, but I noticed that this cop is marked as safe. Any time a method selector is changed this is not safe (as I mean it: known to be semantically equivalent). On something like File.exists? it's still not technically safe, but there the odds of a semantic change are extremely low so that concerns me a bit less. In this particular case it seems rather unlikely that someone would define #iterator?, but it's definitely more likely than redefining File.exists? and certainly not provably safe.

Maybe it's good to define safe as "safeish" but I, personally, would prefer to be able to trust that safe is truly safe under all circumstances. Perhaps we need a distinction between cops with (in theory) provably safe autocorrects (maybe even assert the AST is unchanged?) v.s. things that almost always do the right thing, even if there are edge cases that rubocop can't reasonably detect via static analysis (for example, the #iterator? -> #block_given?).

Has someone defined a specific policy for what counts as safe anywhere?

@Drenmi Drenmi merged commit 9ae94da into rubocop:master Feb 25, 2019
@tejasbubane tejasbubane deleted the new-cop-kernal-iterator-predicate branch February 25, 2019 05:04
@Drenmi
Copy link
Collaborator

Drenmi commented Feb 25, 2019

Has someone defined a specific policy for what counts as safe anywhere?

You're asking good questions, @dgollahon. We don't have a formal definition of this at the moment. It was also relatively recently we started marking cops as unsafe, so I haven't given this a lot of thought. On first thought, I also prefer that "safe" is truly safe, although it could turn out (I don't have a good feel of this) that it would disqualify a very large subset of cops, which might be the wrong trade-off. 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants