Rubocop: False positive redundant return detected

Created on 18 May 2017  路  4Comments  路  Source: rubocop-hq/rubocop

Hi, some methods, specially the ones ending with a ! don't always return what you want you method to return and you do need to use return, see the following example:

      def slice_host(slice, prefix)
        prefix.chars.zip(slice.chars).each do |char1, char2|
          break if char1 != char2
          slice.slice!(char1)
        end
        slice.slice!('.') if slice.chars.first == '.'
        return slice
      end

In the function above, rubocop complains about redundant return detected, however, if I don't use return slice the function won't return what I expect it to return.

Most helpful comment

@deivid-rodriguez: There's a feature request for that. 馃檪

All 4 comments

It just wants you to replace return slice with slice on the last line

And what is the problem of using return?

@luisdavim It's not a problem, it's just a matter of style and consistency. If you don't like the rule, you can disable it in your rubocop config:

Style/RedundantReturn:
  Enabled: false

PS: I think it would make sense to add the ability for this cop to enforce _always_ using explicit returns.

@deivid-rodriguez: There's a feature request for that. 馃檪

Was this page helpful?
0 / 5 - 0 ratings