added specializing error "Variable initializer is redundant" for unused-vars analysis

This commit is contained in:
svtk
2011-12-08 13:43:46 +04:00
parent 7213d43f22
commit e05dea7046
11 changed files with 43 additions and 39 deletions
@@ -16,6 +16,7 @@ import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingContextUtils; import org.jetbrains.jet.lang.resolve.BindingContextUtils;
import org.jetbrains.jet.lang.resolve.BindingTrace; import org.jetbrains.jet.lang.resolve.BindingTrace;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.types.JetStandardClasses; import org.jetbrains.jet.lang.types.JetStandardClasses;
import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lexer.JetTokens; import org.jetbrains.jet.lexer.JetTokens;
@@ -354,8 +355,11 @@ public class JetFlowInformationProvider {
return true; return true;
} }
JetClassOrObject propertyClassOrObject = PsiTreeUtil.getParentOfType(property, JetClassOrObject.class); JetNamedDeclaration parentDeclaration = PsiTreeUtil.getParentOfType(element, JetNamedDeclaration.class);
if (propertyClassOrObject == null || !PsiTreeUtil.isAncestor(propertyClassOrObject, element, false)) { DeclarationDescriptor declarationDescriptor = trace.get(BindingContext.DECLARATION_TO_DESCRIPTOR, parentDeclaration);
assert declarationDescriptor != null;
ClassDescriptor variableClass = DescriptorUtils.getParentOfType(variableDescriptor, ClassDescriptor.class);
if (variableClass == null || !DescriptorUtils.isAncestor(variableClass, declarationDescriptor, false)) {
trace.report(Errors.INACCESSIBLE_BACKING_FIELD.on(element)); trace.report(Errors.INACCESSIBLE_BACKING_FIELD.on(element));
return true; return true;
} }
@@ -482,11 +486,7 @@ public class JetFlowInformationProvider {
assert pseudocode != null; assert pseudocode != null;
JetControlFlowGraphTraverser<Map<VariableDescriptor, VariableStatus>> traverser = JetControlFlowGraphTraverser.create(pseudocode, true, false); JetControlFlowGraphTraverser<Map<VariableDescriptor, VariableStatus>> traverser = JetControlFlowGraphTraverser.create(pseudocode, true, false);
final Collection<VariableDescriptor> declaredVariables = collectDeclaredVariables(subroutine); final Collection<VariableDescriptor> declaredVariables = collectDeclaredVariables(subroutine);
Collection<VariableDescriptor> usedVariables = collectUsedVariables(pseudocode);
Map<VariableDescriptor, VariableStatus> sinkInstructionData = Maps.newHashMap(); Map<VariableDescriptor, VariableStatus> sinkInstructionData = Maps.newHashMap();
for (VariableDescriptor usedVariable : usedVariables) {
sinkInstructionData.put(usedVariable, VariableStatus.UNUSED);
}
traverser.collectInformationFromInstructionGraph(new JetControlFlowGraphTraverser.InstructionDataMergeStrategy<Map<VariableDescriptor, VariableStatus>>() { traverser.collectInformationFromInstructionGraph(new JetControlFlowGraphTraverser.InstructionDataMergeStrategy<Map<VariableDescriptor, VariableStatus>>() {
@Override @Override
public Pair<Map<VariableDescriptor, VariableStatus>, Map<VariableDescriptor, VariableStatus>> execute(Instruction instruction, @NotNull Collection<Map<VariableDescriptor, VariableStatus>> incomingEdgesData) { public Pair<Map<VariableDescriptor, VariableStatus>, Map<VariableDescriptor, VariableStatus>> execute(Instruction instruction, @NotNull Collection<Map<VariableDescriptor, VariableStatus>> incomingEdgesData) {
@@ -505,7 +505,17 @@ public class JetFlowInformationProvider {
exitResult.put(variableDescriptor, VariableStatus.READ); exitResult.put(variableDescriptor, VariableStatus.READ);
} }
else if (instruction instanceof WriteValueInstruction) { else if (instruction instanceof WriteValueInstruction) {
exitResult.put(variableDescriptor, VariableStatus.WRITTEN); VariableStatus variableStatus = enterResult.get(variableDescriptor);
if (variableStatus == null) variableStatus = VariableStatus.UNUSED;
switch(variableStatus) {
case UNUSED:
case ONLY_WRITTEN:
exitResult.put(variableDescriptor, VariableStatus.ONLY_WRITTEN);
break;
case WRITTEN:
case READ:
exitResult.put(variableDescriptor, VariableStatus.WRITTEN);
}
} }
} }
return new Pair<Map<VariableDescriptor, VariableStatus>, Map<VariableDescriptor, VariableStatus>>(enterResult, exitResult); return new Pair<Map<VariableDescriptor, VariableStatus>, Map<VariableDescriptor, VariableStatus>>(enterResult, exitResult);
@@ -516,13 +526,12 @@ public class JetFlowInformationProvider {
public void execute(Instruction instruction, @Nullable Map<VariableDescriptor, VariableStatus> enterData, @Nullable Map<VariableDescriptor, VariableStatus> exitData) { public void execute(Instruction instruction, @Nullable Map<VariableDescriptor, VariableStatus> enterData, @Nullable Map<VariableDescriptor, VariableStatus> exitData) {
assert enterData != null && exitData != null; assert enterData != null && exitData != null;
VariableDescriptor variableDescriptor = extractVariableDescriptorIfAny(instruction, false); VariableDescriptor variableDescriptor = extractVariableDescriptorIfAny(instruction, false);
if (variableDescriptor == null || !declaredVariables.contains(variableDescriptor)) return; if (variableDescriptor == null || !declaredVariables.contains(variableDescriptor) ||
PsiElement variableDeclarationElement = trace.get(BindingContext.DESCRIPTOR_TO_DECLARATION, variableDescriptor); !DescriptorUtils.isLocal(variableDescriptor.getContainingDeclaration(), variableDescriptor)) return;
assert variableDeclarationElement instanceof JetDeclaration; VariableStatus variableStatus = enterData.get(variableDescriptor);
if (!JetPsiUtil.isLocal((JetDeclaration) variableDeclarationElement)) return;
if (instruction instanceof WriteValueInstruction) { if (instruction instanceof WriteValueInstruction) {
JetElement element = ((WriteValueInstruction) instruction).getElement(); JetElement element = ((WriteValueInstruction) instruction).getElement();
if (enterData.get(variableDescriptor) == VariableStatus.WRITTEN || enterData.get(variableDescriptor) == VariableStatus.UNUSED) { if (variableStatus != VariableStatus.READ) {
if (element instanceof JetBinaryExpression && ((JetBinaryExpression) element).getOperationToken() == JetTokens.EQ) { if (element instanceof JetBinaryExpression && ((JetBinaryExpression) element).getOperationToken() == JetTokens.EQ) {
JetExpression right = ((JetBinaryExpression) element).getRight(); JetExpression right = ((JetBinaryExpression) element).getRight();
if (right != null) { if (right != null) {
@@ -542,7 +551,7 @@ public class JetFlowInformationProvider {
if (element instanceof JetNamedDeclaration) { if (element instanceof JetNamedDeclaration) {
PsiElement nameIdentifier = ((JetNamedDeclaration) element).getNameIdentifier(); PsiElement nameIdentifier = ((JetNamedDeclaration) element).getNameIdentifier();
PsiElement elementToMark = nameIdentifier != null ? nameIdentifier : element; PsiElement elementToMark = nameIdentifier != null ? nameIdentifier : element;
if (enterData.get(variableDescriptor) == VariableStatus.UNUSED) { if (variableStatus == null || variableStatus == VariableStatus.UNUSED) {
if (element instanceof JetProperty) { if (element instanceof JetProperty) {
trace.report(Errors.UNUSED_VARIABLE.on((JetProperty) element, elementToMark, variableDescriptor)); trace.report(Errors.UNUSED_VARIABLE.on((JetProperty) element, elementToMark, variableDescriptor));
} }
@@ -559,9 +568,15 @@ public class JetFlowInformationProvider {
} }
} }
} }
else if (enterData.get(variableDescriptor) == VariableStatus.WRITTEN && element instanceof JetProperty) { else if (variableStatus == VariableStatus.ONLY_WRITTEN && element instanceof JetProperty) {
trace.report(Errors.ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE.on((JetNamedDeclaration) element, elementToMark, variableDescriptor)); trace.report(Errors.ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE.on((JetNamedDeclaration) element, elementToMark, variableDescriptor));
} }
else if (variableStatus == VariableStatus.WRITTEN && element instanceof JetProperty) {
JetExpression initializer = ((JetProperty) element).getInitializer();
if (initializer != null) {
trace.report(Errors.VARIABLE_WITH_REDUNDANT_INITIALIZER.on((JetNamedDeclaration) element, initializer, variableDescriptor));
}
}
} }
} }
@@ -570,8 +585,9 @@ public class JetFlowInformationProvider {
} }
private static enum VariableStatus { private static enum VariableStatus {
READ(2), READ(3),
WRITTEN(1), WRITTEN(2),
ONLY_WRITTEN(1),
UNUSED(0); UNUSED(0);
private int importance; private int importance;
@@ -154,6 +154,7 @@ public interface Errors {
UnusedElementDiagnosticFactory<JetProperty, VariableDescriptor> UNUSED_VARIABLE = UnusedElementDiagnosticFactory.create(WARNING, "Variable ''{0}'' is never used", NAME); UnusedElementDiagnosticFactory<JetProperty, VariableDescriptor> UNUSED_VARIABLE = UnusedElementDiagnosticFactory.create(WARNING, "Variable ''{0}'' is never used", NAME);
UnusedElementDiagnosticFactory<JetParameter, VariableDescriptor> UNUSED_PARAMETER = UnusedElementDiagnosticFactory.create(WARNING, "Parameter ''{0}'' is never used", NAME); UnusedElementDiagnosticFactory<JetParameter, VariableDescriptor> UNUSED_PARAMETER = UnusedElementDiagnosticFactory.create(WARNING, "Parameter ''{0}'' is never used", NAME);
UnusedElementDiagnosticFactory<JetNamedDeclaration, DeclarationDescriptor> ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE = UnusedElementDiagnosticFactory.create(WARNING, "Variable ''{0}'' is assigned but never accessed", NAME); UnusedElementDiagnosticFactory<JetNamedDeclaration, DeclarationDescriptor> ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE = UnusedElementDiagnosticFactory.create(WARNING, "Variable ''{0}'' is assigned but never accessed", NAME);
PsiElementOnlyDiagnosticFactory1<JetNamedDeclaration, DeclarationDescriptor> VARIABLE_WITH_REDUNDANT_INITIALIZER = PsiElementOnlyDiagnosticFactory1.create(WARNING, "Variable ''{0}'' initializer is redundant", NAME);
PsiElementOnlyDiagnosticFactory2<JetElement, JetElement, DeclarationDescriptor> UNUSED_VALUE = new PsiElementOnlyDiagnosticFactory2<JetElement, JetElement, DeclarationDescriptor>(WARNING, "The value ''{0}'' assigned to ''{1}'' is never used", NAME) { PsiElementOnlyDiagnosticFactory2<JetElement, JetElement, DeclarationDescriptor> UNUSED_VALUE = new PsiElementOnlyDiagnosticFactory2<JetElement, JetElement, DeclarationDescriptor>(WARNING, "The value ''{0}'' assigned to ''{1}'' is never used", NAME) {
@Override @Override
protected String makeMessageForA(@NotNull JetElement element) { protected String makeMessageForA(@NotNull JetElement element) {
@@ -103,17 +103,4 @@ public class JetPsiUtil {
return unquoteIdentifier(quoted); return unquoteIdentifier(quoted);
} }
} }
public static boolean isLocal(@NotNull JetElement element) {
JetClassOrObject classOrObject = PsiTreeUtil.getParentOfType(element, JetClassOrObject.class);
JetDeclarationWithBody function = PsiTreeUtil.getParentOfType(element, JetDeclarationWithBody.class);
if (function != null && PsiTreeUtil.isAncestor(classOrObject, function, false)) {
return true;
}
if (classOrObject != null && PsiTreeUtil.isAncestor(function, classOrObject, false)) {
return false;
}
if (function != null) return true;
return false;
}
} }
@@ -11,7 +11,7 @@ class C() {
} }
fun f(): Unit { fun f(): Unit {
var <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>x<!>: Int? = 1 var x: Int? = <!VARIABLE_WITH_REDUNDANT_INITIALIZER!>1<!>
x = 1 x = 1
x <!UNSAFE_INFIX_CALL!>+<!> 1 x <!UNSAFE_INFIX_CALL!>+<!> 1
x <!UNSAFE_INFIX_CALL!>plus<!> 1 x <!UNSAFE_INFIX_CALL!>plus<!> 1
@@ -18,7 +18,7 @@ fun bbb() {
return <!TYPE_MISMATCH!>1<!> return <!TYPE_MISMATCH!>1<!>
} }
fun foo(expr: StringBuilder): Int { fun foo(<!UNUSED_PARAMETER!>expr<!>: StringBuilder): Int {
val c = 'a' val c = 'a'
when(c) { when(c) {
0.chr => throw Exception("zero") 0.chr => throw Exception("zero")
@@ -211,4 +211,4 @@ fun testFunctionLiterals() {
object A {} object A {}
} }
} }
@@ -16,7 +16,7 @@ fun t1(b : Boolean) {
} }
doSmth(<!UNINITIALIZED_VARIABLE!>u<!>) doSmth(<!UNINITIALIZED_VARIABLE!>u<!>)
var <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>r<!>: String var r: String
if (b) { if (b) {
r = "s" r = "s"
} }
@@ -137,7 +137,7 @@ fun tf() : Int {
<!UNREACHABLE_CODE!>return 1<!> <!UNREACHABLE_CODE!>return 1<!>
} }
fun failtest(a : Int) : Int { fun failtest(<!UNUSED_PARAMETER!>a<!> : Int) : Int {
if (fail() || <!UNREACHABLE_CODE!>true<!>) { if (fail() || <!UNREACHABLE_CODE!>true<!>) {
} }
@@ -1,7 +1,7 @@
namespace unused_variables namespace unused_variables
fun testSimpleCases() { fun testSimpleCases() {
var <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>i<!> = 2 var i = <!VARIABLE_WITH_REDUNDANT_INITIALIZER!>2<!>
i = <!UNUSED_VALUE!>34<!> i = <!UNUSED_VALUE!>34<!>
i = 34 i = 34
doSmth(i) doSmth(i)
@@ -62,7 +62,7 @@ class MyTest() {
} }
fun testIf() { fun testIf() {
var <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>a<!> : Any var a : Any
if (1 < 2) { if (1 < 2) {
a = 23 a = 23
} }
@@ -11,7 +11,7 @@ class C() {
} }
fun f(): Unit { fun f(): Unit {
var <warning>x</warning>: Int? = 1 var x: Int? = <warning>1</warning>
x = 1 x = 1
x <error>+</error> 1 x <error>+</error> 1
x <error>plus</error> 1 x <error>plus</error> 1
@@ -13,7 +13,7 @@ fun bbb() {
return <error>1</error> return <error>1</error>
} }
fun foo(expr: StringBuilder): Int { fun foo(<warning>expr</warning>: StringBuilder): Int {
val c = 'a' val c = 'a'
when(c) { when(c) {
0.chr => throw Exception("zero") 0.chr => throw Exception("zero")
+1 -1
View File
@@ -135,7 +135,7 @@ fun tf() : Int {
<error>return 1</error> <error>return 1</error>
} }
fun failtest(a : Int) : Int { fun failtest(<warning>a</warning> : Int) : Int {
if (fail() || <error>true</error>) { if (fail() || <error>true</error>) {
} }