-
Notifications
You must be signed in to change notification settings - Fork 22
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
1 parent
3a130e9
commit 01c39b3
Showing
3 changed files
with
51 additions
and
0 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
18 changes: 18 additions & 0 deletions
18
analyzer/src/main/scala/com/avsystem/commons/analyzer/ByNameImplicitParameter.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,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 _ => | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
analyzer/src/test/scala/com/avsystem/commons/analyzer/ByNameParametersTest.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,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 | ||
) | ||
} | ||
} |