Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code action for unused pattern variable / unused lambda parameter. #3319

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public CleanUpRegistry() {
new LambdaExpressionCleanup(),
new TryWithResourceCleanUp(),
new LambdaExpressionAndMethodRefCleanUp(),
new OrganizeImportsCleanup());
new OrganizeImportsCleanup(),
new RenameUnusedLocalVariableCleanup());

// Store in a Map so that they can be accessed by ID quickly
cleanUps = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*******************************************************************************
* Copyright (c) 2024 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal.cleanup;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
import org.eclipse.jdt.internal.corext.fix.RenameUnusedVariableFixCore;
import org.eclipse.jdt.ui.cleanup.CleanUpContext;
import org.eclipse.jdt.ui.cleanup.ICleanUpFix;

/**
* Represents a clean up that renames unused lambda parameter identifier to an
* underscore (_)
*/
public class RenameUnusedLocalVariableCleanup implements ISimpleCleanUp {

private static final List<String> COMPILER_OPTS = Arrays.asList(JavaCore.COMPILER_PB_UNUSED_LAMBDA_PARAMETER, JavaCore.COMPILER_PB_UNUSED_LOCAL);

@Override
public Collection<String> getIdentifiers() {
return List.of("removeUnusedLambdaParameters", CleanUpConstants.REMOVE_UNUSED_CODE_LOCAL_VARIABLES);
}

@Override
public ICleanUpFix createFix(CleanUpContext context) throws CoreException {
return RenameUnusedVariableFixCore.createCleanUp(context.getAST(), true);
}

@Override
public List<String> getRequiredCompilerMarkers() {
return COMPILER_OPTS;
}

}
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
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void setup() throws Exception {
javaProject = JavaCore.create(project);

Hashtable<String, String> options = TestOptions.getDefaultOptions();
JavaCore.setComplianceOptions(JavaCore.VERSION_19, options);
JavaCore.setComplianceOptions(JavaCore.VERSION_22, options);
options.put(DefaultCodeFormatterConstants.FORMATTER_NUMBER_OF_EMPTY_LINES_TO_PRESERVE, String.valueOf(99));
javaProject.setOptions(options);

Expand Down Expand Up @@ -640,4 +640,39 @@ public void test() {
assertEquals(expected, actual);
}

@Test
public void testRenameUnusedLambdaParameterCleanup() throws Exception {
String contents = """
package test1;
public class A {
private interface J {
public void run(String a, String b);
}
public void test() {
J j = (a, b) -> System.out.println(a);
j.run("a", "b");
}
}
""";

ICompilationUnit unit = pack1.createCompilationUnit("A.java", contents, false, monitor);
String uri = unit.getUnderlyingResource().getLocationURI().toString();

String expected = """
package test1;
public class A {
private interface J {
public void run(String a, String b);
}
public void test() {
J j = (a, _) -> System.out.println(a);
j.run("a", "b");
}
}
""";
List<TextEdit> textEdits = registry.getEditsForAllActiveCleanUps(new TextDocumentIdentifier(uri), Arrays.asList("removeUnusedLambdaParameters"), monitor);
String actual = TextEditUtil.apply(unit, textEdits);
assertEquals(expected, actual);
}

}
Loading