Invert if statements with negated conditions. #1555
Fixes `NegatedIfElse` inspection violations. Description: >Reports if statements which contain else branches and whose conditions are negated. Flipping the order of the if and else branches will usually increase the clarity of such statements.
This commit is contained in:
parent
d0939d3bc9
commit
86826e1fa3
|
|
@ -82,20 +82,20 @@ public final class Main {
|
|||
// return error is smth is wrong in arguments
|
||||
final List<String> messages = validateCli(commandLine);
|
||||
cliViolations = !messages.isEmpty();
|
||||
if (!cliViolations) {
|
||||
// create config helper object
|
||||
final CliOptions config = convertCliToPojo(commandLine);
|
||||
// run Checker
|
||||
errorCounter = runCheckstyle(config);
|
||||
exitStatus = errorCounter;
|
||||
}
|
||||
else {
|
||||
if (cliViolations) {
|
||||
exitStatus = exitWithCliViolation;
|
||||
errorCounter = 1;
|
||||
for (String message : messages) {
|
||||
System.out.println(message);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// create config helper object
|
||||
final CliOptions config = convertCliToPojo(commandLine);
|
||||
// run Checker
|
||||
errorCounter = runCheckstyle(config);
|
||||
exitStatus = errorCounter;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (ParseException pex) {
|
||||
|
|
@ -147,10 +147,7 @@ public final class Main {
|
|||
private static List<String> validateCli(CommandLine cmdLine) {
|
||||
final List<String> result = new ArrayList<>();
|
||||
// ensure a configuration file is specified
|
||||
if (!cmdLine.hasOption("c")) {
|
||||
result.add("Must specify a config XML file.");
|
||||
}
|
||||
else {
|
||||
if (cmdLine.hasOption("c")) {
|
||||
// validate optional parameters
|
||||
if (cmdLine.hasOption("f")) {
|
||||
final String format = cmdLine.getOptionValue("f");
|
||||
|
|
@ -178,6 +175,9 @@ public final class Main {
|
|||
result.add("Must specify files to process, found 0.");
|
||||
}
|
||||
}
|
||||
else {
|
||||
result.add("Must specify a config XML file.");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -250,7 +250,10 @@ public final class TreeWalker
|
|||
throws CheckstyleException {
|
||||
final int[] tokens;
|
||||
final Set<String> checkTokens = check.getTokenNames();
|
||||
if (!checkTokens.isEmpty()) {
|
||||
if (checkTokens.isEmpty()) {
|
||||
tokens = check.getDefaultTokens();
|
||||
}
|
||||
else {
|
||||
tokens = check.getRequiredTokens();
|
||||
|
||||
//register configured tokens
|
||||
|
|
@ -268,9 +271,6 @@ public final class TreeWalker
|
|||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
tokens = check.getDefaultTokens();
|
||||
}
|
||||
for (int element : tokens) {
|
||||
registerCheck(element, check);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -74,7 +74,14 @@ public class OuterTypeFilenameCheck extends Check {
|
|||
@Override
|
||||
public void visitToken(DetailAST ast) {
|
||||
final String outerTypeName = ast.findFirstToken(TokenTypes.IDENT).getText();
|
||||
if (!seenFirstToken) {
|
||||
if (seenFirstToken) {
|
||||
final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
|
||||
if (modifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null
|
||||
&& ast.getParent() == null) {
|
||||
hasPublic = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
if (fileName.equals(outerTypeName)) {
|
||||
validFirst = true;
|
||||
|
|
@ -83,13 +90,6 @@ public class OuterTypeFilenameCheck extends Check {
|
|||
wrongType = ast;
|
||||
}
|
||||
}
|
||||
else {
|
||||
final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
|
||||
if (modifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null
|
||||
&& ast.getParent() == null) {
|
||||
hasPublic = true;
|
||||
}
|
||||
}
|
||||
seenFirstToken = true;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -142,11 +142,11 @@ public class FallThroughCheck extends Check {
|
|||
|
||||
if (slist != null && !isTerminated(slist, true, true)
|
||||
&& !hasFallTruComment(ast, nextGroup)) {
|
||||
if (!isLastGroup) {
|
||||
log(nextGroup, MSG_FALL_THROUGH);
|
||||
if (isLastGroup) {
|
||||
log(ast, MSG_FALL_THROUGH_LAST);
|
||||
}
|
||||
else {
|
||||
log(ast, MSG_FALL_THROUGH_LAST);
|
||||
log(nextGroup, MSG_FALL_THROUGH);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -243,12 +243,12 @@ public class InnerAssignmentCheck
|
|||
for (int anElement : element) {
|
||||
current = current.getParent();
|
||||
final int expectedType = anElement;
|
||||
if (current.getType() != expectedType) {
|
||||
found = false;
|
||||
break;
|
||||
if (current.getType() == expectedType) {
|
||||
found = true;
|
||||
}
|
||||
else {
|
||||
found = true;
|
||||
found = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -342,11 +342,11 @@ public class VariableDeclarationUsageDistanceCheck extends Check {
|
|||
}
|
||||
// differs from previous instance
|
||||
else if (!instanceName.equals(initInstanceName)) {
|
||||
if (!initInstanceName.isEmpty()) {
|
||||
result = false;
|
||||
if (initInstanceName.isEmpty()) {
|
||||
initInstanceName = instanceName;
|
||||
}
|
||||
else {
|
||||
initInstanceName = instanceName;
|
||||
result = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,11 +44,11 @@ public class MemberDefHandler extends AbstractExpressionHandler {
|
|||
@Override
|
||||
public void checkIndentation() {
|
||||
final DetailAST modifiersNode = getMainAst().findFirstToken(TokenTypes.MODIFIERS);
|
||||
if (modifiersNode.getChildCount() != 0) {
|
||||
checkModifiers();
|
||||
if (modifiersNode.getChildCount() == 0) {
|
||||
checkType();
|
||||
}
|
||||
else {
|
||||
checkType();
|
||||
checkModifiers();
|
||||
}
|
||||
final LineWrappingHandler lineWrap =
|
||||
new LineWrappingHandler(getIndentCheck(), getMainAst(),
|
||||
|
|
|
|||
|
|
@ -365,13 +365,7 @@ public class JavadocStyleCheck
|
|||
//do nothing
|
||||
continue;
|
||||
}
|
||||
if (!tag.isCloseTag()) {
|
||||
//We only push html tags that are allowed
|
||||
if (isAllowedTag(tag)) {
|
||||
htmlStack.push(tag);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (tag.isCloseTag()) {
|
||||
// We have found a close tag.
|
||||
if (isExtraHtml(tag.getId(), htmlStack)) {
|
||||
// No corresponding open tag was found on the stack.
|
||||
|
|
@ -386,6 +380,12 @@ public class JavadocStyleCheck
|
|||
checkUnclosedTags(htmlStack, tag.getId());
|
||||
}
|
||||
}
|
||||
else {
|
||||
//We only push html tags that are allowed
|
||||
if (isAllowedTag(tag)) {
|
||||
htmlStack.push(tag);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Identify any tags left on the stack.
|
||||
|
|
|
|||
|
|
@ -130,14 +130,14 @@ public final class JavadocUtils {
|
|||
final Matcher commentMatcher = commentPattern.matcher(s);
|
||||
final String commentContents;
|
||||
final int commentOffset; // offset including comment characters
|
||||
if (!commentMatcher.find()) {
|
||||
commentContents = s; // No leading asterisks, still valid
|
||||
commentOffset = 0;
|
||||
}
|
||||
else {
|
||||
if (commentMatcher.find()) {
|
||||
commentContents = commentMatcher.group(1);
|
||||
commentOffset = commentMatcher.start(1) - 1;
|
||||
}
|
||||
else {
|
||||
commentContents = s; // No leading asterisks, still valid
|
||||
commentOffset = 0;
|
||||
}
|
||||
final Pattern tagPattern = Pattern.compile(".*?\\{@(\\p{Alpha}+)\\s+(.*?)\\}");
|
||||
final Matcher tagMatcher = tagPattern.matcher(commentContents);
|
||||
while (tagMatcher.find()) {
|
||||
|
|
|
|||
|
|
@ -79,14 +79,14 @@ class MultilineDetector {
|
|||
this.text = new FileText(text);
|
||||
resetState();
|
||||
|
||||
if (!Strings.isNullOrEmpty(options.getFormat())) {
|
||||
if (Strings.isNullOrEmpty(options.getFormat())) {
|
||||
options.getReporter().log(0, EMPTY);
|
||||
}
|
||||
else {
|
||||
matcher = options.getPattern().matcher(text.getFullText());
|
||||
findMatch();
|
||||
finish();
|
||||
}
|
||||
else {
|
||||
options.getReporter().log(0, EMPTY);
|
||||
}
|
||||
}
|
||||
|
||||
/** Method that finds the matches. */
|
||||
|
|
|
|||
Loading…
Reference in New Issue