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

Cop ideas #2

Open
pocke opened this issue Jan 1, 2020 · 2 comments
Open

Cop ideas #2

pocke opened this issue Jan 1, 2020 · 2 comments

Comments

@pocke
Copy link
Owner

@pocke pocke commented Jan 1, 2020

Feel free to comment cop ideas with type.

  • Prefer find_each over each
    • We already have Rails/FindEach
  • Check method executes SQL usage in view.
    • AR::Base.where is not allowed in view
    • Detect method call that returns AR::Relation
  • Check bool type argument receives type that is always truthy or falsy.
    • e.g. after_update :foo, if: -> { 1 + 1 }
    • The if's block requires bool type, but 1 + 1 is always truthy.
  • Prefer Hash#dig over Hash#[]
  • Improve Rails/SaveBang
  • find_by with bang or conditional rubocop-hq/rubocop-rails#167
  • Detect unreachable code with bot type
  • Rails/DynamicFindBy without false positives
  • truthy type in conditional
  • Replacement of Style/BracesAroundHashParameters
    • ref: rubocop-hq/rubocop#7643
    • Maybe we can provide a cop for methods that receive both of keyword arguments and a hash.
  • Method aliases
    • find vs detect, map vs collect, etc...
  • Replace String#first and comparison with String#start_with?.
    • #2 (comment)
    • It is faster.
    • String#frist is added by ActiveSupport
  • AR::Relation#pluck vs AR::Relation#select for sub-query
    • rel.where(id: rel2.pluck(:id))
    • We may be able to reduce query with replacing pluck with select, but it depends on context.
  • send with public method name
    • foo.send(:foo_public_method) should be foo.foo_public_method
  • str[/substr/]should be str['substr']
    • Maybe typing is overkill?

I already search the following sources to find cop ideas.

@koic

This comment has been minimized.

Copy link
Contributor

@koic koic commented Feb 6, 2020

This comment is inspired by the following commit in the rails/rails repository.
rails/rails@051e349

Performance/StartWith cop would be extended with the following example if RuboCop can detect a receiver type is the String type.

# bad
receiver.first == 'str'

# good
receiver.start_with?('str')

https://github.com/rubocop-hq/rubocop-performance/blob/master/lib/rubocop/cop/performance/start_with.rb

@pocke

This comment has been minimized.

Copy link
Owner Author

@pocke pocke commented Feb 7, 2020

Thanks for your comment, @koic !

I'll add your idea to the checklist.

By the way, I guess your example code does not work well because String#first returns the first character, but it compares with 'str' in your example.
Probably it should be receiver.first == 's'.

And I confirmed String#first is added by ActiveSupport.

Thanks!

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

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.