Skip to content
This repository has been archived by the owner on Sep 22, 2022. It is now read-only.

Commit

Permalink
Issue checkstyle#4940: fix MultipleReturnPointsPerMethod idea violation
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimfadora committed Aug 19, 2017
1 parent 29901f7 commit cbfb587
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 44 deletions.
8 changes: 5 additions & 3 deletions config/intellij-idea-inspections.xml
Expand Up @@ -1487,9 +1487,9 @@ isolated classes and we cannot put them to separate package as it will affect us
<inspection_tool class="MultipleInjectedConstructorsForClass" enabled="true" level="ERROR" enabled_by_default="true" />
<inspection_tool class="MultipleMethodDesignatorsInspection" enabled="true" level="ERROR" enabled_by_default="true" />
<inspection_tool class="MultipleRepositoryUrls" enabled="true" level="ERROR" enabled_by_default="true" />
<inspection_tool class="MultipleReturnPointsPerMethod" enabled="false" level="ERROR" enabled_by_default="false">
<option name="ignoreGuardClauses" value="false" />
<option name="ignoreEqualsMethod" value="false" />
<inspection_tool class="MultipleReturnPointsPerMethod" enabled="true" level="ERROR" enabled_by_default="true">
<option name="ignoreGuardClauses" value="true" />
<option name="ignoreEqualsMethod" value="true" />
<option name="m_limit" value="1" />
</inspection_tool>
<inspection_tool class="MultipleTopLevelClassesInFile" enabled="true" level="WARNING" enabled_by_default="true" />
Expand Down Expand Up @@ -2149,6 +2149,8 @@ isolated classes and we cannot put them to separate package as it will affect us
<option value="UseOfPropertiesAsHashtable" />
<!-- till #4861, #4862, #4863, #4864, #4866 -->
<option value="ThisEscapedInObjectConstruction" />
<!-- it will makes code too complicated in some cases -->
<option value="MultipleReturnPointsPerMethod" />
</list>
</option>
</inspection_tool>
Expand Down
Expand Up @@ -487,7 +487,8 @@ private static String replaceProperties(
fragment = props.resolve(propertyName);
if (fragment == null) {
if (defaultValue != null) {
return defaultValue;
sb.replace(0, sb.length(), defaultValue);
break;
}
throw new CheckstyleException(
"Property ${" + propertyName + "} has not been set");
Expand Down
Expand Up @@ -154,11 +154,7 @@ private boolean isSameNameMethod(DetailAST ast) {
&& sibling.getType() == TokenTypes.TYPE_ARGUMENTS) {
sibling = sibling.getNextSibling();
}
if (sibling == null) {
return true;
}
final String name = sibling.getText();
return !getMethodName().equals(name);
return sibling == null || !getMethodName().equals(sibling.getText());
}

@Override
Expand Down
Expand Up @@ -605,12 +605,14 @@ public void addMethodCall(DetailAST methodCall) {
* @return true if this FieldFrame contains instance field field.
*/
public DetailAST findField(String name) {
DetailAST resultField = null;
for (DetailAST field: fields) {
if (getFieldName(field).equals(name)) {
return field;
resultField = field;
break;
}
}
return null;
return resultField;
}

/**
Expand Down
Expand Up @@ -192,16 +192,18 @@ private static boolean isNumericType(int type) {
*/
private static boolean isZero(DetailAST expr) {
final int type = expr.getType();
final boolean isZero;
switch (type) {
case TokenTypes.NUM_FLOAT:
case TokenTypes.NUM_DOUBLE:
case TokenTypes.NUM_INT:
case TokenTypes.NUM_LONG:
final String text = expr.getText();
return Double.compare(
CheckUtils.parseDouble(text, type), 0.0) == 0;
isZero = Double.compare(CheckUtils.parseDouble(text, type), 0.0) == 0;
break;
default:
return false;
isZero = false;
}
return isZero;
}
}
Expand Up @@ -371,19 +371,15 @@ private boolean hasFallThroughComment(DetailAST currentCase, DetailAST nextCase)
* @param lineNo The line number in the file.
* @return True if a match was found inside a comment.
*/
private boolean matchesComment(Pattern pattern, String line, int lineNo
) {
private boolean matchesComment(Pattern pattern, String line, int lineNo) {
final Matcher matcher = pattern.matcher(line);
boolean matches = false;

final boolean hit = matcher.find();

if (hit) {
final int startMatch = matcher.start();
if (matcher.find()) {
// -1 because it returns the char position beyond the match
final int endMatch = matcher.end() - 1;
return getFileContents().hasIntersectionWithComment(lineNo,
startMatch, lineNo, endMatch);
matches = getFileContents().hasIntersectionWithComment(lineNo, matcher.start(),
lineNo, matcher.end() - 1);
}
return false;
return matches;
}
}
Expand Up @@ -402,16 +402,17 @@ else if (parent.getType() == TokenTypes.METHOD_DEF
* ignoreSetter is true and ast is the parameter of a setter method.
*/
private boolean isIgnoredSetterParam(DetailAST ast, String name) {
boolean isIgnoredSetterParam = false;
if (ignoreSetter && ast.getType() == TokenTypes.PARAMETER_DEF) {
final DetailAST parametersAST = ast.getParent();
final DetailAST methodAST = parametersAST.getParent();
if (parametersAST.getChildCount() == 1
&& methodAST.getType() == TokenTypes.METHOD_DEF
&& isSetterMethod(methodAST, name)) {
return true;
isIgnoredSetterParam = true;
}
}
return false;
return isIgnoredSetterParam;
}

/**
Expand Down Expand Up @@ -653,13 +654,15 @@ public FieldFrame getParent() {
*/
private boolean isEmbeddedIn(String classOrEnumName) {
FieldFrame currentFrame = this;
boolean isEmbeddedIn = false;
while (currentFrame != null) {
if (Objects.equals(currentFrame.frameName, classOrEnumName)) {
return true;
isEmbeddedIn = true;
break;
}
currentFrame = currentFrame.parent;
}
return false;
return isEmbeddedIn;
}
}
}
Expand Up @@ -326,6 +326,7 @@ private boolean isSamePackage(String className) {
* @return true if type is standard
*/
private boolean isStandardClass(String className, String illegal) {
boolean isStandardCalss = false;
// class from java.lang
if (illegal.length() - JAVA_LANG.length() == className.length()
&& illegal.endsWith(className)
Expand All @@ -340,10 +341,10 @@ private boolean isStandardClass(String className, String illegal) {
final boolean isSamePackage = isSamePackage(className);

if (!isSameFile && !isSamePackage) {
return true;
isStandardCalss = true;
}
}
return false;
return isStandardCalss;
}

/**
Expand Down
Expand Up @@ -152,15 +152,17 @@ public void visitToken(DetailAST ast) {
* token type in {@link #ignoreOccurrenceContext}.
*/
private boolean isInIgnoreOccurrenceContext(DetailAST ast) {
boolean isInIgnoreOccurrenceContext = false;
for (DetailAST token = ast;
token.getParent() != null;
token = token.getParent()) {
final int type = token.getType();
if (ignoreOccurrenceContext.get(type)) {
return true;
isInIgnoreOccurrenceContext = true;
break;
}
}
return false;
return isInIgnoreOccurrenceContext;
}

@Override
Expand Down
Expand Up @@ -45,14 +45,16 @@ public ArrayInitHandler(IndentationCheck indentCheck,
protected IndentLevel getIndentImpl() {
final DetailAST parentAST = getMainAst().getParent();
final int type = parentAST.getType();
final IndentLevel indentLevel;
if (type == TokenTypes.LITERAL_NEW || type == TokenTypes.ASSIGN) {
// note: assumes new or assignment is line to align with
return new IndentLevel(getLineStart(parentAST));
indentLevel = new IndentLevel(getLineStart(parentAST));
}
else {
// at this point getParent() is instance of BlockParentHandler
return ((BlockParentHandler) getParent()).getChildrenExpectedIndent();
indentLevel = ((BlockParentHandler) getParent()).getChildrenExpectedIndent();
}
return indentLevel;
}

@Override
Expand Down Expand Up @@ -126,11 +128,9 @@ private int getNextFirstNonBlankOnLineAfter(int lineNo, int columnNo) {
}

if (realColumnNo == lineLength) {
return -1;
}
else {
return realColumnNo;
realColumnNo = -1;
}
return realColumnNo;
}

/**
Expand Down
Expand Up @@ -59,12 +59,11 @@ public enum LineWrappingOptions {
* @noinspection BooleanParameter
*/
public static LineWrappingOptions ofBoolean(boolean val) {
LineWrappingOptions option = NONE;
if (val) {
return IGNORE_FIRST_LINE;
}
else {
return NONE;
option = IGNORE_FIRST_LINE;
}
return option;
}
}

Expand Down
Expand Up @@ -342,6 +342,7 @@ else if (index > 0 && builder.charAt(index) == '/'
* @param ast the node with the Javadoc
* @param comment the {@code TextBlock} which represents
* the Javadoc comment.
* @noinspection MethodWithMultipleReturnPoints
*/
// -@cs[ReturnCount] Too complex to break apart.
private void checkHtmlTags(final DetailAST ast, final TextBlock comment) {
Expand Down
Expand Up @@ -136,12 +136,14 @@ private static boolean isCommentConsistent(IndentComment comment) {
}

private static int getLineStart(String line, final int tabWidth) {
int lineStart = 0;
for (int index = 0; index < line.length(); ++index) {
if (!Character.isWhitespace(line.charAt(index))) {
return CommonUtils.lengthExpandedTabs(line, index, tabWidth);
lineStart = CommonUtils.lengthExpandedTabs(line, index, tabWidth);
break;
}
}
return 0;
return lineStart;
}

private void verifyWarns(Configuration config, String filePath,
Expand Down

0 comments on commit cbfb587

Please sign in to comment.