Merge pull request #3351 from Duhemm/wip/fix-3299

Allow `.value` on SettingKey inside ifs & lambdas
This commit is contained in:
Dale Wijnand 2017-07-24 07:42:58 +01:00 committed by GitHub
commit d6803f0645
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)
}
}
}