-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new inspection: abstract trait (#887)
* add abstract trait inspection * add to all inspections * fix readme info * change import for compiling in scala2.12 * resorct inspections in README.me, fix test
- Loading branch information
Showing
4 changed files
with
79 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/scala-2/com/sksamuel/scapegoat/inspections/traits/AbstractTrait.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.sksamuel.scapegoat.inspections.traits | ||
|
||
import com.sksamuel.scapegoat._ | ||
|
||
class AbstractTrait | ||
extends Inspection( | ||
text = "Use of abstract trait", | ||
defaultLevel = Levels.Info, | ||
description = "Traits are automatically abstract.", | ||
explanation = "The abstract modifier is used in class definitions. It is redundant for traits, and mandatory for all other classes which have incomplete members." | ||
){ | ||
|
||
override def inspector(ctx: InspectionContext): Inspector = { | ||
new Inspector(ctx) { | ||
override def postTyperTraverser: context.Traverser = | ||
new context.Traverser { | ||
|
||
import context.global._ | ||
|
||
def isAbstractTrait(positions: Map[Long, Position]): Boolean = { | ||
positions.contains(Flag.TRAIT) && positions.contains(Flag.ABSTRACT) | ||
} | ||
|
||
override def inspect(tree: Tree): Unit = { | ||
tree match { | ||
// I use positions, because all traits are abstract by default | ||
case ClassDef(mods, _, _, _) if isAbstractTrait(mods.positions) => | ||
context.warn(tree.pos, self, tree.toString.take(500)) | ||
case _ => continue(tree) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/test/scala-2/com/sksamuel/scapegoat/inspections/traits/AbstractTraitTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.sksamuel.scapegoat.inspections.traits | ||
|
||
import com.sksamuel.scapegoat.{Inspection, InspectionTest} | ||
|
||
class AbstractTraitTest extends InspectionTest { | ||
|
||
override val inspections = Seq[Inspection](new AbstractTrait) | ||
|
||
"abstract trait use" - { | ||
"should report warning" in { | ||
val code = "abstract trait Test { val x: Int = 1 }" | ||
|
||
compileCodeSnippet(code) | ||
compiler.scapegoat.feedback.warnings.size shouldBe 1 | ||
} | ||
|
||
"should not report warning on sealed" in { | ||
val code = "sealed trait Test { val x: Int = 1 }" | ||
|
||
compileCodeSnippet(code) | ||
compiler.scapegoat.feedback.warnings.size shouldBe 0 | ||
} | ||
|
||
"should not report warning on trait without modifiers" in { | ||
val code = "trait Test1 { val x: Int = 1 }" | ||
|
||
compileCodeSnippet(code) | ||
compiler.scapegoat.feedback.warnings.size shouldBe 0 | ||
} | ||
|
||
"should not report on private trait" in { | ||
val code = "private trait Test1 { val x: Int = 1 }" | ||
|
||
compileCodeSnippet(code) | ||
compiler.scapegoat.feedback.warnings.size shouldBe 0 | ||
} | ||
} | ||
|
||
} |