Skip to content

Performance/AncestorsInclude shows warning for instance methods #ancestors #159

@klyonrad

Description

@klyonrad

Calling a method with the name ancestors on an object raises the rubocop-performance warning.

Is this possible to detect more correctly with static code analysis or are we out of luck? If not, honestly I would question the usefulness of this cop because I don't think we are the only ones to happen to have data models where something is called ancestors.


Expected behavior

rubocop-performance only warns when .ancestors is called on a Class.
Calling of the instance method should be fine

Actual behavior

It seems to screem at any calling of something called .ancestors

Steps to reproduce the problem

We have a rails model with the following association declaration

has_many :ancestors

Somewhere we have a test where usage this method - on the instance.

expect(object_one.ancestors.include?(object_two)).to eq(true)

rubocop warns

Performance/AncestorsInclude: Use <= instead of ancestors.include?

RuboCop version

$ [bundle exec] rubocop -V
0.83.0 (using Parser 2.7.1.4, running on ruby 2.6.6 x86_64-darwin19)
# ...
ruby-2.6.6/gems/rubocop-performance-1.7.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions