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

Add new inspection: abstract trait #887

Merged
merged 5 commits into from
Oct 16, 2024
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
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
}
}

}