46

Are there any tools for performing static analysis of Scala code, similar to FindBugs and PMD for Java or Splint for C/C++? I know that FindBugs works on the bytecode produced by compiling Java, so I'm curious as to how it would work on Scala.

Google searches (as of 27 October 2009) reveal very little.

Google searches (as of 01 February 2010) reveal this question.

Jonas
  • 121,568
  • 97
  • 310
  • 388
Roman Kagan
  • 10,440
  • 26
  • 86
  • 126
  • 1
    What kind of bugs, beyond the ones that the compiler is already looking for? – James Black Oct 21 '09 at 06:01
  • 1
    Not really - what compiler can find I can find easily also. What I'm looking for is "dead code", unnecessary initialized variables, etc. For more static analysis take a look at http://findbugs.sourceforge.net/ – Roman Kagan Oct 21 '09 at 06:05
  • 1
    Good luck in your search, but analyzers of the kind of FindBugs represent a lot of work and Scala may be too young a language to have any yet. – Pascal Cuoq Oct 21 '09 at 06:44
  • 2
    *@Pascal* - Scala was first released in 2003: it is 6 years old! – oxbow_lakes Oct 21 '09 at 07:01
  • 5
    @Pascal FindBugs works at JVM bytecode level, so it should work with Scala as well. – Walter Chang Oct 21 '09 at 09:26
  • @Walter Interesting. It's a good way to handle bytecode-only libraries, but doesn't it make it harder to locate the problem for inspection by the user? – Pascal Cuoq Oct 22 '09 at 12:41
  • @oxbow_lakes 6 years is a short time when you don't have the marketing divisions of Sun or Microsoft to make everyone switch to your language. Coincidentally, I started working on a static analyzer in 2003, too. 6 years later, it is starting to be usable. Of course, that could not have been a Scala analyzer (it's for embedded C :) – Pascal Cuoq Oct 22 '09 at 12:45
  • Can I ask this same question as of August 2012? – Luciano Aug 15 '12 at 17:22
  • @Luciano Yes, you can, and I've added an answer. – Matthew Farwell Oct 12 '12 at 18:37

9 Answers9

20

FindBugs analyzes JVM byte codes, regardless of the tool that generated them. I've tried using FindBugs to check .class files generated by Scala. Unfortunately, FindBugs produced many warnings, even for trivial Scala programs.

Chris Peterson
  • 2,377
  • 1
  • 21
  • 24
  • 1
    It's not really suitable for Scala due to the focus on Java SDK specific things, although with the massive amount of false positives (all in IMO) – sksamuel Aug 04 '14 at 10:54
17

There is now Scalastyle which does the job that Checkstyle does for Java. This includes not only formatting checks, but also some checks for known sources of bugs, such as a class which implements hashCode() but not equals.

There are currently about 40 checks, but we're adding them all of the time.

For more information, see www.scalastyle.org.

Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
16

There is some work going on in that direction. Some links:

There is also a discussion on scala mail list, archive available here.

George
  • 8,368
  • 12
  • 65
  • 106
8

Here is an updated answer as of August 2014 for some that are aimed or work well with Scala.

Personally I think the JVM or Java ones end up with far too many false positives, or have inspections that are aimed mostly at Java specific classes. For example, since in Scala we don't tend to use the Java Collections, all the findbugs collection based inspections are not needed. Another example is the inspections for use of static fields which are irrelevant in Scala.

sksamuel
  • 16,154
  • 8
  • 60
  • 108
7

Findbugs and other tools that are bytecode based will work, in the sense that they will find faults in your code. Unfortunately, the bytecode based approaches have been tuned against the output of the javac compilers, meaning they are likely to produce very high false positive rates, and miss basic issues, because Scala will be producing different idioms than the javac compiler.

Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
3

I'm having a lot of fun with Codacy (e.g. https://www.codacy.com/app/hejfelix/Frase/dashboard) for Open Source projects

Felix
  • 8,385
  • 10
  • 40
  • 59
1

There is a SBT plugin for PMD copy paste detector CPD.

https://github.com/sbt/cpd4sbt

Petteri H
  • 11,779
  • 12
  • 64
  • 94
1

scala copy paste detector, based on AST. Looking for copy-pasted subtrees/ASTs, and inform about it.

It's plugin for sbt.

https://github.com/ajtkulov/scala-cpd

Pavel Ajtkulov
  • 515
  • 7
  • 21
-2

I don't know much about Scala but if is Java compatible Klocwork's Solo product might work. You can find it here Klocwork Solo

user193749
  • 21
  • 1
  • 2
    why is this the accepted answer? I can't find any evidence that Klocwork 'solves' the problems you'ed have running Findbugs...? – p3t0r Feb 20 '10 at 20:27