Skip to content

Commit 671f1b4

Browse files
fedochetSpace Team
authored and
Space Team
committed
KT-67706 [FIR] Ignore DesugaredAugmentedAssign source in UnusedChecker
In the cases like `foo.bar += 10`, there is a `val <receiver> = foo.bar` variable which is also reported from `variablesWithoutReads` However, this variable has a fake source pointing to the `foo.bar` `KtDotQualifiedExpression` expression, which is impossible to report with `UNUSED_VARIABLE`, because it accepts only `KtNamedDeclaration`s ^KT-67706 Fixed
1 parent 2598443 commit 671f1b4

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/extended/UnusedChecker.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ object UnusedChecker : AbstractFirPropertyInitializationChecker(MppCheckerKind.C
6262
private val FirPropertySymbol.ignoreWarnings: Boolean
6363
get() = name == SpecialNames.UNDERSCORE_FOR_UNUSED_VAR ||
6464
source == null ||
65+
// if <receiver> variable is reported as unused,
66+
// then the assignment itself is a dead code because of its RHS expression,
67+
// which will be eventually reported
68+
source?.kind is KtFakeSourceElementKind.DesugaredAugmentedAssign ||
6569
source?.elementType == KtNodeTypes.DESTRUCTURING_DECLARATION ||
6670
initializerSource?.kind == KtFakeSourceElementKind.DesugaredForLoop
6771

compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInAssignment.fir.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ fun testAssignment() {
33
<!UNREACHABLE_CODE!><!ASSIGNED_VALUE_IS_NEVER_READ!>a<!> =<!> todo()
44
}
55

6+
class Foo {
7+
var property: Int = 0
8+
}
9+
10+
fun testClassPropertyAssignment(foo: Foo) {
11+
foo.property = 1
12+
foo<!UNREACHABLE_CODE!>.property =<!> todo()
13+
}
14+
615
fun testVariableDeclaration() {
716
<!UNREACHABLE_CODE!>val <!UNUSED_VARIABLE!>a<!> =<!> todo()
817
}
@@ -14,5 +23,10 @@ fun testPlusAssign() {
1423
a <!UNREACHABLE_CODE!>+=<!> todo()
1524
}
1625

26+
fun testClassPropertyPlusAssign(foo: Foo) {
27+
foo.property += 1
28+
foo<!UNREACHABLE_CODE!>.property<!> += todo() as Int
29+
}
30+
1731

1832
fun todo(): Nothing = throw Exception()

compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInAssignment.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ fun testAssignment() {
33
<!UNREACHABLE_CODE!>a =<!> todo()
44
}
55

6+
class Foo {
7+
var property: Int = 0
8+
}
9+
10+
fun testClassPropertyAssignment(foo: Foo) {
11+
foo.property = 1
12+
foo<!UNREACHABLE_CODE!>.property =<!> todo()
13+
}
14+
615
fun testVariableDeclaration() {
716
<!UNREACHABLE_CODE!>val <!UNUSED_VARIABLE!>a<!> =<!> todo()
817
}
@@ -14,5 +23,10 @@ fun testPlusAssign() {
1423
a <!UNREACHABLE_CODE!>+=<!> todo()
1524
}
1625

26+
fun testClassPropertyPlusAssign(foo: Foo) {
27+
foo.property += 1
28+
foo.property <!UNREACHABLE_CODE!>+=<!> todo() as Int
29+
}
30+
1731

1832
fun todo(): Nothing = throw Exception()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
package
22

33
public fun testAssignment(): kotlin.Unit
4+
public fun testClassPropertyAssignment(/*0*/ foo: Foo): kotlin.Unit
5+
public fun testClassPropertyPlusAssign(/*0*/ foo: Foo): kotlin.Unit
46
public fun testPlusAssign(): kotlin.Unit
57
public fun testVariableDeclaration(): kotlin.Unit
68
public fun todo(): kotlin.Nothing
9+
10+
public final class Foo {
11+
public constructor Foo()
12+
public final var property: kotlin.Int
13+
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
14+
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
15+
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
16+
}
17+

0 commit comments

Comments
 (0)