Skip to content
schauder edited this page Aug 17, 2011 · 11 revisions

Feel free to add requests for new bug patterns here.#

unintentional procedure

In some cases it's possible to identify an accidentally left-off equals sign before a function body.

def sum(x: Int, y: Int) {
    x + y //why on earth would we calculate this value if not to return it?
}

unused private vals

This is dead code that should result in a warning.

unused bindings in pattern matching (maybe just a special case of the previous?)

{
    val (x,y,z) =triple
    x + z
}

unused private defs

This is dead code that should result in a warning.

semicolon inference puzzlers

def method = {
  val result = 3
    + 12
  result
}

overriden methods with different named paramters

trait Foo {
  def add(x : Int, y : Double) : Double
}
trait Bar extends Foo {
  def add(y : int, x : Double) : Double
}
val x : Bar = ...
x.add(y = 1, x = 2) // Eh?

Required Early Initialization

This one might need some kind of annotation on abstract values that require early initialization. If it's possible to detect solo, that'd rock.

trait Box {
  val height : Int  // This must be initialized 'early'
  val width : Int   // this must also be initialized 'early'
  val area = height * width   // This depends on height/width being available
  val perimeter = 2*(height + width)
}

val x = new Box with {
  val height = 1  // Error, not early enough
  val width = 2
}

val y = new {
  val height = 1  // WINNERS
  val width = 2
} with Box

Names that differ only in capitalization

implicit conversions, that wouldn't be needed with a minimal change in the code

I can't define it properly, but I'll give an example

def implicit toString(any : Any) : String { .... }

def takesString(s : String) ={...}

object Obj{
     def apply() : String = { ... }
}

takesString(Obj) // does an implicit conversion, possibly a bug

takesString(Obj()) // no implict conversion necessary, probably what was intended

assignement of Null to any value

checking for null

package declaration not matching the path of the source file

usage of java packages

Although Scala is mostly used on the JDK it is able to run on .NET which might fail when Java classes are used

here is whole list of bug patterns

http://stackoverflow.com/questions/1332574/common-programming-mistakes-for-scala-developers-to-avoid