java: fix global unused declaration inspection suppression (IDEA-256471)

GitOrigin-RevId: 5764b1db0c2d1acd5fb4574af04a3510ccb26448
This commit is contained in:
Bas Leijdekkers
2024-04-25 13:49:38 +00:00
committed by intellij-monorepo-bot
parent a5ab84ef3d
commit 5a7449d17c
12 changed files with 73 additions and 9 deletions
@@ -175,10 +175,8 @@ public class UnusedDeclarationInspectionBase extends GlobalInspectionTool {
if (!refElement.isSuspicious()) return;
PsiFile file = refElement.getContainingFile();
if (file == null) return;
boolean isSuppressed = refElement.isSuppressed(getShortName(), ALTERNATIVE_ID);
if (isSuppressed || !((GlobalInspectionContextBase)globalContext).isToCheckFile(file, UnusedDeclarationInspectionBase.this) && !scope.contains(file)) {
if (!((GlobalInspectionContextBase)globalContext).isToCheckFile(file, UnusedDeclarationInspectionBase.this) && !scope.contains(file)) {
getEntryPointsManager(globalContext).addEntryPoint(refElement, false);
}
}
@@ -65,6 +65,7 @@ import java.util.function.Supplier;
import java.util.stream.Collectors;
public class UnusedDeclarationPresentation extends DefaultInspectionToolPresentation {
private static final String[] SUPPRESSIONS = {UnusedDeclarationInspectionBase.SHORT_NAME, UnusedDeclarationInspectionBase.ALTERNATIVE_ID};
private final Map<RefEntity, UnusedDeclarationHint> myFixedElements =
ConcurrentCollectionFactory.createConcurrentIdentityMap();
private final Set<RefEntity> myExcludedElements = ConcurrentCollectionFactory.createConcurrentIdentitySet();
@@ -430,14 +431,14 @@ public class UnusedDeclarationPresentation extends DefaultInspectionToolPresenta
}
private @Nullable RefJavaElement refineElement(RefEntity refEntity) {
if (!(refEntity instanceof RefJavaElement refElement)) return null; //dead code doesn't work with refModule | refPackage
if (!(refEntity instanceof RefJavaElement refElement)) return null; // dead code doesn't work with refModule | refPackage
RefFilter filter = getFilter();
if (!filter.accepts(refElement)) return null;
RefJavaElement refinedElement = (RefJavaElement)getRefManager().getRefinedElement(refEntity);
if (refinedElement != refEntity && filter.accepts(refinedElement)) return null; // prevent duplicate reporting
if (!refinedElement.isValid()) return null;
if (!compareVisibilities(refinedElement, getTool().getSharedLocalInspectionTool())) return null;
if (isSuppressed(refinedElement)) return null;
if (isSuppressed(refinedElement) || refinedElement.isSuppressed(SUPPRESSIONS)) return null;
if (!ApplicationManager.getApplication().isHeadlessEnvironment() & getContext().getUIOptions().FILTER_RESOLVED_ITEMS &&
(myFixedElements.containsKey(refinedElement) || isExcluded(refinedElement))) {
return null;
@@ -1,4 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problems>
<problem>
<file>B.java</file>
<line>2</line>
<offset>2</offset>
<length>1</length>
<highlighted_element>B</highlighted_element>
<description>All constructor usages belong to the calls chain that has no members reachable from entry points.</description>
</problem>
</problems>
@@ -1,4 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problems>
<problem>
<file>B.java</file>
<line>1</line>
<offset>6</offset>
<length>1</length>
<description>Class has one instantiation, but it is not reachable from entry points.</description>
</problem>
</problems>
@@ -1,4 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>A.java</file>
<line>1</line>
<offset>22</offset>
<length>1</length>
<description>&amp;nbsp;Abstract&amp;nbsp;class has an implementation but &lt;ul&gt;&lt;li&gt;it is never instantiated OR&lt;/li&gt;&lt;li&gt;no instantiations are reachable from entry points.&lt;/li&gt;&lt;/ul&gt;</description>
</problem>
<problem>
<file>B.java</file>
<line>1</line>
<offset>13</offset>
<length>1</length>
<description>Class has one instantiation, but it is not reachable from entry points.</description>
</problem>
</problems>
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>MyInterafaceImpl.java</file>
<line>2</line>
<offset>13</offset>
<length>16</length>
<description>Class is not instantiated.</description>
</problem>
</problems>
@@ -0,0 +1,5 @@
public class MyInterafaceImpl implements MyInterface {
@Override
public void myMethod() {}
}
@@ -0,0 +1,5 @@
public interface MyInterface {
@SuppressWarnings("unused")
void myMethod();
}
@@ -79,6 +79,10 @@ public class UnusedDeclarationInspectionTest extends AbstractUnusedDeclarationTe
doTest5();
}
public void testSuppressionIsNotAnEntryPoint() {
doTest();
}
public void testSuppressByNoinspectionTag() {
doTest();
}
@@ -2101,6 +2101,7 @@ com.intellij.codeInspection.reference.RefElement
- a:isPermanentEntry():Z
- a:isReachable():Z
- a:isReferenced():Z
- isSuppressed(java.lang.String[]):Z
com.intellij.codeInspection.reference.RefEntity
- com.intellij.openapi.util.UserDataHolder
- sf:EMPTY_ELEMENTS_ARRAY:com.intellij.codeInspection.reference.RefEntity[]
@@ -1,4 +1,4 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInspection.reference;
import com.intellij.psi.PsiElement;
@@ -106,4 +106,14 @@ public interface RefElement extends RefEntity {
@NotNull
RefElement getContainingEntry();
/**
* Checks if the specified warnings are suppressed for this element or any of its surrounding elements.
*
* @param toolIds the tool ids to check, for example "unchecked" or "deprecation"
* @return true, when any one of the specified ids is suppressed or when "ALL" is suppressed, false otherwise.
*/
default boolean isSuppressed(String @NotNull ... warningIds) {
return false;
}
}
@@ -1,4 +1,4 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInspection.reference;
import com.intellij.codeInspection.SuppressionUtil;
@@ -277,6 +277,7 @@ public abstract class RefElementImpl extends RefEntityImpl implements RefElement
mySuppressions = text.split("[, ]");
}
@Override
public boolean isSuppressed(String @NotNull ... toolIds) {
if (mySuppressions != null) {
for (@NonNls String suppression : mySuppressions) {