mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-13 15:52:01 +07:00
Java: don't report non-private field as assigned not accessed when it is implicitly read and written (IDEA-357404)
GitOrigin-RevId: 943ef6961c19510e5d4291367293424a87499487
This commit is contained in:
committed by
intellij-monorepo-bot
parent
624ffd24a6
commit
5c06f74975
@@ -196,7 +196,7 @@ public final class UnusedSymbolLocalInspection extends AbstractBaseJavaLocalInsp
|
||||
}
|
||||
}
|
||||
else if (!UnusedSymbolUtil.isFieldUsed(project, file, field, helper)) {
|
||||
if (UnusedSymbolUtil.isImplicitWrite(project, field)) {
|
||||
if (UnusedSymbolUtil.isImplicitWrite(project, field) && !UnusedSymbolUtil.isImplicitRead(project, field)) {
|
||||
registerProblem(field, getNotUsedForReadingMessage(field), List.of(fixFactory.createSafeDeleteFix(field)));
|
||||
}
|
||||
else if (!UnusedSymbolUtil.isImplicitUsage(project, field)) {
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
class <warning descr="Class 'Test' is never used">Test</warning>{
|
||||
|
||||
private int <warning descr="Private field 'fieldWritten' is assigned but never accessed">fieldWritten</warning>;
|
||||
|
||||
private int fieldWritten2;
|
||||
|
||||
int fieldReadWritten;
|
||||
|
||||
int <warning descr="Package-private field 'fieldWritten3' is assigned but never accessed">fieldWritten3</warning><EOLError descr="';' expected"></EOLError>
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return fieldWritten2;
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package javax.annotation;
|
||||
|
||||
/*
|
||||
* Copyright 2000-2011 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.
|
||||
*/
|
||||
@interface Resource {}
|
||||
class Test{
|
||||
@Resource
|
||||
private int <warning descr="Private field 'field' is assigned but never accessed">field</warning>;
|
||||
|
||||
@Resource
|
||||
private int field2;
|
||||
|
||||
int f() {
|
||||
return field2;
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
package com.intellij.java.codeInsight.daemon;
|
||||
|
||||
import com.intellij.codeInsight.daemon.DaemonAnalyzerTestCase;
|
||||
import com.intellij.codeInsight.daemon.ImplicitUsageProvider;
|
||||
import com.intellij.codeInsight.daemon.impl.HighlightInfo;
|
||||
import com.intellij.codeInspection.deadCode.UnusedDeclarationInspection;
|
||||
import com.intellij.lang.annotation.HighlightSeverity;
|
||||
@@ -10,7 +11,10 @@ import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.projectRoots.Sdk;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.PsiDocumentManager;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiField;
|
||||
import com.intellij.testFramework.IdeaTestUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
@@ -41,7 +45,26 @@ public class UnusedSymbolLocalTest extends DaemonAnalyzerTestCase {
|
||||
});
|
||||
}
|
||||
|
||||
//public void testInjectedAnno() throws Exception { doTest(); }
|
||||
public void testImplicitReadsWrites() throws Exception {
|
||||
ImplicitUsageProvider.EP_NAME.getPoint().registerExtension(new ImplicitUsageProvider() {
|
||||
@Override
|
||||
public boolean isImplicitUsage(@NotNull PsiElement element) {
|
||||
return isImplicitWrite(element) || isImplicitRead(element);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isImplicitRead(@NotNull PsiElement element) {
|
||||
return element instanceof PsiField field && field.getName().contains("Read");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isImplicitWrite(@NotNull PsiElement element) {
|
||||
return element instanceof PsiField field && field.getName().contains("Written");
|
||||
}
|
||||
}, getTestRootDisposable());
|
||||
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testChangeInsideCodeBlock() throws Exception {
|
||||
doTest();
|
||||
|
||||
@@ -55,7 +55,7 @@ class <warning descr="Class 'IntellijInspectionNPEDemo' is never used">IntellijI
|
||||
}
|
||||
}
|
||||
|
||||
public final String <warning descr="Public field 'jsonObject' is assigned but never accessed">jsonObject</warning>;
|
||||
public final String jsonObject;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user