From ab1a47ddef0f177a34a763e8f0539c9751e2d47d Mon Sep 17 00:00:00 2001 From: "Anna.Kozlova" Date: Mon, 2 Jan 2017 14:04:27 +0100 Subject: [PATCH] highlight unused fields implicitly assigned by extension (IDEA-166091) --- .../analysis/PostHighlightingVisitor.java | 8 ++- ...eldsWhichAreExplicitlyWrittenAsUnused.java | 4 ++ .../LightUnusedHighlightingFixtureTest.java | 70 +++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advFixture/MarkFieldsWhichAreExplicitlyWrittenAsUnused.java create mode 100644 java/java-tests/testSrc/com/intellij/codeInsight/daemon/LightUnusedHighlightingFixtureTest.java diff --git a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/PostHighlightingVisitor.java b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/PostHighlightingVisitor.java index a95aaf3f3fd2..77e399aa1e89 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/PostHighlightingVisitor.java +++ b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/PostHighlightingVisitor.java @@ -339,10 +339,16 @@ class PostHighlightingVisitor { return info; } } - else if (UnusedSymbolUtil.isImplicitUsage(myProject, field, progress)) { + else if (UnusedSymbolUtil.isImplicitRead(myProject, field, progress)) { return null; } else if (UnusedSymbolUtil.isFieldUnused(myProject, myFile, field, progress, helper)) { + if (UnusedSymbolUtil.isImplicitWrite(myProject, field, progress)) { + String message = JavaErrorMessages.message("private.field.is.not.used.for.reading", identifier.getText()); + HighlightInfo highlightInfo = UnusedSymbolUtil.createUnusedSymbolInfo(identifier, message, myDeadCodeInfoType); + QuickFixAction.registerQuickFixAction(highlightInfo, QuickFixFactory.getInstance().createSafeDeleteFix(field), myDeadCodeKey); + return highlightInfo; + } return formatUnusedSymbolHighlightInfo(project, "field.is.not.used", field, "fields", myDeadCodeKey, myDeadCodeInfoType, identifier); } return null; diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advFixture/MarkFieldsWhichAreExplicitlyWrittenAsUnused.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advFixture/MarkFieldsWhichAreExplicitlyWrittenAsUnused.java new file mode 100644 index 000000000000..eff890cf55e6 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advFixture/MarkFieldsWhichAreExplicitlyWrittenAsUnused.java @@ -0,0 +1,4 @@ +public class MarkFieldsWhichAreExplicitlyWrittenAsUnused { + + public String implicitWrite; +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/LightUnusedHighlightingFixtureTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/LightUnusedHighlightingFixtureTest.java new file mode 100644 index 000000000000..262717f11484 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/LightUnusedHighlightingFixtureTest.java @@ -0,0 +1,70 @@ +/* + * Copyright 2000-2017 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.codeInsight.daemon; + +import com.intellij.JavaTestUtil; +import com.intellij.codeInspection.deadCode.UnusedDeclarationInspection; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiField; +import com.intellij.psi.PsiNamedElement; +import com.intellij.testFramework.LightProjectDescriptor; +import com.intellij.testFramework.PlatformTestUtil; +import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase; +import org.jetbrains.annotations.NotNull; + +public class LightUnusedHighlightingFixtureTest extends LightCodeInsightFixtureTestCase { + + @Override + protected void setUp() throws Exception { + super.setUp(); + myFixture.enableInspections(new UnusedDeclarationInspection(true)); + } + + public void testMarkFieldsWhichAreExplicitlyWrittenAsUnused() throws Exception { + PlatformTestUtil.registerExtension(ImplicitUsageProvider.EP_NAME, new ImplicitUsageProvider() { + @Override + public boolean isImplicitUsage(PsiElement element) { + return isImplicitWrite(element); + } + + @Override + public boolean isImplicitRead(PsiElement element) { + return false; + } + + @Override + public boolean isImplicitWrite(PsiElement element) { + return element instanceof PsiField && "implicitWrite".equals(((PsiNamedElement)element).getName()); + } + }, getTestRootDisposable()); + + myFixture.configureByFile(getTestName(false) + ".java"); + myFixture.checkHighlighting(); + } + + + + @Override + protected String getBasePath() { + return JavaTestUtil.getRelativeJavaTestDataPath() + "/codeInsight/daemonCodeAnalyzer/advFixture"; + } + + @NotNull + @Override + protected LightProjectDescriptor getProjectDescriptor() { + return JAVA_8; + } +}