-
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
6 changed files
with
122 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.AvoidRequire | ||
import com.sksamuel.scapegoat.inspections.option._ | ||
|
||
object Inspections { | ||
|
||
final val inspections: List[Inspection] = List( | ||
new OptionGet | ||
new OptionGet, | ||
new AvoidRequire | ||
) | ||
|
||
} |
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
39 changes: 39 additions & 0 deletions
39
src/main/scala-3/com/sksamuel/scapegoat/inspections/AvoidRequire.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 | ||
|
||
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.Types.TermRef | ||
import dotty.tools.dotc.core.Symbols.Symbol | ||
import dotty.tools.dotc.util.SourcePosition | ||
|
||
class AvoidRequire | ||
extends Inspection( | ||
text = "Use of require", | ||
defaultLevel = Levels.Warning, | ||
description = "Use require in code", | ||
explanation = "Using require throws an untyped Exception." | ||
) { | ||
|
||
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 f @ Apply(ident: Ident, _) if ident.name.toString == "require" => | ||
ident.tpe.normalizedPrefix match { | ||
case TermRef(tx, nm: Symbol) | ||
if nm.toString == "object Predef" && | ||
tx.normalizedPrefix.typeSymbol.name.toString == "<root>" => | ||
feedback.warn(tree.sourcePos, self, tree.asSnippet) | ||
case x => | ||
} | ||
case _ => traverseChildren(tree) | ||
} | ||
} | ||
} | ||
traverser.traverse(tree) | ||
} | ||
} |
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
62 changes: 62 additions & 0 deletions
62
src/test/scala-3/com/sksamuel/scapegoat/inspections/AvoidRequireTest.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,62 @@ | ||
package com.sksamuel.scapegoat.inspections | ||
|
||
import com.sksamuel.scapegoat.InspectionTest | ||
|
||
class AvoidRequireTest extends InspectionTest(classOf[AvoidRequire]) { | ||
|
||
"require use" - { | ||
"should return warning in method" in { | ||
val code = | ||
""" | ||
object Test { | ||
def test(x: Int): Int = { | ||
require(x == 1) | ||
x | ||
} | ||
} | ||
""".stripMargin | ||
|
||
val feedback = runner.compileCodeSnippet(code) | ||
feedback.warnings.assertable.size shouldBe 1 | ||
} | ||
|
||
"should return warning in class" in { | ||
val code = | ||
""" | ||
class Test(val x: Int) { | ||
require(x == 1, "oops") | ||
} | ||
""".stripMargin | ||
|
||
val feedback = runner.compileCodeSnippet(code) | ||
feedback.warnings.assertable.size shouldBe 1 | ||
} | ||
|
||
"should not return warnin on own require method" in { | ||
val code = | ||
""" | ||
object T { | ||
def require(x: Boolean): Boolean = false | ||
def foo(): Boolean = { | ||
require(1 == 1) | ||
} | ||
} | ||
""".stripMargin | ||
|
||
val feedback = runner.compileCodeSnippet(code) | ||
feedback.warnings.assertable.size shouldBe 0 | ||
} | ||
|
||
"should not return warning if no require" in { | ||
val code = | ||
""" | ||
class Test(val x: Int) { } | ||
""".stripMargin | ||
|
||
val feedback = runner.compileCodeSnippet(code) | ||
feedback.warnings.assertable.size shouldBe 0 | ||
} | ||
} | ||
|
||
} |