From 63cbf2cb9b7251f86ce90ac5f398fd4fbb19a38e Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Fri, 7 Jul 2017 15:48:01 +0200 Subject: [PATCH 1/2] IDEA-96785 ("Result of object allocation ignored": Allow suppression for certain classes) --- ...ObjectAllocationIgnoredInspectionBase.java | 24 +++++++++++++------ ...ltOfObjectAllocationIgnoredInspection.java | 11 ++++++++- .../ResultOfObjectAllocationIgnored.java | 1 + ...ObjectAllocationIgnoredInspectionTest.java | 4 +++- 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/ResultOfObjectAllocationIgnoredInspectionBase.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/ResultOfObjectAllocationIgnoredInspectionBase.java index 6471277153c9..0ee937b2e43d 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/ResultOfObjectAllocationIgnoredInspectionBase.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/ResultOfObjectAllocationIgnoredInspectionBase.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2015 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2017 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,9 +15,8 @@ */ package com.siyeh.ig.bugs; -import com.intellij.psi.PsiExpression; -import com.intellij.psi.PsiExpressionStatement; -import com.intellij.psi.PsiNewExpression; +import com.intellij.psi.*; +import com.intellij.util.containers.OrderedSet; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; @@ -25,6 +24,8 @@ import org.jetbrains.annotations.NotNull; public class ResultOfObjectAllocationIgnoredInspectionBase extends BaseInspection { + @SuppressWarnings("PublicField") public OrderedSet ignoredClasses = new OrderedSet<>(); + @Override @NotNull public String getDisplayName() { @@ -42,7 +43,7 @@ public class ResultOfObjectAllocationIgnoredInspectionBase extends BaseInspectio return new ResultOfObjectAllocationIgnoredVisitor(); } - private static class ResultOfObjectAllocationIgnoredVisitor extends BaseInspectionVisitor { + private class ResultOfObjectAllocationIgnoredVisitor extends BaseInspectionVisitor { @Override public void visitExpressionStatement(@NotNull PsiExpressionStatement statement) { @@ -53,10 +54,19 @@ public class ResultOfObjectAllocationIgnoredInspectionBase extends BaseInspectio } final PsiNewExpression newExpression = (PsiNewExpression)expression; final PsiExpression[] arrayDimensions = newExpression.getArrayDimensions(); - if (arrayDimensions.length != 0) { + if (arrayDimensions.length != 0 || newExpression.getArrayInitializer() != null) { return; } - if (newExpression.getArrayInitializer() != null) { + final PsiJavaCodeReferenceElement reference = newExpression.getClassOrAnonymousClassReference(); + if (reference == null) { + return; + } + final PsiElement target = reference.resolve(); + if (!(target instanceof PsiClass)) { + return; + } + final PsiClass aClass = (PsiClass)target; + if (!(expression instanceof PsiAnonymousClass) && ignoredClasses.contains(aClass.getQualifiedName())) { return; } registerNewExpressionError(newExpression, newExpression); diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/ResultOfObjectAllocationIgnoredInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/ResultOfObjectAllocationIgnoredInspection.java index d31cd55a8e34..a3730f539319 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/ResultOfObjectAllocationIgnoredInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/ResultOfObjectAllocationIgnoredInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2017 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,13 +18,22 @@ package com.siyeh.ig.bugs; import com.intellij.psi.PsiElement; import com.siyeh.ig.InspectionGadgetsFix; import com.siyeh.ig.fixes.SuppressForTestsScopeFix; +import com.siyeh.ig.ui.UiUtils; import org.jetbrains.annotations.Nullable; +import javax.swing.*; + /** * @author Bas Leijdekkers */ public class ResultOfObjectAllocationIgnoredInspection extends ResultOfObjectAllocationIgnoredInspectionBase { + @Nullable + @Override + public JComponent createOptionsPanel() { + return UiUtils.createTreeClassChooserList(ignoredClasses, "Ignored classes", "Choose class for which object allocation can be ignored"); + } + @Nullable @Override protected InspectionGadgetsFix buildFix(Object... infos) { diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/result_of_object_allocation_ignored/ResultOfObjectAllocationIgnored.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/result_of_object_allocation_ignored/ResultOfObjectAllocationIgnored.java index c5c38cbdb5ed..41132335a44d 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/result_of_object_allocation_ignored/ResultOfObjectAllocationIgnored.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/result_of_object_allocation_ignored/ResultOfObjectAllocationIgnored.java @@ -8,6 +8,7 @@ public class ResultOfObjectAllocationIgnored { public static void foo() { new Integer(3); + new javax.swing.JFrame(); } void boom() { diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/ResultOfObjectAllocationIgnoredInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/ResultOfObjectAllocationIgnoredInspectionTest.java index 5f18f1e7a86a..7c21cddec555 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/ResultOfObjectAllocationIgnoredInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/ResultOfObjectAllocationIgnoredInspectionTest.java @@ -31,6 +31,8 @@ public class ResultOfObjectAllocationIgnoredInspectionTest extends LightInspecti @Nullable @Override protected InspectionProfileEntry getInspection() { - return new ResultOfObjectAllocationIgnoredInspection(); + final ResultOfObjectAllocationIgnoredInspection inspection = new ResultOfObjectAllocationIgnoredInspection(); + inspection.ignoredClasses.add("javax.swing.JFrame"); + return inspection; } } \ No newline at end of file From f72be8efae211bfeccb796f97f7e277c6f1caf1d Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Fri, 7 Jul 2017 16:44:22 +0200 Subject: [PATCH 2/2] IDEA-175486 (AssertionError in PackageNamingConventionInspection) --- .../ig/naming/PackageNamingConventionInspection.java | 9 +++------ .../ig/naming/PackageNamingConventionInspectionTest.java | 5 +++++ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/naming/PackageNamingConventionInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/naming/PackageNamingConventionInspection.java index 7bad29330828..ce8e67f88f4f 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/naming/PackageNamingConventionInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/naming/PackageNamingConventionInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2017 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,7 +26,6 @@ import com.intellij.codeInspection.ui.ConventionOptionsPanel; import com.intellij.openapi.util.InvalidDataException; import com.intellij.psi.PsiJavaCodeReferenceElement; import com.intellij.psi.PsiPackageStatement; -import com.siyeh.HardcodedMethodConstants; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseGlobalInspection; import com.siyeh.ig.BaseInspectionVisitor; @@ -120,13 +119,11 @@ public class PackageNamingConventionInspection extends BaseGlobalInspection { if (m_maxLength > 0 && length > m_maxLength) { return false; } - if (HardcodedMethodConstants.SERIAL_VERSION_UID.equals(name)) { - return true; - } final Matcher matcher = m_regexPattern.matcher(name); return matcher.matches(); } + @Override @Nullable public LocalInspectionTool getSharedLocalInspectionTool() { return new LocalPackageNamingConventionInspection(this); @@ -176,7 +173,7 @@ public class PackageNamingConventionInspection extends BaseGlobalInspection { index = text.indexOf('.', start); } final String lastName = text.substring(start); - if (!mySettingsDelegate.isValid(lastName)) { + if (!lastName.isEmpty() && !mySettingsDelegate.isValid(lastName)) { registerErrorAtOffset(reference, start, lastName.length(), lastName); } } diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/naming/PackageNamingConventionInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/naming/PackageNamingConventionInspectionTest.java index 266d5f9dd116..b5d2848d707c 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/naming/PackageNamingConventionInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/naming/PackageNamingConventionInspectionTest.java @@ -28,6 +28,11 @@ public class PackageNamingConventionInspectionTest extends LightInspectionTestCa "class X {}"); } + public void testNoAssertion() { + doTest("package /*Package name 'a' is too short*/a/**/./*!Identifier expected*/;/*!*/" + + "class X {}"); + } + @Override protected InspectionProfileEntry getInspection() { return new PackageNamingConventionInspection();