-
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
4 changed files
with
80 additions
and
1 deletion.
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
31 changes: 31 additions & 0 deletions
31
src/main/scala-2/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,31 @@ | ||
package com.sksamuel.scapegoat.inspections | ||
|
||
import com.sksamuel.scapegoat.{Inspection, InspectionContext, Inspector, Levels} | ||
|
||
class AvoidRequire extends | ||
Inspection( | ||
text = "Use of require", | ||
defaultLevel = Levels.Warning, | ||
description = "Use require in code", | ||
explanation = "Using require throws an untyped Exception." | ||
) { | ||
|
||
def inspector(ctx: InspectionContext): Inspector = | ||
new Inspector(ctx) { | ||
override def postTyperTraverser: context.Traverser = | ||
new context.Traverser { | ||
|
||
import context.global._ | ||
|
||
override def inspect(tree: Tree): Unit = { | ||
tree match { | ||
case Select(lhs, TermName("require")) if lhs.tpe.typeSymbol.fullName == "scala.Predef" => | ||
context.warn(tree.pos, self, tree.toString.take(200)) | ||
case _ => | ||
continue(tree) | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
src/test/scala-2/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,46 @@ | ||
package com.sksamuel.scapegoat.inspections | ||
|
||
import com.sksamuel.scapegoat.{Inspection, InspectionTest} | ||
|
||
class AvoidRequireTest extends InspectionTest { | ||
override val inspections = Seq[Inspection](new AvoidRequire) | ||
|
||
"require use" - { | ||
"should return warning in method" in { | ||
val code = | ||
""" | ||
object Test { | ||
def test(x: Int): Int = { | ||
require(x == 1) | ||
x | ||
} | ||
} | ||
""".stripMargin | ||
|
||
compileCodeSnippet(code) | ||
compiler.scapegoat.feedback.warnings.size shouldBe 1 | ||
} | ||
|
||
"should return warning in class" in { | ||
val code = | ||
""" | ||
class Test(val x: Int) { | ||
require(x == 1, "oops") | ||
} | ||
""".stripMargin | ||
|
||
compileCodeSnippet(code) | ||
compiler.scapegoat.feedback.warnings.size shouldBe 1 | ||
} | ||
|
||
"should not return warning if no require" in { | ||
val code = | ||
""" | ||
class Test(val x: Int) { } | ||
""".stripMargin | ||
|
||
compileCodeSnippet(code) | ||
compiler.scapegoat.feedback.warnings.size shouldBe 0 | ||
} | ||
} | ||
} |