Skip to content

Two ways of false positives for DuplicateMethodCall #1813

Description

@RST-J

Hello reekers and thanks for the effort to support a world with better Ruby code.

In my opinion I found two kinds of false positives for the DuplicateMethodCall smell. Consider the following method - which also contains nested iterators and could be split such that the duplicate method calls are not found, but that is not the point. Reek reports duplicate method calls for att[:file_name].

def dedup_attachment_names
  attachments.group_by { |att| att[:file_name] }.each_value do |atts|
    atts.each_with_index do |att, idx|
      next if idx.zero?

      att[:file_name] += "_#{idx + 1}"
    end
  end
end

First, renaming one of the att block arguments makes the duplicate method call going away. My point is, it is a mere syntactical duplication but not a semantic one. It would not be possible to hold the lookup in a separate variable and have it reused in both places.

This is the weaker of my false positives and I'd say as tools like reek have to make tradeoffs its better to get a hint here which makes me reconsider what I wrote and if I'm sticking with it, use a magic comment to disable the finding. Nevertheless I thought, it might be worth to bring that to your attention.

The second false positive, for which I think this should not be flagged, is, that its not actually a duplicate method call. The first is a lookup. The second is a write using +=. I can see the syntactical reason for that, but semantically, to my knowledge, there is no way to avoid that. I see the need for duplicate method calls being flagged with method chaining - foo.bar.baz and foo.bar.baz += 42 should flag foo.bar as duplicate - but I think bar.baz and bar.baz += 42 should not be considered duplicate method calls.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions