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

False-positive - empty methods no so empty #329

Merged
merged 2 commits into from
Mar 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class EmptyMethod
case DefDef(mods, _, _, _, _, _) if mods.isOverride =>
case ClassDef(mods, _, _, _) if mods.isTrait => continue(tree)
case DefDef(_, _, _, _, _, _) if tree.symbol != null && tree.symbol.enclClass.isTrait =>
case DefDef(_, _, _, _, _, Literal(Constant(()))) =>
case d @ DefDef(_, _, _, _, _, Literal(Constant(()))) if d.symbol != null && d.symbol.isPrivate =>
context.warn(tree.pos, self, tree.toString.take(500))
case _ => continue(tree)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ class EmptyMethodTest extends AnyFreeSpec with Matchers with PluginRunner with O
"empty method" - {
"should report warning" in {
val code = """object Test {
def foo = { }
def foo2 = true
def foo3 = {
private def foo = { }
private def foo2 = true
private def foo3 = {
()
}
def foo4 = {
private def foo4 = {
println("sammy")
()
}
Expand All @@ -33,6 +33,26 @@ class EmptyMethodTest extends AnyFreeSpec with Matchers with PluginRunner with O
compileCodeSnippet(code)
compiler.scapegoat.feedback.warnings.size shouldBe 0
}

"for empty methods in public classes" in {
val code =
"""
|class Animal {
| def makeSound(): Unit = {}
|}
|
|class Dog extends Animal {
| override def makeSound(): Unit = {
| println("Bark")
| }
|}
|
|class Fish extends Animal {}
|""".stripMargin

compileCodeSnippet(code)
compiler.scapegoat.feedback.warnings.size shouldBe 0
}
}
}
}