Pattern: toString()
returns null
Issue: -
Checks for toString()
methods that return null
. This is unconventional and could cause unexpected NullPointerExceptions
from normal or implicit use of toString()
.
Example of violations:
class SomeClass {
String toString() {
if (foo()) {
return 'SomeClass'
} else {
return null // violation
}
}
}
class SomeClass {
String toString() {
calculateStuff()
null // violation
}
}
class SomeClass {
String toString() { // violation - implicit return of null
}
}