[java-inspections] IDEA-341640 Mark main(String[] args) parameter as unused with implicit classes

GitOrigin-RevId: 08ca1f5ed4f48439b394f7fd313f2b95d50c48e7
This commit is contained in:
Mikhail Pyltsin
2024-01-04 10:15:11 +00:00
committed by intellij-monorepo-bot
parent 90a55b57c9
commit 0df89a1b62
7 changed files with 70 additions and 1 deletions
@@ -379,7 +379,7 @@ class PostHighlightingVisitor extends JavaElementVisitor {
(!isOverriddenOrOverrides(method) || myUnusedSymbolInspection.checkParameterExcludingHierarchy())) &&
!method.hasModifierProperty(PsiModifier.NATIVE) &&
!JavaHighlightUtil.isSerializationRelatedMethod(method, method.getContainingClass()) &&
!PsiClassImplUtil.isMainOrPremainMethod(method)) {
!isUsedMainOrPremainMethod(method)) {
if (UnusedSymbolUtil.isInjected(project, method)) return;
checkUnusedParameter(parameter, method);
if (message != null) {
@@ -435,6 +435,20 @@ class PostHighlightingVisitor extends JavaElementVisitor {
}
}
private static boolean isUsedMainOrPremainMethod(@NotNull PsiMethod method) {
if (!PsiClassImplUtil.isMainOrPremainMethod(method)) {
return false;
}
//premain
if (!"main".equals(method.getName())) {
return true;
}
if (!HighlightingFeature.IMPLICIT_CLASSES.isAvailable(method)) {
return true;
}
return false;
}
private void checkUnusedParameter(@NotNull PsiParameter parameter, @Nullable PsiMethod declarationMethod) {
if (!myRefCountHolder.isReferenced(parameter) && !UnusedSymbolUtil.isImplicitUsage(myProject, parameter)) {
message = JavaErrorBundle.message(parameter instanceof PsiPatternVariable ?
@@ -0,0 +1,6 @@
// "Safe delete 'args'" "false"
public class Main{
public static void main(String[] ar<caret>gs) {
}
}
@@ -0,0 +1,6 @@
// "Safe delete 'args'" "true-preview"
public class Main{
public static void main() {
}
}
@@ -0,0 +1,5 @@
// "Safe delete 'args'" "true-preview"
public static void main() {
}
@@ -0,0 +1,6 @@
// "Safe delete 'args'" "true-preview"
public class Main{
public static void main(String[] arg<caret>s) {
}
}
@@ -0,0 +1,5 @@
// "Safe delete 'args'" "true-preview"
public static void main(String[] arg<caret>s) {
}
@@ -0,0 +1,27 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
import com.intellij.codeInspection.deadCode.UnusedDeclarationInspection;
import com.intellij.pom.java.LanguageLevel;
public class RemoveUnusedParameterJava22PreviewTest extends LightQuickFixParameterizedTestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
enableInspectionTool(new UnusedDeclarationInspection());
}
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/removeUnusedParameterJava22Preview";
}
@Override
protected LanguageLevel getLanguageLevel() {
return LanguageLevel.JDK_22_PREVIEW;
}
}