I would like to create a custom inspection in IntelliJ to warn the use of a return statement inside an anonymous function in scala. For example, lets suppose I want to count the number of values higher than 3 in a Seq (in a very ugly way):
val seq = Seq(1,2,3,4,5,6,7)
def foo(s: Seq[Int]): Int = {
s.map(x => {
if (x > 3)
return 1
0
}).count()
}
foo(seq)
This code is wrong, the mistake here is that the return
statement will cause the named foo
function to return 1, instead of returning 1 in the anonymous function x => {...}
.
The code I'm working on is composed of a lot of files, and I already found this mistake several times, but there are too many files and I cannot examine them all manually.
I'm working with IntelliJ-idea, and I know that it is possible to create custom inspection rules with the search and replace template option, but for such complex inspection, I havn't been able to produce the template nor to find any help on how to create it.
Thanks in advance for any help/hints !