redundant suppressions on the fly: check alternative ids, old suppress ids (IDEA-223759)

GitOrigin-RevId: 3d1332fd4cdb1bdd39c26de6713cf6f464fc61e3
This commit is contained in:
Anna Kozlova
2019-10-01 10:11:58 +00:00
committed by intellij-monorepo-bot
parent 991047113e
commit bb650d3c68
7 changed files with 63 additions and 12 deletions
@@ -0,0 +1,9 @@
// "Remove 'boxing' suppression" "true"
import java.util.ArrayList;
class Test {
void doSomething() {
final Integer number = Integer.valueOf(1);
System.out.println(number);
}
}
@@ -0,0 +1,9 @@
// "Remove 'CloneDoesntCallSuperClone' suppression" "true"
class NoSuperCall {
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
@@ -0,0 +1,10 @@
// "Remove 'boxing' suppression" "true"
import java.util.ArrayList;
class Test {
@SuppressWarnings( {"bo<caret>xing"})
void doSomething() {
final Integer number = Integer.valueOf(1);
System.out.println(number);
}
}
@@ -0,0 +1,10 @@
// "Remove 'CloneDoesntCallSuperClone' suppression" "true"
class NoSuperCall {
@SuppressWarnings("CloneDoesntCa<caret>llSuperClone")
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
@@ -9,6 +9,8 @@ import com.intellij.codeInspection.deadCode.UnusedDeclarationInspection;
import com.intellij.codeInspection.uncheckedWarnings.UncheckedWarningLocalInspection;
import com.intellij.psi.impl.source.tree.injected.MyTestInjector;
import com.siyeh.ig.controlflow.FallthruInSwitchStatementInspection;
import com.siyeh.ig.inheritance.RefusedBequestInspection;
import com.siyeh.ig.jdk.AutoBoxingInspection;
public class RemoveRedundantSuppressionTest extends LightQuickFixParameterizedTestCase {
@Override
@@ -20,7 +22,9 @@ public class RemoveRedundantSuppressionTest extends LightQuickFixParameterizedTe
new UncheckedWarningLocalInspection(),
new FallthruInSwitchStatementInspection(),
new UnusedDeclarationInspection(true),
new RedundantLambdaCodeBlockInspection());
new RedundantLambdaCodeBlockInspection(),
new AutoBoxingInspection(),
new RefusedBequestInspection());
}
@Override
@@ -55,7 +55,6 @@ import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @author max
@@ -224,7 +223,17 @@ public class LocalInspectionsPass extends ProgressableTextEditorHighlightingPass
if (toolWrappers.stream().anyMatch(LocalInspectionToolWrapper::runForWholeFile)) {
return;
}
Set<String> activeTools = toolWrappers.stream().filter(tool -> !tool.isUnfair()).map(tool -> tool.getID()).collect(Collectors.toSet());
Set<String> activeTools = new HashSet<>();
for (LocalInspectionToolWrapper tool : toolWrappers) {
if (!tool.isUnfair()) {
activeTools.add(tool.getID());
ContainerUtil.addIfNotNull(activeTools, tool.getAlternativeID());
InspectionElementsMerger elementsMerger = InspectionElementsMerger.getMerger(tool.getShortName());
if (elementsMerger != null) {
activeTools.addAll(Arrays.asList(elementsMerger.getSuppressIds()));
}
}
}
LocalInspectionTool
localTool = ((RedundantSuppressInspection)toolWrapper.getTool()).createLocalTool((RedundantSuppressionDetector)suppressor, mySuppressedElements, activeTools);
ProblemsHolder holder = new ProblemsHolder(iManager, getFile(), true);
@@ -108,17 +108,17 @@ public class RedundantSuppressInspection extends GlobalSimpleInspectionTool {
if (suppressedScopes.values().isEmpty()) return ProblemDescriptor.EMPTY_ARRAY;
// have to visit all file from scratch since inspections can be written in any pervasive way including checkFile() overriding
Map<InspectionToolWrapper, String> suppressedTools = new THashMap<>();
InspectionToolWrapper[] toolWrappers = getInspectionTools(psiElement, manager);
Map<InspectionToolWrapper<?, ?>, String> suppressedTools = new THashMap<>();
InspectionToolWrapper<?, ?>[] toolWrappers = getInspectionTools(psiElement, manager);
for (Collection<String> ids : suppressedScopes.values()) {
for (Iterator<String> iterator = ids.iterator(); iterator.hasNext(); ) {
String suppressId = iterator.next().trim();
List<InspectionToolWrapper> reportingWrappers = findReportingTools(toolWrappers, suppressId);
List<InspectionToolWrapper<?, ?>> reportingWrappers = findReportingTools(toolWrappers, suppressId);
if (reportingWrappers.isEmpty()) {
iterator.remove();
}
else {
for (InspectionToolWrapper toolWrapper : reportingWrappers) {
for (InspectionToolWrapper<?, ?> toolWrapper : reportingWrappers) {
suppressedTools.put(toolWrapper, suppressId);
}
}
@@ -135,7 +135,7 @@ public class RedundantSuppressInspection extends GlobalSimpleInspectionTool {
final List<ProblemDescriptor> result;
try {
result = new ArrayList<>();
for (InspectionToolWrapper toolWrapper : suppressedTools.keySet()) {
for (InspectionToolWrapper<?, ?> toolWrapper : suppressedTools.keySet()) {
String toolId = suppressedTools.get(toolWrapper);
toolWrapper.initialize(globalContext);
final Collection<CommonProblemDescriptor> descriptors;
@@ -203,10 +203,10 @@ public class RedundantSuppressInspection extends GlobalSimpleInspectionTool {
return result.toArray(ProblemDescriptor.EMPTY_ARRAY);
}
private static List<InspectionToolWrapper> findReportingTools(InspectionToolWrapper[] toolWrappers, String suppressedId) {
List<InspectionToolWrapper> wrappers = Collections.emptyList();
private static List<InspectionToolWrapper<?, ?>> findReportingTools(InspectionToolWrapper<?, ?>[] toolWrappers, String suppressedId) {
List<InspectionToolWrapper<?, ?>> wrappers = Collections.emptyList();
String mergedToolName = InspectionElementsMerger.getMergedToolName(suppressedId);
for (InspectionToolWrapper toolWrapper : toolWrappers) {
for (InspectionToolWrapper<?, ?> toolWrapper : toolWrappers) {
String toolWrapperShortName = toolWrapper.getShortName();
String alternativeID = toolWrapper.getTool().getAlternativeID();
if (toolWrapper instanceof LocalInspectionToolWrapper &&
@@ -275,7 +275,7 @@ public class RedundantSuppressInspection extends GlobalSimpleInspectionTool {
}
@NotNull
protected InspectionToolWrapper[] getInspectionTools(PsiElement psiElement, @NotNull InspectionManager manager) {
protected InspectionToolWrapper<?, ?>[] getInspectionTools(PsiElement psiElement, @NotNull InspectionManager manager) {
String currentProfileName = ((InspectionManagerBase)manager).getCurrentProfile();
InspectionProjectProfileManager profileManager = InspectionProjectProfileManager.getInstance(manager.getProject());
InspectionProfileImpl usedProfile = profileManager.getProfile(currentProfileName, false);