Would it be possible to write this Akka HTTP Routes check?
ktoso opened this issue · comments
Hi guys,
would it be possible to detect such a case:
val routes =
get { /*anything here */ } ~
post { } // GOOD
val bad =
get { /*anything here */ }
post { } // BAD, post is not included in the "bad" val, missing ~ combinator
If its possible I'll give it a try, but may need some pointers where to start looking at examples, so I don't spend an entire weekend on it :)
Hey, have a look at the other rules, in principle you need to match calls to get
/post
and flag if they are part of a block in the "stats" part (not the expression). Here's the definition of a Block:
Block(stats: List[Tree], expr: Tree)
Everything in stats
is going to be evaluated, but the value is dropped. You want to warn about any calls to get
or post
in there. Actually, the compiler already warns if a pure expression is in statement position, but it doesn't know that get
is pure.
Thanks, that's helpful! I'll try to give it a shot.
I now realised that it'll be a bit painful because we have 150+ of these methods and it could also be user methods hm... But a best effort may be a good idea anyway :)
Actually, the compiler already warns if a pure expression is in statement position, but it doesn't know that get is pure.
Ah very true. That would be awesome to tell it's @pure
(yeah I know, another one to add to the neverending scalac whishlist ;-)).
Heh that's exactly an instance of the more general question I just asked on scala-user: https://groups.google.com/forum/#!topic/scala-user/fIzON6Ss87A, and replies led me here.
Maybe instead of checking every directive, it would be enough to check that results of type Route
aren't discarded. Or even more generally, that results from package akka.http.directives._
(I know that's not the right name ;) ) can't be discarded.
Maybe this could go into configuration of the plugin ... let me check :)
Several month ago I wrote a simple scalac plugin that detects and reports value discards within the Block AST nodes: https://github.com/mkubala/scalac-guardedblocks-plugin
One of the tests for akka-http covers the case you've mentioned: https://github.com/mkubala/scalac-guardedblocks-plugin/blob/master/tests/akkaHttp/src/test/resources/akkaHttp/routes/SimpleRouteFail.scala
I'm going to help @adamw port this plugin to scala-abide.
Abide is superseded by https://github.com/scalacenter/scalafix; closing all issues