Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix UtilityFunction false positive with Kernel refinement #1563

Closed
bkuhlmann opened this issue Dec 4, 2020 · 4 comments · Fixed by #1574
Closed

Fix UtilityFunction false positive with Kernel refinement #1563

bkuhlmann opened this issue Dec 4, 2020 · 4 comments · Fixed by #1574
Assignees
Labels

Comments

@bkuhlmann
Copy link

bkuhlmann commented Dec 4, 2020

Overview

Seeing the following warning:

lib/versionaire/cast.rb -- 1 warning:
  [7]:UtilityFunction: Versionaire::Cast#Version doesn't depend on instance state (maybe move it to another class?) [https://github.com/troessner/reek/blob/v6.0.2/docs/Utility-Function.md]

...for this refinement:

module Versionaire
  # Refines Kernel in order to provide a top-level Version conversion function.
  module Cast
    refine Kernel do
      def Version object
        Versionaire::Version object
      end
    end
  end
end

Potential Usage:

using Versionaire::Cast

Version "1.2.3"  # => #<struct Versionaire::Version major=1, minor=2, patch=3>

Desired Behavior

It would be nice to not flag this as a violation in this case. This is similar in nature to what was reported earlier.

Notes

I do a lot with refinements so, for context, this might help:

  • Refinements - A collection of enhancements to Ruby core classes.
  • Versionaire - The Ruby Version type, shown above, that I'd like to access at the Kernel level much like Integer, String, Array, Hash, etc.
@mvz
Copy link
Collaborator

mvz commented Dec 4, 2020

This makes sense. I'm going to take a look at how we can support refinements in Reek.

@mvz mvz added the defect label Dec 4, 2020
@mvz mvz self-assigned this Dec 4, 2020
@bkuhlmann
Copy link
Author

bkuhlmann commented Dec 12, 2020

In case it's of interest/help, here's the Reek configuration I ended up settling on to workaround this issue:

detectors:
  UncommunicativeMethodName:
    exclude:
    - Cast#Version
    - Versionaire#Version
  UtilityFunction:
    exclude:
    - Versionaire::Cast#Version

Due to implementing a Ruby cast/conversion function, this also causes Reek's UncommunicativeMethodName analyzer to trip too.

@mvz
Copy link
Collaborator

mvz commented Jan 3, 2021

I've investigated this a bit further and it looks like even if Reek correctly recognizes refinements (and it should!), this would still be considered a UtilityFunction, just like in the following case:

module Foo
  def Version object
    Versionaire::Version object
  end
end

However, with proper support, you should be able to exclude Kernel#Version instead of Cast#Version.

@bkuhlmann
Copy link
Author

🙇 @mvz. OK, so reduced to a utility function but not uncommunicative if I understand correctly? Yep, I'll give this a look once the next version is released of Reek. 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants