IG: cleanup and test

This commit is contained in:
Bas Leijdekkers
2018-06-04 12:54:54 +02:00
parent 278f94cc30
commit 9e7c5df510
6 changed files with 64 additions and 29 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2018 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,14 +16,13 @@
package com.siyeh.ig.controlflow;
import com.intellij.psi.PsiConditionalExpression;
import com.intellij.psi.util.PsiTreeUtil;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.ControlFlowUtils;
import org.jetbrains.annotations.NotNull;
public class NestedConditionalExpressionInspection
extends BaseInspection {
public class NestedConditionalExpressionInspection extends BaseInspection {
@Override
@NotNull
@@ -44,18 +43,14 @@ public class NestedConditionalExpressionInspection
return new NestedConditionalExpressionVisitor();
}
private static class NestedConditionalExpressionVisitor
extends BaseInspectionVisitor {
private static class NestedConditionalExpressionVisitor extends BaseInspectionVisitor {
@Override
public void visitConditionalExpression(
PsiConditionalExpression expression) {
public void visitConditionalExpression(PsiConditionalExpression expression) {
super.visitConditionalExpression(expression);
if (PsiTreeUtil.getParentOfType(expression,
PsiConditionalExpression.class) == null) {
return;
if (ControlFlowUtils.isNestedElement(expression, PsiConditionalExpression.class)) {
registerError(expression);
}
registerError(expression);
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2018 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,7 +19,7 @@ import com.intellij.psi.PsiSwitchStatement;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.threading.NestedSynchronizedStatementInspection;
import com.siyeh.ig.psiutils.ControlFlowUtils;
import org.jetbrains.annotations.NotNull;
public class NestedSwitchStatementInspection extends BaseInspection {
@@ -48,7 +48,7 @@ public class NestedSwitchStatementInspection extends BaseInspection {
@Override
public void visitSwitchStatement(@NotNull PsiSwitchStatement statement) {
super.visitSwitchStatement(statement);
if (NestedSynchronizedStatementInspection.isNestedStatement(statement, PsiSwitchStatement.class)) {
if (ControlFlowUtils.isNestedElement(statement, PsiSwitchStatement.class)) {
registerStatementError(statement);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2018 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,7 +21,7 @@ import com.intellij.psi.util.PsiTreeUtil;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.threading.NestedSynchronizedStatementInspection;
import com.siyeh.ig.psiutils.ControlFlowUtils;
import org.jetbrains.annotations.NotNull;
public class NestedTryStatementInspection extends BaseInspection {
@@ -62,7 +62,7 @@ public class NestedTryStatementInspection extends BaseInspection {
if (!PsiTreeUtil.isAncestor(tryBlock, statement, true)) {
return;
}
if (NestedSynchronizedStatementInspection.isNestedStatement(statement, PsiTryStatement.class)) {
if (ControlFlowUtils.isNestedElement(statement, PsiTryStatement.class)) {
registerStatementError(statement);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2017 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2018 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -551,6 +551,10 @@ public class ControlFlowUtils {
return i == count;
}
public static <T extends PsiElement> boolean isNestedElement(@NotNull T element, @NotNull Class<T> aClass) {
return PsiTreeUtil.getParentOfType(element, aClass, true, PsiClass.class, PsiLambdaExpression.class) != null;
}
public static boolean isEmptyCodeBlock(PsiCodeBlock codeBlock) {
return hasStatementCount(codeBlock, 0);
}
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2018 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,14 +15,11 @@
*/
package com.siyeh.ig.threading;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiLambdaExpression;
import com.intellij.psi.PsiStatement;
import com.intellij.psi.PsiSynchronizedStatement;
import com.intellij.psi.util.PsiTreeUtil;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.ControlFlowUtils;
import org.jetbrains.annotations.NotNull;
public class NestedSynchronizedStatementInspection extends BaseInspection {
@@ -51,13 +48,9 @@ public class NestedSynchronizedStatementInspection extends BaseInspection {
@Override
public void visitSynchronizedStatement(@NotNull PsiSynchronizedStatement statement) {
super.visitSynchronizedStatement(statement);
if (isNestedStatement(statement, PsiSynchronizedStatement.class)) {
if (ControlFlowUtils.isNestedElement(statement, PsiSynchronizedStatement.class)) {
registerStatementError(statement);
}
}
}
public static <T extends PsiStatement> boolean isNestedStatement(@NotNull T statement, @NotNull Class<T> aClass) {
return PsiTreeUtil.getParentOfType(statement, aClass, true, PsiClass.class, PsiLambdaExpression.class) != null;
}
}
@@ -0,0 +1,43 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.siyeh.ig.controlflow;
import com.intellij.codeInspection.InspectionProfileEntry;
import com.intellij.testFramework.LightProjectDescriptor;
import com.siyeh.ig.LightInspectionTestCase;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author Bas Leijdekkers
*/
public class NestedConditionalExpressionInspectionTest extends LightInspectionTestCase {
public void testSimple() {
doTest("class X {" +
" boolean x(int i, int j) {" +
" return i == 0 ? true : /*Nested conditional expression 'j == 0 ? true : false'*/j == 0 ? true : false/**/;" +
" }" +
"}");
}
public void testLambda() {
doTest("import java.util.function.IntFunction;" +
"class X {" +
" private IntFunction<String> nullIfEmpty(String str) {\n" +
" return str == null ? null : (a) -> (str.isEmpty() ? null : str);\n" +
" }" +
"}");
}
@Nullable
@Override
protected InspectionProfileEntry getInspection() {
return new NestedConditionalExpressionInspection();
}
@NotNull
@Override
protected LightProjectDescriptor getProjectDescriptor() {
return JAVA_9;
}
}