Open pocke opened 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')
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!
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
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) }
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
Performance/AncestorsInclude
cop.
https://github.com/rubocop-hq/rubocop-performance/pull/149
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
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 })
Type information is required to determine whether the receiver has to_time
method.
https://github.com/rubocop/rubocop-rails/issues/288
Feel free to comment cop ideas with type.
AR::Base.where
is not allowed in viewafter_update :foo, if: -> { 1 + 1 }
bool
type, but1 + 1
is always truthy.find_by
with bang or conditional https://github.com/rubocop-hq/rubocop-rails/issues/167find
vsdetect
,map
vscollect
, etc...String#first
and comparison withString#start_with?
.String#frist
is added by ActiveSupportAR::Relation#pluck
vsAR::Relation#select
for sub-queryrel.where(id: rel2.pluck(:id))
pluck
withselect
, but it depends on context.send
with public method namefoo.send(:foo_public_method)
should befoo.foo_public_method
str[/substr/]
should bestr['substr']
Style/HashTransformValues
with not-hash iterationModel.pluck(:id, :val).map { |id, val| [id, foo(val)] }.to_h
I already search the following sources to find cop ideas.