-
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.
- Loading branch information
Showing
5 changed files
with
82 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
package com.sksamuel.scapegoat | ||
|
||
import com.sksamuel.scapegoat.inspections.option._ | ||
import com.sksamuel.scapegoat.inspections.traits._ | ||
|
||
object Inspections { | ||
|
||
final val inspections: List[Inspection] = List( | ||
new OptionGet | ||
new OptionGet, | ||
new AbstractTrait | ||
) | ||
|
||
} |
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
41 changes: 41 additions & 0 deletions
41
src/main/scala-3/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,41 @@ | ||
package com.sksamuel.scapegoat.inspections.traits | ||
|
||
import com.sksamuel.scapegoat.* | ||
import dotty.tools.dotc.ast.Trees.* | ||
import dotty.tools.dotc.ast.tpd | ||
import dotty.tools.dotc.core.Contexts.Context | ||
import dotty.tools.dotc.core.Flags | ||
import dotty.tools.dotc.core.StdNames.* | ||
import dotty.tools.dotc.core.Types.TypeRef | ||
import dotty.tools.dotc.core.Symbols.ClassSymbol | ||
import dotty.tools.dotc.util.SourcePosition | ||
|
||
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." | ||
) { | ||
|
||
import tpd.* | ||
|
||
def inspect(feedback: Feedback[SourcePosition], tree: tpd.Tree)(using ctx: Context): Unit = { | ||
val traverser = new InspectionTraverser { | ||
def traverse(tree: Tree)(using Context): Unit = { | ||
tree match { | ||
case tDef: TypeDef => | ||
tDef.tpe match { | ||
case TypeRef(_, kls: ClassSymbol) if kls.flags.is(Flags.Trait) && kls.flags.is(Flags.Abstract) => | ||
feedback.warn(tree.sourcePos, self, tree.asSnippet) | ||
case _ => | ||
} | ||
|
||
case _ => | ||
traverseChildren(tree) | ||
} | ||
} | ||
} | ||
traverser.traverse(tree) | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/test/scala-3/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,36 @@ | ||
package com.sksamuel.scapegoat.inspections.traits | ||
|
||
import com.sksamuel.scapegoat.InspectionTest | ||
|
||
class AbstractTraitTest extends InspectionTest(classOf[AbstractTrait]) { | ||
|
||
"abstract trait use" - { | ||
"should report warning" in { | ||
val code = "abstract trait Test { }" | ||
|
||
val feedback = runner.compileCodeSnippet(code) | ||
feedback.warnings.assertable.size shouldBe 1 | ||
} | ||
|
||
"should not report warning on sealed" in { | ||
val code = "sealed trait Test { val x: Int = 1 }" | ||
|
||
val feedback = runner.compileCodeSnippet(code) | ||
feedback.warnings.assertable.size shouldBe 0 | ||
} | ||
|
||
"should not report warning on trait without modifiers" in { | ||
val code = "trait Test1 { val x: Int = 1 }" | ||
|
||
val feedback = runner.compileCodeSnippet(code) | ||
feedback.warnings.assertable.size shouldBe 0 | ||
} | ||
|
||
"should not report on private trait" in { | ||
val code = "private trait Test1 { val x: Int = 1 }" | ||
|
||
val feedback = runner.compileCodeSnippet(code) | ||
feedback.warnings.assertable.size shouldBe 0 | ||
} | ||
} | ||
} |