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

New rule: Unnecessary Method #97

Closed
Blacksmoke16 opened this issue Mar 27, 2019 · 3 comments
Closed

New rule: Unnecessary Method #97

Blacksmoke16 opened this issue Mar 27, 2019 · 3 comments
Labels

Comments

@Blacksmoke16
Copy link
Contributor

Given the following code:

abstract class Parent
  def initialize(@name : String); end
end

class Child < Parent
  def initialize(name : String)
    super
   end
end

The child's initializer isn't required since it is the same as the parent.

@veelenga
Copy link
Member

To be more general, we can report any method which implements only call to super, correct?

@Blacksmoke16
Copy link
Contributor Author

Blacksmoke16 commented Mar 27, 2019

Probably as long as the params/types are the same. For example:

abstract class Parent
  def bark(name : String)
    pp name
  end
end

class Child < Parent
  def bark(name : String)
    super
   end
end

Would also be unnecessary.

abstract class Parent
  def bark(name : String)
    pp name
  end
end

class Child < Parent
  def bark(name : String, some_bool)
    super name
   end
end

Would be required since the params are not the same.

@veelenga veelenga changed the title Unnecessary initializer New rule: Unnecessary Method Apr 14, 2019
@veelenga
Copy link
Member

I found this tricky way to expose parent's private method in std lib:

class A
  private def method(a)
    puts a
  end
end

class B < A
  def method(b)
    super
  end
end

B.new.method(10) #=> 10

maybe this is not the best architecture but it looks like the language intentionally allows it. So we don't want to disallow this by Ameba.

FYI Ameba is a syntax analyzer, meaning it can't get info about parent class (it can be located in a different file). So there is no way to distinguish between a real "unnecessary method" and the intentional call to super

@Sija Sija added rule and removed feature labels Oct 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants