KT-7715 Highlight var's that can be replaced by val's

#KT-7715 Fixed
This commit is contained in:
Valentin Kipyatkov
2016-03-04 16:27:01 +03:00
parent e5b5a8db42
commit 28c5dde875
28 changed files with 350 additions and 15 deletions
+6
View File
@@ -0,0 +1,6 @@
fun foo(p: Int) {
var v1: Int
var v2: Int = 0
if (p > 0) v1 = 1 else v1 = 2
v2 = 1
}
+6
View File
@@ -0,0 +1,6 @@
fun foo(p: Int) {
var v: Int
for (i in 1..10) {
v = i
}
}
+5
View File
@@ -0,0 +1,5 @@
fun foo(p: Int) {
var v: Int
v = 0
if (p > 0) v = 1
}
@@ -0,0 +1,6 @@
fun foo(p: Int) {
var (v1, v2) = getPair()
print(v1)
}
fun getPair(): Pair<Int, String> = 1 to ""
@@ -0,0 +1,7 @@
fun foo(p: Int) {
var (v1, v2) = getPair()
print(v1)
v2 = ""
}
fun getPair(): Pair<Int, String> = 1 to ""
@@ -0,0 +1,5 @@
fun foo(p: Int) {
var (v1, v2) = getPair()
}
fun getPair(): Pair<Int, String> = 1 to ""
@@ -0,0 +1,55 @@
<problems>
<problem>
<file>withInitializer.kt</file>
<line>4</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="withInitializer.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Local 'var' can be declared as 'val'</problem_class>
<description>Can be declared as 'val'</description>
</problem>
<problem>
<file>alwaysAssigned.kt</file>
<line>2</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="alwaysAssigned.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Local 'var' can be declared as 'val'</problem_class>
<description>Can be declared as 'val'</description>
</problem>
<problem>
<file>notAssignedWhenNotUsed.kt</file>
<line>2</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="notAssignedWhenNotUsed.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Local 'var' can be declared as 'val'</problem_class>
<description>Can be declared as 'val'</description>
</problem>
<problem>
<file>twoVariables.kt</file>
<line>2</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="twoVariables.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Local 'var' can be declared as 'val'</problem_class>
<description>Can be declared as 'val'</description>
</problem>
<problem>
<file>twoVariables.kt</file>
<line>3</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="twoVariables.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Local 'var' can be declared as 'val'</problem_class>
<description>Can be declared as 'val'</description>
</problem>
<problem>
<file>desctructuringDeclaration1.kt</file>
<line>2</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="desctructuringDeclaration1.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Local 'var' can be declared as 'val'</problem_class>
<description>Can be declared as 'val'</description>
</problem>
</problems>
@@ -0,0 +1 @@
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.CanBeValInspection
@@ -0,0 +1,7 @@
fun foo(p: Int) {
var v: Int
if (p > 0) {
v = 1
print(v)
}
}
+10
View File
@@ -0,0 +1,10 @@
var global = 1
class C {
var field = 2
fun foo() {
print(field)
print(global)
}
}
+4
View File
@@ -0,0 +1,4 @@
fun foo() {
val v = 1
print(v)
}
+6
View File
@@ -0,0 +1,6 @@
fun foo(p: Int) {
var v1: Int
var v2: Int
if (p > 0) v1 = 1 else v1 = 2
v2 = 1
}
+3
View File
@@ -0,0 +1,3 @@
fun foo() {
var v: Int
}
@@ -0,0 +1,3 @@
fun foo() {
var v = 1
}
+8
View File
@@ -0,0 +1,8 @@
fun foo() {
var v1 = 1
var v2 = 2
var v3 = 3
v1 = 1
v2++
print(v3)
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.CanBeValInspection
@@ -0,0 +1,9 @@
// "Make variable immutable" "true"
fun foo(p: Int) {
<caret>var (v1, v2) = getPair()!!
v1
}
fun getPair(): Pair<Int, String>? = null
data class Pair<T1, T2>(val a: T1, val b: T2)
@@ -0,0 +1,9 @@
// "Make variable immutable" "true"
fun foo(p: Int) {
<caret>val (v1, v2) = getPair()!!
v1
}
fun getPair(): Pair<Int, String>? = null
data class Pair<T1, T2>(val a: T1, val b: T2)
@@ -0,0 +1,5 @@
// "Make variable immutable" "true"
fun foo(p: Int) {
<caret>var v: Int
if (p > 0) v = 1 else v = 2
}
@@ -0,0 +1,5 @@
// "Make variable immutable" "true"
fun foo(p: Int) {
<caret>val v: Int
if (p > 0) v = 1 else v = 2
}