Skip to content

Files

Latest commit

 

History

History
37 lines (28 loc) · 812 Bytes

ToStringReturnsNull.md

File metadata and controls

37 lines (28 loc) · 812 Bytes

Pattern: toString() returns null

Issue: -

Description

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
    }
}

Further Reading