Skip to content

Commit f4ed0c2

Browse files
authored
Fix unused suppressed warnings quick fix counts (#1720)
- add new logic for UnusedSuppressWarningsCleanUp.computeNumberOfFixes() that screens the problems for the current literal or returns 1 if removing all unused tokens - fix SuppressWarningsBaseSubProcessor to remove the call to remove all instances of current literal since the computeNumberOfFixes() call in the clean up will offer an option to do this automatically - fix the test in QuickFixTest1d8 - fixes #1719
1 parent 84472de commit f4ed0c2

File tree

3 files changed

+25
-34
lines changed

3 files changed

+25
-34
lines changed

org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/fix/UnusedSuppressWarningsCleanUp.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.eclipse.jdt.core.JavaCore;
2323
import org.eclipse.jdt.core.JavaModelException;
2424
import org.eclipse.jdt.core.compiler.IProblem;
25+
import org.eclipse.jdt.core.dom.ASTNode;
2526
import org.eclipse.jdt.core.dom.CompilationUnit;
2627
import org.eclipse.jdt.core.dom.StringLiteral;
2728

@@ -32,6 +33,8 @@
3233
import org.eclipse.jdt.ui.cleanup.ICleanUpFix;
3334
import org.eclipse.jdt.ui.text.java.IProblemLocation;
3435

36+
import org.eclipse.jdt.internal.ui.text.correction.ProblemLocation;
37+
3538
/**
3639
* Create fix to remove unnecessary SuppressWarnings
3740
* @see org.eclipse.jdt.internal.corext.fix.UnusedSuppressWarningsFixCore
@@ -127,8 +130,28 @@ public int computeNumberOfFixes(CompilationUnit compilationUnit) {
127130
int result= 0;
128131
IProblem[] problems= compilationUnit.getProblems();
129132
if (isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_SUPPRESS_WARNINGS))
130-
result+= getNumberOfProblems(problems, IProblem.UnusedWarningToken);
133+
result+= getNumberOfProblems(problems, compilationUnit);
134+
135+
return result;
136+
}
131137

138+
private int getNumberOfProblems(IProblem[] problems, CompilationUnit compilationUnit) {
139+
int result= 0;
140+
if (fLiteral == null) {
141+
return 1;
142+
}
143+
for (IProblem problem : problems) {
144+
IProblemLocation location= new ProblemLocation(problem);
145+
if (location.getProblemId() == IProblem.UnusedWarningToken) {
146+
ASTNode node= location.getCoveringNode(compilationUnit);
147+
if (node instanceof StringLiteral literal) {
148+
if (literal.getLiteralValue().equals(fLiteral.getLiteralValue())) {
149+
result++;
150+
}
151+
}
152+
}
153+
}
132154
return result;
133155
}
156+
134157
}

org.eclipse.jdt.core.manipulation/proposals/org/eclipse/jdt/internal/ui/text/correction/SuppressWarningsBaseSubProcessor.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -245,21 +245,11 @@ public void getRemoveUnusedSuppressWarningProposals(IInvocationContext context,
245245
T proposal= createFixCorrectionProposal(fix, cleanUp, IProposalRelevance.REMOVE_ANNOTATION, context);
246246
proposals.add(proposal);
247247
}
248-
fix= UnusedSuppressWarningsFixCore.createAllFix(context.getASTRoot(), literal);
249-
if (fix != null) {
250-
Map<String, String> options= new Hashtable<>();
251-
options.put(CleanUpConstants.REMOVE_UNNECESSARY_SUPPRESS_WARNINGS, CleanUpOptions.TRUE);
252-
UnusedSuppressWarningsCleanUp cleanUp= new UnusedSuppressWarningsCleanUp(options);
253-
cleanUp.setLiteral(literal);
254-
T proposal= createFixCorrectionProposal(fix, cleanUp, IProposalRelevance.REMOVE_ANNOTATION, context);
255-
proposals.add(proposal);
256-
}
257248
fix= UnusedSuppressWarningsFixCore.createAllFix(context.getASTRoot(), null);
258249
if (fix != null) {
259250
Map<String, String> options= new Hashtable<>();
260251
options.put(CleanUpConstants.REMOVE_UNNECESSARY_SUPPRESS_WARNINGS, CleanUpOptions.TRUE);
261252
UnusedSuppressWarningsCleanUp cleanUp= new UnusedSuppressWarningsCleanUp(options);
262-
cleanUp.setLiteral(literal);
263253
T proposal= createFixCorrectionProposal(fix, cleanUp, IProposalRelevance.REMOVE_ANNOTATION, context);
264254
proposals.add(proposal);
265255
}

org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/QuickFixTest1d8.java

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3050,7 +3050,6 @@ public boolean simplifyCompoundIf(int x) {
30503050
return false;
30513051
}
30523052
3053-
@SuppressWarnings("unchecked")
30543053
public boolean simplifyNLS(String x) {
30553054
return true;
30563055
}
@@ -3060,27 +3059,6 @@ public boolean simplifyAll() {
30603059
}
30613060
}
30623061
""";
3063-
3064-
String expected3= """
3065-
package test1;
3066-
public class E {
3067-
public boolean simplifyNormal(int x) {
3068-
return true;
3069-
}
3070-
3071-
public boolean simplifyCompoundIf(int x) {
3072-
return false;
3073-
}
3074-
3075-
public boolean simplifyNLS(String x) {
3076-
return true;
3077-
}
3078-
3079-
public boolean simplifyAll() {
3080-
return false;
3081-
}
3082-
}
3083-
""";
3084-
assertExpectedExistInProposals(proposals, new String[] {expected1, expected2, expected3});
3062+
assertExpectedExistInProposals(proposals, new String[] {expected1, expected2});
30853063
}
30863064
}

0 commit comments

Comments
 (0)