fix for KtNamedFunction.isMainFunction() (#3641)

* fix for KtNamedFunction.isMainFunction()
This commit is contained in:
Krzysiek Kruczyński
2021-04-02 18:15:13 +02:00
committed by GitHub
parent ed1ee8f15e
commit 90d5efc0f1
3 changed files with 6 additions and 2 deletions

View File

@@ -223,6 +223,7 @@ If you contributed to detekt but your name is not in the list, please feel free
- [Adam Kobor](https://github.com/adamkobor) - New rule: MultilineLambdaItParameter
- [Slawomir Czerwinski](https://github.com/sczerwinski) - Rule improvement: FunctionOnlyReturningConstant
- [Ivo Smid](https://github.com/bedla) - Fix Local development on Windows
- [Krzysztof Kruczynski](https://github.com/krzykrucz) - Rule fix: ThrowingExceptionInMain, ExitOutsideMain
### Mentions

View File

@@ -34,7 +34,9 @@ private fun KtNamedFunction.hasMainSignature() =
this.name == "main" && this.isPublicNotOverridden() && this.hasMainParameter()
private fun KtNamedFunction.hasMainParameter() =
valueParameters.isEmpty() || valueParameters.size == 1 && valueParameters[0].typeReference?.text == "Array<String>"
valueParameters.isEmpty() ||
(valueParameters.size == 1 && valueParameters[0].typeReference?.text == "Array<String>") ||
(valueParameters.size == 1 && valueParameters[0].isVarArg && valueParameters[0].typeReference?.text == "String")
private fun KtNamedFunction.isMainInsideObject() =
this.name == "main" &&

View File

@@ -14,9 +14,10 @@ class ThrowingExceptionInMainSpec : Spek({
it("reports a runnable main function which throws an exception") {
val code = """
fun main(args: Array<String>) { throw IllegalArgumentException() }
fun main(vararg args: String) { throw IllegalArgumentException() }
fun main() { throw IllegalArgumentException() }
"""
assertThat(subject.compileAndLint(code)).hasSize(2)
assertThat(subject.compileAndLint(code)).hasSize(3)
}
it("reports runnable main functions with @JvmStatic annotation which throw an exception") {