Skip to content

Commit

Permalink
Prevent ConstructorLeaksThis false positive on constructor
Browse files Browse the repository at this point in the history
reference

Fixes #789

RELNOTES: Fixed false positive in ConstructorLeaksThis

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=185219776
  • Loading branch information
Stephan202 authored and cushon committed Feb 12, 2018
1 parent 0bcd7ee commit c2b37f4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
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,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));
}
}
Expand Down
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 c2b37f4

Please sign in to comment.