Skip to content

Fix rule LibraryCodeMustSpecifyReturnType #3155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 23, 2020
Merged

Conversation

chao2zhang
Copy link
Member

False-positive: Members without visibility modifiers in an internal class should not be reported.
False-negative: Members with protected modifiers in a public class should be reported.

This PR changes the underlying implementation of LibraryCodeMustSpecifyReturnType to Kotlin Compiler's explicit API mode, which was introduced in Kotlin 1.4
See https://kotlinlang.org/docs/reference/whatsnew14.html#explicit-api-mode-for-library-authors

This is actually blocking my company's repositories to enable LibraryCodeMustSpecifyReturnType rule because of tons of false positives.

False positive: Members without visibility modifiers in an
internal class should not be reported.

False negative: Members with protected modifiers in a public
class should be reported.
@chao2zhang
Copy link
Member Author

Note that there is an error at LibraryCodeMustSpecifyReturnTypeSpec:99
and there was a previous comment // Kotlin Script Engine reports wrongly local functions here

Please let me know if this is okay to ignore.

    io.github.detekt.test.utils.KotlinScriptException: Given Kotlin code is invalid.
        at io.github.detekt.test.utils.KotlinScriptEngine.compile(KotlinScriptEngine.kt:21)
        at io.gitlab.arturbosch.detekt.test.RuleExtensionsKt.compileAndLintWithContext(RuleExtensions.kt:47)
        at io.gitlab.arturbosch.detekt.rules.style.LibraryCodeMustSpecifyReturnTypeSpec$1$1$4$1.invoke(LibraryCodeMustSpecifyReturnTypeSpec.kt:100)
        at io.gitlab.arturbosch.detekt.rules.style.LibraryCodeMustSpecifyReturnTypeSpec$1$1$4$1.invoke(LibraryCodeMustSpecifyReturnTypeSpec.kt:12)

        Caused by:
        javax.script.ScriptException: Error: error: modifier 'internal' is not applicable to 'local function'
                            internal fun bar() = 5
                            ^
        error: modifier 'private' is not applicable to 'local function'
                            private fun foo() = 5
                            ^
            at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.compile(KotlinJsr223JvmScriptEngineBase.kt:74)
            at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.compile(KotlinJsr223JvmScriptEngineBase.kt:35)
            at io.github.detekt.test.utils.KotlinScriptEngine.compile(KotlinScriptEngine.kt:18)
            ... 3 more

@codecov
Copy link

codecov bot commented Oct 19, 2020

Codecov Report

Merging #3155 into master will decrease coverage by 0.02%.
The diff coverage is 86.44%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #3155      +/-   ##
============================================
- Coverage     79.52%   79.49%   -0.03%     
- Complexity     2592     2595       +3     
============================================
  Files           437      437              
  Lines          7852     7876      +24     
  Branches       1495     1500       +5     
============================================
+ Hits           6244     6261      +17     
- Misses          817      819       +2     
- Partials        791      796       +5     
Impacted Files Coverage Δ Complexity Δ
...kt/rules/style/LibraryCodeMustSpecifyReturnType.kt 87.50% <71.42%> (-12.50%) 12.00 <7.00> (-4.00)
...urbosch/detekt/rules/performance/SpreadOperator.kt 81.25% <83.33%> (-5.71%) 12.00 <6.00> (+4.00) ⬇️
...tlab/arturbosch/detekt/rules/naming/ClassNaming.kt 100.00% <100.00%> (ø) 5.00 <2.00> (+1.00)
...ab/arturbosch/detekt/rules/naming/PackageNaming.kt 100.00% <100.00%> (ø) 7.00 <2.00> (+1.00)
.../arturbosch/detekt/rules/style/ForbiddenComment.kt 100.00% <100.00%> (ø) 7.00 <0.00> (ø)
...b/arturbosch/detekt/rules/style/ForbiddenImport.kt 94.44% <100.00%> (ø) 10.00 <0.00> (ø)
...ekt/rules/style/RedundantVisibilityModifierRule.kt 95.55% <100.00%> (+0.10%) 10.00 <1.00> (+1.00)
...b/arturbosch/detekt/rules/style/UseCheckOrError.kt 90.00% <0.00%> (-0.91%) 8.00% <0.00%> (ø%)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1d7bd33...78f0bfc. Read the comment docs.

