Allow `.value` on SettingKey inside ifs & lambdas

Calling `.value` on a SettingKey doesn't trigger any execution and
doesn't have any side effect, so we can safely allow calls to `.value`
inside conditionals and lambdas.

Fixes #3299
This commit is contained in:
Martin Duhem 2017-07-21 16:26:45 +02:00
parent 2d7ec47b13
commit 9dfe1a8406
No known key found for this signature in database
GPG Key ID: CC9CE9656E10C555
2 changed files with 28 additions and 4 deletions

View File

@ -1,5 +1,6 @@
package sbt.std
import sbt.SettingKey
import sbt.internal.util.ConsoleAppender
import sbt.internal.util.appmacro.{ Convert, Converted, LinterDSL }
@ -62,10 +63,12 @@ abstract class BaseTaskLinterDSL extends LinterDSL {
case ap @ Apply(TypeApply(Select(_, nme), tpe :: Nil), qual :: Nil) =>
val shouldIgnore = uncheckedWrappers.contains(ap)
val wrapperName = nme.decodedName.toString
if (!shouldIgnore && isTask(wrapperName, tpe.tpe, qual)) {
val qualName =
if (qual.symbol != null) qual.symbol.name.decodedName.toString
else ap.pos.lineContent
val (qualName, isSettingKey) =
Option(qual.symbol)
.map(sym => (sym.name.decodedName.toString, sym.info <:< typeOf[SettingKey[_]]))
.getOrElse((ap.pos.lineContent, false))
if (!isSettingKey && !shouldIgnore && isTask(wrapperName, tpe.tpe, qual)) {
if (insideIf && !isDynamicTask) {
// Error on the use of value inside the if of a regular task (dyn task is ok)
ctx.error(ap.pos, TaskLinterDSLFeedback.useOfValueInsideIfExpression(qualName))

View File

@ -150,4 +150,25 @@ class TaskPosSpec {
avoidDCE
}
}
locally {
import sbt._
import sbt.Def._
val foo = settingKey[String]("")
val condition = true
val baz = Def.task[String] {
// settings can be evaluated in a condition
if (condition) foo.value
else "..."
}
}
locally {
import sbt._
import sbt.Def._
val foo = settingKey[String]("")
val baz = Def.task[Seq[String]] {
(1 to 10).map(_ => foo.value)
}
}
}