diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/NestedConditionalExpressionInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/NestedConditionalExpressionInspection.java index baa06004503e..1249ae3cfd6c 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/NestedConditionalExpressionInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/NestedConditionalExpressionInspection.java @@ -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); } } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/NestedSwitchStatementInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/NestedSwitchStatementInspection.java index 4cff1d66ebea..3174e0b3b5a4 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/NestedSwitchStatementInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/NestedSwitchStatementInspection.java @@ -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); } } diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/errorhandling/NestedTryStatementInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/errorhandling/NestedTryStatementInspection.java index a32b48f85bc3..cbe1de049894 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/errorhandling/NestedTryStatementInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/errorhandling/NestedTryStatementInspection.java @@ -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); } } diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ControlFlowUtils.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ControlFlowUtils.java index 196af5aa0f18..8a769e71d7e0 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ControlFlowUtils.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ControlFlowUtils.java @@ -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 boolean isNestedElement(@NotNull T element, @NotNull Class aClass) { + return PsiTreeUtil.getParentOfType(element, aClass, true, PsiClass.class, PsiLambdaExpression.class) != null; + } + public static boolean isEmptyCodeBlock(PsiCodeBlock codeBlock) { return hasStatementCount(codeBlock, 0); } diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/threading/NestedSynchronizedStatementInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/threading/NestedSynchronizedStatementInspection.java index 5a76db14ad7f..ff9dcf8c4984 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/threading/NestedSynchronizedStatementInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/threading/NestedSynchronizedStatementInspection.java @@ -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 boolean isNestedStatement(@NotNull T statement, @NotNull Class aClass) { - return PsiTreeUtil.getParentOfType(statement, aClass, true, PsiClass.class, PsiLambdaExpression.class) != null; - } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/NestedConditionalExpressionInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/NestedConditionalExpressionInspectionTest.java new file mode 100644 index 000000000000..e1666a73341a --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/NestedConditionalExpressionInspectionTest.java @@ -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 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; + } +} \ No newline at end of file