mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
introduce local tool for unused return value
This commit is contained in:
+21
-7
@@ -65,11 +65,7 @@ public class UnusedReturnValue extends GlobalJavaBatchInspectionTool{
|
||||
|
||||
final boolean isNative = psiMethod.hasModifierProperty(PsiModifier.NATIVE);
|
||||
if (refMethod.isExternalOverride() && !isNative) return null;
|
||||
return new ProblemDescriptor[]{manager.createProblemDescriptor(psiMethod.getNavigationElement(),
|
||||
InspectionsBundle
|
||||
.message("inspection.unused.return.value.problem.descriptor"),
|
||||
!isNative ? new MakeVoidQuickFix(processor) : null, ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
|
||||
false)};
|
||||
return new ProblemDescriptor[]{createProblemDescriptor(psiMethod, manager, processor, isNative)};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,11 +132,29 @@ public class UnusedReturnValue extends GlobalJavaBatchInspectionTool{
|
||||
return new MakeVoidQuickFix(null);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public LocalInspectionTool getSharedLocalInspectionTool() {
|
||||
return new UnusedReturnValueLocalInspection(this);
|
||||
}
|
||||
|
||||
|
||||
static ProblemDescriptor createProblemDescriptor(@NotNull PsiMethod psiMethod,
|
||||
@NotNull InspectionManager manager,
|
||||
@Nullable ProblemDescriptionsProcessor processor,
|
||||
boolean isNative) {
|
||||
return manager.createProblemDescriptor(psiMethod.getNameIdentifier(),
|
||||
InspectionsBundle.message("inspection.unused.return.value.problem.descriptor"),
|
||||
isNative ? null : new MakeVoidQuickFix(processor),
|
||||
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
|
||||
false);
|
||||
}
|
||||
|
||||
private static class MakeVoidQuickFix implements LocalQuickFix {
|
||||
private final ProblemDescriptionsProcessor myProcessor;
|
||||
private static final Logger LOG = Logger.getInstance("#" + MakeVoidQuickFix.class.getName());
|
||||
private static final Logger LOG = Logger.getInstance(MakeVoidQuickFix.class);
|
||||
|
||||
public MakeVoidQuickFix(final ProblemDescriptionsProcessor processor) {
|
||||
public MakeVoidQuickFix(@Nullable final ProblemDescriptionsProcessor processor) {
|
||||
myProcessor = processor;
|
||||
}
|
||||
|
||||
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright 2000-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.codeInspection.unusedReturnValue;
|
||||
|
||||
import com.intellij.codeInsight.daemon.impl.UnusedSymbolUtil;
|
||||
import com.intellij.codeInspection.BaseJavaLocalInspectionTool;
|
||||
import com.intellij.codeInspection.InspectionManager;
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.openapi.progress.EmptyProgressIndicator;
|
||||
import com.intellij.patterns.PsiJavaPatterns;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PropertyUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class UnusedReturnValueLocalInspection extends BaseJavaLocalInspectionTool {
|
||||
private final UnusedReturnValue myGlobal;
|
||||
|
||||
public UnusedReturnValueLocalInspection(UnusedReturnValue global) {myGlobal = global;}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getGroupDisplayName() {
|
||||
return myGlobal.getGroupDisplayName();
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getDisplayName() {
|
||||
return myGlobal.getDisplayName();
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getShortName() {
|
||||
return myGlobal.getShortName();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public ProblemDescriptor[] checkMethod(@NotNull PsiMethod method, @NotNull InspectionManager manager, boolean isOnTheFly) {
|
||||
if (method.isConstructor() ||
|
||||
myGlobal.IGNORE_BUILDER_PATTERN && PropertyUtil.isSimplePropertySetter(method) ||
|
||||
method.hasModifierProperty(PsiModifier.NATIVE)) return null;
|
||||
|
||||
final boolean[] atLeastOneUsageExists = new boolean[]{false};
|
||||
if (UnusedSymbolUtil.processUsages(manager.getProject(), method.getContainingFile(), method, new EmptyProgressIndicator(), null, u -> {
|
||||
if (!atLeastOneUsageExists[0]) atLeastOneUsageExists[0] = true;
|
||||
return PsiJavaPatterns.psiElement(PsiReferenceExpression.class)
|
||||
.withParent(PsiJavaPatterns.psiElement(PsiMethodCallExpression.class)
|
||||
.withParent(PsiExpressionStatement.class))
|
||||
.accepts(u.getElement());
|
||||
})) {
|
||||
if (atLeastOneUsageExists[0]) {
|
||||
return new ProblemDescriptor[]{UnusedReturnValue.createProblemDescriptor(method, manager, null, false)};
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 2000-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.codeInspection;
|
||||
|
||||
import com.intellij.JavaTestUtil;
|
||||
import com.intellij.codeInspection.unusedReturnValue.UnusedReturnValue;
|
||||
import com.intellij.codeInspection.unusedReturnValue.UnusedReturnValueLocalInspection;
|
||||
import com.intellij.testFramework.InspectionTestCase;
|
||||
|
||||
public class UnusedReturnValueLocalTest extends InspectionTestCase {
|
||||
private final UnusedReturnValue myGlobal = new UnusedReturnValue();
|
||||
private final UnusedReturnValueLocalInspection myTool = new UnusedReturnValueLocalInspection(myGlobal);
|
||||
|
||||
@Override
|
||||
protected String getTestDataPath() {
|
||||
return JavaTestUtil.getJavaTestDataPath() + "/inspection";
|
||||
}
|
||||
|
||||
private void doTest() throws Exception {
|
||||
doTest("unusedReturnValue/" + getTestName(true), myTool);
|
||||
}
|
||||
|
||||
|
||||
public void testNonLiteral() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testHierarchy() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testMethodReference() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testSimpleSetter() throws Exception {
|
||||
try {
|
||||
myGlobal.IGNORE_BUILDER_PATTERN = true;
|
||||
doTest();
|
||||
}
|
||||
finally {
|
||||
myGlobal.IGNORE_BUILDER_PATTERN = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user