Skip to content

Commit

Permalink
Prevent ConstructorLeaksThis false positive on constructor reference
Browse files Browse the repository at this point in the history
Fixes #655.
  • Loading branch information
Stephan202 committed Oct 23, 2017
1 parent 319c505 commit 495b0be
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -83,7 +84,8 @@ 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.isConstructor() && identifier.contentEquals("this") && thisClass.equals(sym.owner)) {
state.reportMatch(describeMatch(node));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
}
}

0 comments on commit 495b0be

Please sign in to comment.