Skip to content

Commit

Permalink
require check for scala 2
Browse files Browse the repository at this point in the history
  • Loading branch information
fntz committed Oct 18, 2024
1 parent 16b1c9d commit e6fd22a
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ To suppress warnings globally for the project, use `disabledInspections` or `ove

### Inspections

There are currently 122 inspections for Scala 2, and 1 for Scala 3.
There are currently 123 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 |
Expand All @@ -168,6 +168,7 @@ An overview list is given, followed by a more detailed description of each inspe
| ArraysToString | Checks for explicit toString calls on arrays | Warning | Yes | No |
| AsInstanceOf | Checks for use of `asInstanceOf` | Warning | Yes | No |
| AvoidOperatorOverload | Checks for mental symbolic method names | Info | Yes | No |
| AvoidRequire | Use of require | Warning | Yes | No |
| AvoidSizeEqualsZero | Traversable.size can be slow for some data structure, prefer .isEmpty | Warning | Yes | No |
| AvoidSizeNotEqualsZero | Traversable.size can be slow for some data structure, prefer .nonEmpty | Warning | Yes | No |
| AvoidToMinusOne | Checks for loops that use `x to n-1` instead of `x until n` | Info | Yes | No |
Expand Down
1 change: 1 addition & 0 deletions src/main/scala-2/com/sksamuel/scapegoat/Inspections.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ object Inspections extends App {

def inspections: Seq[Inspection] =
Seq(
new AvoidRequire,
new ArrayEquals,
new ArraysInFormat,
new ArraysToString,
Expand Down
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)
}
}
}
}

}
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
}
}
}

0 comments on commit e6fd22a

Please sign in to comment.