pocke / rubocop-typed

RuboCop with type
MIT License
17 stars 1 forks source link

Cop ideas #2

Open pocke opened 4 years ago

pocke commented 4 years ago

Feel free to comment cop ideas with type.


I already search the following sources to find cop ideas.

koic commented 4 years ago

This comment is inspired by the following commit in the rails/rails repository. https://github.com/rails/rails/commit/051e3490412d6136cb8ab0abe0f1b7c2236f4a8b

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 commented 4 years ago

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!

yahonda commented 4 years ago

Let me follow up https://github.com/rubocop-hq/rubocop-minitest/issues/68#issuecomment-599200113 here.

_ method is only defined by Minitest::Spec::DSL::InstanceMethods but rubocop-minitest is not able to handle the class of this method. I'm wondering if RuboCop knows the type it can skip auto-corrections.

cc @koic

koic commented 4 years ago

I think Style/HashEachMethods cop can be strictly detected depending on the receiver type. https://github.com/rubocop-hq/rubocop/pull/7832

values.each { |k, v| do_something(k, v) }
koic commented 4 years ago

It would be useful to be able to know a full qualified class name that omit the namespace as follows:

module Gem
  def self.foo
    raise Exception
  end
end

Gem.foo #=> Gem::Exception

cf. rubocop-hq/rubocop#7846

koic commented 4 years ago

Performance/AncestorsInclude cop. https://github.com/rubocop-hq/rubocop-performance/pull/149

koic commented 4 years ago

It seems possible to detect without false positive by using receiver type (AR mode or not). https://github.com/rubocop-hq/rubocop-rails/issues/149

yahonda commented 3 years ago

I'm wondering if warnings via ruby/ruby#4070 can be detected by RuboCop or RuboCop with type.

# frozen_string_literal: true

# bad
Struct.new(:a).new(a: 1)

foo = Struct.new(:x)
foo.new(x: 1)

# good
Struct.new(:b).new({ b: 1 })

bar = Struct.new(:y)
bar.new({ y: 1 })
koic commented 3 years ago

Type information is required to determine whether the receiver has to_time method. https://github.com/rubocop/rubocop-rails/issues/288