Skip to content

Commit

Permalink
Add new inspection: abstract trait (#887)
Browse files Browse the repository at this point in the history
* 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
fntz authored Oct 16, 2024
1 parent 85d47a4 commit 16b1c9d
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,12 @@ To suppress warnings globally for the project, use `disabledInspections` or `ove

### Inspections

There are currently 121 inspections for Scala 2, and 1 for Scala 3.
There are currently 122 inspections for Scala 2, and 1 for Scala 3.
An overview list is given, followed by a more detailed description of each inspection after the list (todo: finish rest of detailed descriptions)

| Name | Brief Description | Default Level | Scala 2 | Scala 3 |
|---------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------|---------|---------|
| AbstractTrait | Check if trait is abstract | Info | Yes | No |
| ArrayEquals | Checks for comparison of arrays using `==` which will always return false | Info | Yes | No |
| ArraysInFormat | Checks for arrays passed to String.format | Error | Yes | No |
| ArraysToString | Checks for explicit toString calls on arrays | Warning | Yes | No |
Expand Down
4 changes: 3 additions & 1 deletion src/main/scala-2/com/sksamuel/scapegoat/Inspections.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.sksamuel.scapegoat.inspections.option._
import com.sksamuel.scapegoat.inspections.string._
import com.sksamuel.scapegoat.inspections.style._
import com.sksamuel.scapegoat.inspections.unneccesary._
import com.sksamuel.scapegoat.inspections.traits._
import com.sksamuel.scapegoat.inspections.unsafe._

/**
Expand Down Expand Up @@ -146,6 +147,7 @@ object Inspections extends App {
new VarClosure,
new VarCouldBeVal,
new WhileTrue,
new ZeroNumerator
new ZeroNumerator,
new AbstractTrait
)
}
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)
}
}
}
}
}
}
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
}
}

}

0 comments on commit 16b1c9d

Please sign in to comment.