IDEA-96785 ("Result of object allocation ignored": Allow suppression for certain classes)

This commit is contained in:
Bas Leijdekkers
2017-07-07 16:48:01 +02:00
parent 883c34ac86
commit 63cbf2cb9b
4 changed files with 31 additions and 9 deletions
@@ -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<String> 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);
@@ -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) {
@@ -8,6 +8,7 @@ public class ResultOfObjectAllocationIgnored {
public static void foo() {
new <warning descr="Result of 'new Integer()' is ignored">Integer</warning>(3);
new javax.swing.JFrame();
}
void boom() {
@@ -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;
}
}