Skip to content

Commit 3d84e15

Browse files
sandwwraithSpace Team
authored andcommitted
[FIR] Do not set must-use status for a local function unconditionally
Use corresponding AnalysisFlag instead, to avoid unwanted warnings on local functions usages — CHECKER mode usually means that user wanted to see reports only on stdlib/libraries functions. #KT-78112 Fixed
1 parent 14a782a commit 3d84e15

File tree

12 files changed

+160
-13
lines changed

12 files changed

+160
-13
lines changed

analysis/low-level-api-fir/tests-gen/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLDiagnosticsFe10TestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

analysis/low-level-api-fir/tests-gen/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLPartialDiagnosticsFe10TestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

analysis/low-level-api-fir/tests-gen/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLReversedDiagnosticsFe10TestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsWithLatestLanguageVersionTestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/PhasedJvmDiagnosticLightTreeTestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/PhasedJvmDiagnosticPsiTestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/fir/semantics/src/org/jetbrains/kotlin/fir/declarations/FirMustUseReturnValueStatusComponent.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,10 @@ abstract class FirMustUseReturnValueStatusComponent : FirSessionComponent {
124124
containingProperty: FirPropertySymbol?,
125125
overriddenStatuses: List<FirResolvedDeclarationStatus>,
126126
): ReturnValueStatus {
127+
val analysisMode = session.languageVersionSettings.getFlag(AnalysisFlags.returnValueCheckerMode)
127128
if (isLocal) {
128-
// FIXME (KT-78112): pass through outer declaration through BodyResolveTransformer when we compute status for local functions
129-
return if (declaration is FirFunctionSymbol) ReturnValueStatus.MustUse else ReturnValueStatus.Unspecified
129+
// To compute status using annotations, getFirCallableContainerFile/getContainingDeclaration should work correctly for local declarations (KT-80564)
130+
return if (declaration is FirFunctionSymbol && analysisMode == ReturnValueCheckerMode.FULL) ReturnValueStatus.MustUse else ReturnValueStatus.Unspecified
130131
}
131132
// Implementation note: just with intersection overrides, in case we have more than one immediate parent, we take first from the list
132133
// See inheritanceChainIgnorability.kt test.
@@ -138,7 +139,7 @@ abstract class FirMustUseReturnValueStatusComponent : FirSessionComponent {
138139
// In the case of inheriting from Ignorable or Unspecified, global FULL setting has lesser priority than annotations/parent
139140
// but we want to check it here first to avoid looking through the containers
140141
val overridesIgnorableOrUnspecified = overriddenFlag == ReturnValueStatus.ExplicitlyIgnorable || overriddenFlag == ReturnValueStatus.Unspecified
141-
if (session.languageVersionSettings.getFlag(AnalysisFlags.returnValueCheckerMode) == ReturnValueCheckerMode.FULL && !overridesIgnorableOrUnspecified)
142+
if (analysisMode == ReturnValueCheckerMode.FULL && !overridesIgnorableOrUnspecified)
142143
return ReturnValueStatus.MustUse
143144

144145
if (findMustUseAmongContainers(

compiler/testData/diagnostics/tests/crv/nestedScopesInsideFile.fir.kt

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,32 @@
55

66
fun localFun() {
77
fun local(): Int = 123
8-
<!RETURN_VALUE_NOT_USED!>local()<!> //unused
8+
@IgnorableReturnValue fun localIgnorable() = ""
9+
local()
10+
localIgnorable()
911
}
1012

11-
class A {
12-
fun foo(): Int = 123
13-
fun test() {
14-
<!RETURN_VALUE_NOT_USED!>foo()<!> //unused
13+
class Outer {
14+
fun foo(): String {
15+
class Inner {
16+
@IgnorableReturnValue fun bar() {
17+
fun local() = ""
18+
local()
19+
}
20+
fun inner() = ""
21+
}
22+
Inner()
23+
Inner().inner()
24+
Inner().bar()
25+
return ""
1526
}
27+
28+
fun bar(): String = ""
29+
}
30+
31+
fun main() {
32+
<!RETURN_VALUE_NOT_USED!>Outer().foo()<!>
33+
<!RETURN_VALUE_NOT_USED!>Outer().bar()<!>
1634
}
1735

1836
/* GENERATED_FIR_TAGS: annotationUseSiteTargetFile, classDeclaration, functionDeclaration, integerLiteral, localFunction */

compiler/testData/diagnostics/tests/crv/nestedScopesInsideFile.kt

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,32 @@
55

66
fun localFun() {
77
fun local(): Int = 123
8-
local() //unused
8+
@IgnorableReturnValue fun localIgnorable() = ""
9+
local()
10+
localIgnorable()
911
}
1012

11-
class A {
12-
fun foo(): Int = 123
13-
fun test() {
14-
foo() //unused
13+
class Outer {
14+
fun foo(): String {
15+
class Inner {
16+
@IgnorableReturnValue fun bar() {
17+
fun local() = ""
18+
local()
19+
}
20+
fun inner() = ""
21+
}
22+
Inner()
23+
Inner().inner()
24+
Inner().bar()
25+
return ""
1526
}
27+
28+
fun bar(): String = ""
29+
}
30+
31+
fun main() {
32+
Outer().foo()
33+
Outer().bar()
1634
}
1735

1836
/* GENERATED_FIR_TAGS: annotationUseSiteTargetFile, classDeclaration, functionDeclaration, integerLiteral, localFunction */
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// RUN_PIPELINE_TILL: BACKEND
2+
// WITH_STDLIB
3+
4+
fun localFun() {
5+
fun local(): Int = 123
6+
@IgnorableReturnValue fun localIgnorable() = ""
7+
<!RETURN_VALUE_NOT_USED!>local()<!>
8+
localIgnorable()
9+
}
10+
11+
class Outer {
12+
fun foo(): String {
13+
class Inner {
14+
@IgnorableReturnValue fun bar() {
15+
fun local() = ""
16+
<!RETURN_VALUE_NOT_USED!>local()<!>
17+
}
18+
fun inner() = ""
19+
}
20+
<!RETURN_VALUE_NOT_USED!>Inner()<!>
21+
<!RETURN_VALUE_NOT_USED!>Inner().inner()<!>
22+
Inner().bar()
23+
return ""
24+
}
25+
26+
fun bar(): String = ""
27+
}
28+
29+
fun main() {
30+
<!RETURN_VALUE_NOT_USED!>Outer().foo()<!>
31+
<!RETURN_VALUE_NOT_USED!>Outer().bar()<!>
32+
}
33+
34+
/* GENERATED_FIR_TAGS: annotationUseSiteTargetFile, classDeclaration, functionDeclaration, integerLiteral, localFunction */

0 commit comments

Comments
 (0)