Issue #3334: fixed RequireThis when can't find end block token (#3336)

This commit is contained in:
rnveach 2016-07-05 12:56:02 -04:00 committed by Roman Ivanov
parent a2117301de
commit 0a85131b14
3 changed files with 37 additions and 8 deletions

View File

@ -481,15 +481,19 @@ public class RequireThisCheck extends AbstractCheck {
final DetailAST blockStartToken = definitionToken.findFirstToken(TokenTypes.SLIST);
final DetailAST blockEndToken = getBlockEndToken(blockFrameNameIdent, blockStartToken);
final Set<DetailAST> variableUsagesInsideBlock =
getAllTokensWhichAreEqualToCurrent(definitionToken, ident, blockEndToken.getLineNo());
boolean userDefinedArrangementOfThis = false;
for (DetailAST variableUsage : variableUsagesInsideBlock) {
final DetailAST prevSibling = variableUsage.getPreviousSibling();
if (prevSibling != null
&& prevSibling.getType() == TokenTypes.LITERAL_THIS) {
userDefinedArrangementOfThis = true;
if (blockEndToken != null) {
final Set<DetailAST> variableUsagesInsideBlock =
getAllTokensWhichAreEqualToCurrent(definitionToken, ident,
blockEndToken.getLineNo());
for (DetailAST variableUsage : variableUsagesInsideBlock) {
final DetailAST prevSibling = variableUsage.getPreviousSibling();
if (prevSibling != null
&& prevSibling.getType() == TokenTypes.LITERAL_THIS) {
userDefinedArrangementOfThis = true;
}
}
}
return userDefinedArrangementOfThis;

View File

@ -247,4 +247,12 @@ public class RequireThisCheckTest extends BaseCheckTestSupport {
final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
verify(checkConfig, getPath("InputRequireThisReceiver.java"), expected);
}
@Test
public void testBraceAlone() throws Exception {
final DefaultConfiguration checkConfig = createCheckConfig(RequireThisCheck.class);
checkConfig.addAttribute("validateOnlyOverlapping", "false");
final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
verify(checkConfig, getPath("InputRequireThisBraceAlone.java"), expected);
}
}

View File

@ -0,0 +1,17 @@
package com.puppycrawl.tools.checkstyle.checks.coding;
public final class InputRequireThisBraceAlone {
protected void test() throws Exception {
{
boolean var1 = false;
var1 = true;
}
{
boolean var2 = false;
var2 = true;
}
}
}