Skip to content

Commit

Permalink
Add code action for unused pattern variable / unused lambda parameter.
Browse files Browse the repository at this point in the history
Signed-off-by: Roland Grunberg <[email protected]>
  • Loading branch information
rgrunber committed Nov 5, 2024
1 parent 3698052 commit a69770d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ private void process(CodeActionParams params, IInvocationContext context, IProbl
case IProblem.UnusedPrivateType:
case IProblem.LocalVariableIsNeverUsed:
case IProblem.ArgumentIsNeverUsed:
case IProblem.LambdaParameterIsNeverUsed:
case IProblem.UnusedPrivateField:
LocalCorrectionsSubProcessor.addUnusedMemberProposal(context, problem, proposals);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
import org.eclipse.jdt.internal.corext.fix.CodeStyleFixCore;
import org.eclipse.jdt.internal.corext.fix.IProposableFix;
import org.eclipse.jdt.internal.corext.fix.RenameUnusedVariableFixCore;
import org.eclipse.jdt.internal.corext.fix.SealedClassFixCore;
import org.eclipse.jdt.internal.corext.fix.UnimplementedCodeFixCore;
import org.eclipse.jdt.internal.corext.fix.UnusedCodeFixCore;
Expand Down Expand Up @@ -434,6 +435,22 @@ public static void addUnimplementedMethodsProposals(IInvocationContext context,
public static void addUnusedMemberProposal(IInvocationContext context, IProblemLocation problem, Collection<ProposalKindWrapper> proposals) {
int problemId = problem.getProblemId();

if (JavaModelUtil.is22OrHigher(context.getCompilationUnit().getJavaProject()) && (problemId == IProblem.LocalVariableIsNeverUsed || problemId == IProblem.LambdaParameterIsNeverUsed)) {
RenameUnusedVariableFixCore fix = RenameUnusedVariableFixCore.createRenameToUnnamedFix(context.getASTRoot(), problem);
if (fix != null) {
try {
CompilationUnitChange change = fix.createChange(null);
CUCorrectionProposalCore proposal = new CUCorrectionProposalCore(change.getName(), change.getCompilationUnit(), change, IProposalRelevance.UNUSED_MEMBER);
proposals.add(CodeActionHandler.wrap(proposal, CodeActionKind.QuickFix));
} catch (CoreException e) {
JavaLanguageServerPlugin.log(e);
}
}
}
if (problemId == IProblem.LambdaParameterIsNeverUsed) {
return;
}

UnusedCodeFixCore fix = UnusedCodeFixCore.createUnusedMemberFix(context.getASTRoot(), problem, false);
if (fix != null) {
try {
Expand Down

0 comments on commit a69770d

Please sign in to comment.