diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/ConstructorLeaksThis.java b/core/src/main/java/com/google/errorprone/bugpatterns/ConstructorLeaksThis.java index ca0b4aac0de..b2705bc4872 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/ConstructorLeaksThis.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/ConstructorLeaksThis.java @@ -29,6 +29,7 @@ import com.sun.source.tree.MemberSelectTree; import com.sun.source.tree.Tree; import com.sun.source.util.TreeScanner; +import com.sun.tools.javac.code.Symbol; import com.sun.tools.javac.code.Symbol.ClassSymbol; import javax.lang.model.element.Name; @@ -83,7 +84,11 @@ public Void visitAssignment(AssignmentTree node, Void unused) { private void checkForThis( ExpressionTree node, Name identifier, ClassSymbol thisClass, VisitorState state) { - if (identifier.contentEquals("this") && thisClass.equals(ASTHelpers.getSymbol(node).owner)) { + Symbol sym = ASTHelpers.getSymbol(node); + if (sym != null + && !sym.isConstructor() + && identifier.contentEquals("this") + && thisClass.equals(sym.owner)) { state.reportMatch(describeMatch(node)); } } diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/testdata/ConstructorLeaksThisNegativeCases.java b/core/src/test/java/com/google/errorprone/bugpatterns/testdata/ConstructorLeaksThisNegativeCases.java index 9782968463d..37a9d255741 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/testdata/ConstructorLeaksThisNegativeCases.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/testdata/ConstructorLeaksThisNegativeCases.java @@ -97,4 +97,13 @@ static class Inner { @SuppressWarnings("ConstructorLeaksThis") final FixtureController that = new FixtureController(this); } + + static final class WithTwoConstructors { + public WithTwoConstructors() { + // 'this' references another constructor, not an object + this(0); + } + + public WithTwoConstructors(int i) {} + } }