@arturbosch
Copy link
Member

Note that there is an error at LibraryCodeMustSpecifyReturnTypeSpec:99
and there was a previous comment // Kotlin Script Engine reports wrongly local functions here

Please let me know if this is okay to ignore.

    io.github.detekt.test.utils.KotlinScriptException: Given Kotlin code is invalid.
        at io.github.detekt.test.utils.KotlinScriptEngine.compile(KotlinScriptEngine.kt:21)
        at io.gitlab.arturbosch.detekt.test.RuleExtensionsKt.compileAndLintWithContext(RuleExtensions.kt:47)
        at io.gitlab.arturbosch.detekt.rules.style.LibraryCodeMustSpecifyReturnTypeSpec$1$1$4$1.invoke(LibraryCodeMustSpecifyReturnTypeSpec.kt:100)
        at io.gitlab.arturbosch.detekt.rules.style.LibraryCodeMustSpecifyReturnTypeSpec$1$1$4$1.invoke(LibraryCodeMustSpecifyReturnTypeSpec.kt:12)

        Caused by:
        javax.script.ScriptException: Error: error: modifier 'internal' is not applicable to 'local function'
                            internal fun bar() = 5
                            ^
        error: modifier 'private' is not applicable to 'local function'
                            private fun foo() = 5
                            ^
            at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.compile(KotlinJsr223JvmScriptEngineBase.kt:74)
            at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.compile(KotlinJsr223JvmScriptEngineBase.kt:35)
            at io.github.detekt.test.utils.KotlinScriptEngine.compile(KotlinScriptEngine.kt:18)
            ... 3 more

Hi, thanks for the PR, it looks good.
I will wait for 1.15 to merge this as it includes changing a rule quite drastically (a good imporvement).
Regarding the test case please introduce a lintWithContext which does skip the script compilation check.
The function should include a comment that this function should not be used normally:

fun BaseRule.lintWithContext(
    environment: KotlinCoreEnvironment,
    @Language("kotlin") content: String,
    @Language("kotlin") vararg additionalContents: String,
): List<Finding> {
    val ktFile = compileContentForTest(content.trimIndent())
    val additionalKtFiles = additionalContents.mapIndexed { index, additionalContent ->
        compileContentForTest(additionalContent.trimIndent(), "AdditionalTest$index.kt")
    }
    val bindingContext = getContextForPaths(environment, listOf(ktFile) + additionalKtFiles)
    val languageVersionSettings = environment.configuration.languageVersionSettings
    @Suppress("DEPRECATION")
    val dataFlowValueFactory = DataFlowValueFactoryImpl(languageVersionSettings)
    val compilerResources = CompilerResources(languageVersionSettings, dataFlowValueFactory)
    return findingsAfterVisit(ktFile, bindingContext, compilerResources)
}

fun BaseRule.compileAndLintWithContext(
    environment: KotlinCoreEnvironment,
    @Language("kotlin") content: String,
    @Language("kotlin") vararg additionalContents: String,
): List<Finding> {
    if (shouldCompileTestSnippets && additionalContents.isEmpty()) {
        KotlinScriptEngine.compile(content)
    }
    return lintWithContext(environment, content, *additionalContents)
}

@arturbosch arturbosch added this to the 1.15.0 milestone Oct 20, 2020
Copy link
Member

@schalkms schalkms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@chao2zhang
Copy link
Member Author

