即使我正在使用变量,也会收到未使用的变量警告

getting unused variable warnings even though I am using the variables

在下面的代码中,我得到了一个令牌,我正在查询 return Future 的多个数据库。我想根据上一个查询的结果进行下一个查询。

val result:Future[Result] = for{tokenOption:Option[UserToken] <- if(urlHost != "" && successUrlParameter != "" && failUrlParameter != "") {userTokenRepo.findOne(UserTokenKey(UUID.fromString(token)))} else {Future.successful(None)}  //generator 1 - get token from database
                                    userOption:Option[User] <- if (tokenOption.isDefined) {userRepo.findOne(tokenOption.get.userKeys)} else {Future.successful(None)} //generator2. found token, look for corresponding user to which the token belongs
                                    modifiedUser:Option[User] <- if (userOption.isDefined) {confirmSignupforUser(userOption.get)} else Future.successful(None) //generator 3. found user and token. Update profile
                                    deletedToken:Option[UserTokenKey] <- if(modifiedUser.isDefined) {userTokenRepo.delete(UserTokenKey(UUID.fromString(token)))} else Future.successful(None)
       }
         yield { //check if we have user and token and modified user here. If any is missing, return error else success
           if(urlHost == "" || successUrlParameter == "" || failUrlParameter == ""){//error in reading redirection url.
             InternalServerError(Json.toJson(JsonResultError("Internal Server Error. Redirection missing ")))
           } else { //read redirection url, so can redirect with success or failure parameter
             println("db query results tokenOption: " + tokenOption + ", userOption: " + userOption + " : modifiedUserOption: " + modifiedUser + ", deletedToken: " + deletedToken)
             //all db ops other than deletinng token should be successful. Error in deletinng token is not a critical error from user perspective and thus should not affect signup verification
             if (tokenOption.isDefined && userOption.isDefined && modifiedUser.isDefined /*&& deletedToken.isDefined*/) {
               Redirect(s"${urlHost};${successUrlParameter}") //TODOM - pick from config
             }
             else {
               /*TODOM - when redirecting with error, can provide additional info why sign up failed*/
               Redirect(s"${urlHost};${failUrlParameter}") //TODOM - pick from config
             }
           }
         }

我在编译器选项中启用了 "-Xfatal-warnings"-Ywarn-unused:locals。我收到警告,未使用 tokenOptionuserOptionmodifiedUserdeletedToken,但我正在使用它们(如果 Option [,则下一个查询完成=25=]由前一个编辑 isDefined。)。为什么我会收到编译器警告?

Error:(410, 40) pattern var tokenOption in value $anonfun is never used; `tokenOption@_' suppresses this warning
       val result:Future[Result] = for{tokenOption:Option[UserToken] <- if(urlHost != "" && successUrlParameter != "" && failUrlParameter != "") {userTokenRepo.findOne(UserTokenKey(UUID.fromString(token)))} else {Future.successful(None)}  //generator 1 - get token from database

Error:(411, 37) pattern var userOption in value $anonfun is never used; `userOption@_' suppresses this warning
                                    userOption:Option[User] <- if (tokenOption.isDefined) {userRepo.findOne(tokenOption.get.userKeys)} else {Future.successful(None)} //generator2. found token, look for corresponding user to which the token belongs

根据 Unused warning occurred even if the variable was used #11175,这似乎是一个错误。以下片段

object Hello {
  for {
    a <- Option(1)
    b <- Option(2) if a == 1
  } yield a + b
}

在我的机器上用 Scala 2.12.8 和 2.13 重现了这个问题,但在 Scala 2.12.1 上似乎没有出现,所以我建议尝试不同的版本,看看哪个版本适合你。