Skip to content

Commit

Permalink
add byNameImplicitParameter rule
Browse files Browse the repository at this point in the history
  • Loading branch information
halotukozak committed Aug 22, 2024
1 parent 3a130e9 commit 01c39b3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ final class AnalyzerPlugin(val global: Global) extends Plugin { plugin =>
new BadSingletonComponent(global),
new ConstantDeclarations(global),
new BasePackage(global),
new ByNameImplicitParameter(global),
)

private lazy val rulesByName = rules.map(r => (r.name, r)).toMap
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.avsystem.commons
package analyzer

import scala.tools.nsc.Global

final class ByNameImplicitParameter(g: Global) extends AnalyzerRule(g, "byNameImplicitParameter") {

import global.*

private def isByNameImplicit(x: ValDef): Boolean =
x.symbol.isImplicit && x.symbol.isByNameParam && !x.symbol.isSynthetic
def analyze(unit: CompilationUnit): Unit = unit.body.foreach {
case tree: DefDef if !tree.symbol.isSynthetic && tree.vparamss.flatten.exists(isByNameImplicit) =>
report(tree.pos, "Implicit by-name parameters are disabled")
case _ =>
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.avsystem.commons
package analyzer

import org.scalatest.funsuite.AnyFunSuite

final class ByNameParametersTest extends AnyFunSuite with AnalyzerTest {
test("should report by-name implicit parameters") {
assertErrors(2,
//language=Scala
"""
|object whatever {
| def byName(x: => Int) = x
| def byName(implicit x: => String) = x
| def byName(x: => Int)(implicit y: => Int) = x
| def byName(implicit x: Int) = x
|}
""".stripMargin
)
}

test("should report by-name implicit constructor parameters") {
assertErrors(2,
//language=Scala
"""
|object whatever {
| class C(x: => Int)(implicit y: => Int, z: String)
| class D(implicit y: => Int, z: => String)
|}
|""".stripMargin
)
}
}

0 comments on commit 01c39b3

Please sign in to comment.