You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

启用Return规则的Wartremover与scala-async兼容问题咨询

Compatibility Issue Between scala-async and Wartremover's Return Rule

Problem Description

I'm using the scala-async library alongside Wartremover with its Return rule enabled. When compiling the following test code:

async { 
  val studentsCount = await(studentsDao.getStudents()) 
  val externalStudentsCount = await(studentsDao.getExternalStudents()) 
  externalStudentsCount + studentsCount 
}

I get the error: [wartremover:Return] return is disabled. Is there a way to make Wartremover work with scala-async without disabling the Return rule entirely?

Solution

Great question! The root cause here is that the scala-async macro generates hidden return statements under the hood to manage the asynchronous execution flow—these are the ones triggering Wartremover's Return rule, even though you didn't write them explicitly.

Here are two reliable ways to fix this:

1. Locally Suppress the Return Rule for the Async Block

The most precise approach is to use Wartremover's @SuppressWarnings annotation to disable the Return rule only for the code containing the async block. This way you keep the rule enabled everywhere else in your codebase.

Modify your code like this:

import org.wartremover.warts.Return

@SuppressWarnings(Array("org.wartremover.warts.Return"))
async {
  val studentsCount = await(studentsDao.getStudents())
  val externalStudentsCount = await(studentsDao.getExternalStudents())
  externalStudentsCount + studentsCount
}

2. Exclude Specific Files/Paths in Wartremover Configuration

If you have multiple async-heavy files, you can configure Wartremover to skip checking the Return rule for those files in your build.sbt. For example:

// Target specific files (recommended over global disable)
wartremoverExcludedPaths ++= Seq(
  "glob:**/AsyncStudentService.scala" // Adjust path to your async-heavy file
)

Note: Avoid globally disabling the Return rule unless absolutely necessary, as it defeats the purpose of Wartremover's safety checks.


内容的提问来源于stack exchange,提问作者Krzysztof Atłasik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:57:39