Note that there is an error at LibraryCodeMustSpecifyReturnTypeSpec:99
and there was a previous comment // Kotlin Script Engine reports wrongly local functions here
Please let me know if this is okay to ignore.

    io.github.detekt.test.utils.KotlinScriptException: Given Kotlin code is invalid.
        at io.github.detekt.test.utils.KotlinScriptEngine.compile(KotlinScriptEngine.kt:21)
        at io.gitlab.arturbosch.detekt.test.RuleExtensionsKt.compileAndLintWithContext(RuleExtensions.kt:47)
        at io.gitlab.arturbosch.detekt.rules.style.LibraryCodeMustSpecifyReturnTypeSpec$1$1$4$1.invoke(LibraryCodeMustSpecifyReturnTypeSpec.kt:100)
        at io.gitlab.arturbosch.detekt.rules.style.LibraryCodeMustSpecifyReturnTypeSpec$1$1$4$1.invoke(LibraryCodeMustSpecifyReturnTypeSpec.kt:12)

        Caused by:
        javax.script.ScriptException: Error: error: modifier 'internal' is not applicable to 'local function'
                            internal fun bar() = 5
                            ^
        error: modifier 'private' is not applicable to 'local function'
                            private fun foo() = 5
                            ^
            at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.compile(KotlinJsr223JvmScriptEngineBase.kt:74)
            at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.compile(KotlinJsr223JvmScriptEngineBase.kt:35)
            at io.github.detekt.test.utils.KotlinScriptEngine.compile(KotlinScriptEngine.kt:18)
            ... 3 more

Hi, thanks for the PR, it looks good.
I will wait for 1.15 to merge this as it includes changing a rule quite drastically (a good imporvement).
Regarding the test case please introduce a lintWithContext which does skip the script compilation check.
The function should include a comment that this function should not be used normally:

fun BaseRule.lintWithContext(
    environment: KotlinCoreEnvironment,
    @Language("kotlin") content: String,
    @Language("kotlin") vararg additionalContents: String,
): List<Finding> {
    val ktFile = compileContentForTest(content.trimIndent())
    val additionalKtFiles = additionalContents.mapIndexed { index, additionalContent ->
        compileContentForTest(additionalContent.trimIndent(), "AdditionalTest$index.kt")
    }
    val bindingContext = getContextForPaths(environment, listOf(ktFile) + additionalKtFiles)
    val languageVersionSettings = environment.configuration.languageVersionSettings
    @Suppress("DEPRECATION")
    val dataFlowValueFactory = DataFlowValueFactoryImpl(languageVersionSettings)
    val compilerResources = CompilerResources(languageVersionSettings, dataFlowValueFactory)
    return findingsAfterVisit(ktFile, bindingContext, compilerResources)
}

fun BaseRule.compileAndLintWithContext(
    environment: KotlinCoreEnvironment,
    @Language("kotlin") content: String,
    @Language("kotlin") vararg additionalContents: String,
): List<Finding> {
    if (shouldCompileTestSnippets && additionalContents.isEmpty()) {
        KotlinScriptEngine.compile(content)
    }
    return lintWithContext(environment, content, *additionalContents)
}

That is a great suggestion! I have made the changes and all the checks passed now.

Copy link
Member

@BraisGabin BraisGabin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add @requiresTypeResolution to the documentation?

@chao2zhang
Copy link
Member Author

requiresTypeResolution

Were you referring to create a new @OptIn annotation class?

@schalkms schalkms merged commit 2705f09 into detekt:master Oct 23, 2020
arturbosch pushed a commit that referenced this pull request Nov 15, 2020
* Fix rule LibraryCodeMustSpecifyReturnType

False positive: Members without visibility modifiers in an
internal class should not be reported.

False negative: Members with protected modifiers in a public
class should be reported.

* Create BaseRule.lintWithContext to skip KotlinScriptEngine compilation

* Add comment back

* Add @requiresTypeResolution and binding context check

* Complete the javadoc and build
arturbosch pushed a commit that referenced this pull request Dec 21, 2020
* Fix rule LibraryCodeMustSpecifyReturnType

False positive: Members without visibility modifiers in an
internal class should not be reported.

False negative: Members with protected modifiers in a public
class should be reported.

* Create BaseRule.lintWithContext to skip KotlinScriptEngine compilation

* Add comment back

* Add @requiresTypeResolution and binding context check

* Complete the javadoc and build
arturbosch pushed a commit that referenced this pull request Jan 16, 2021
* Fix rule LibraryCodeMustSpecifyReturnType

False positive: Members without visibility modifiers in an
internal class should not be reported.

False negative: Members with protected modifiers in a public
class should be reported.

* Create BaseRule.lintWithContext to skip KotlinScriptEngine compilation

* Add comment back

* Add @requiresTypeResolution and binding context check

* Complete the javadoc and build
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants