启用Return规则的Wartremover与scala-async兼容问题咨询
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

