Skip to content

Commit

Permalink
False positive with single expression functions that use multiple ret…
Browse files Browse the repository at this point in the history
…urn keywords (#1617)
  • Loading branch information
Cheshiriks authored Mar 14, 2023
1 parent 2a56ca7 commit 4af545e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,10 @@ class NewlinesRule(configRules: List<RulesConfig>) : DiktatRule(
private fun handleReturnStatement(node: ASTNode) {
val blockNode = node.treeParent.takeIf { it.elementType == BLOCK && it.treeParent.elementType == FUN }
val returnsUnit = node.children().count() == 1 // the only child is RETURN_KEYWORD
if (blockNode == null || returnsUnit) {
val hasMultipleReturn = node.findAllDescendantsWithSpecificType(RETURN_KEYWORD).count() > 1
if (blockNode == null || returnsUnit || hasMultipleReturn) {
// function is either already with expression body or definitely can't be converted to it
// or the function has more than one keyword `return` inside it
return
}
blockNode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,18 @@ class NewlinesRuleWarnTest : LintTestBase(::NewlinesRule) {
)
}

@Test
@Tag(WarningNames.WRONG_NEWLINES)
fun `shouldn't warn if function consists of a single return statement with a nested return`() {
lintMethod(
"""
|fun foo(): String {
| return Demo(string ?: return null)
|}
""".trimMargin(),
)
}

@Test
@Tag(WarningNames.WRONG_NEWLINES)
@Suppress("TOO_LONG_FUNCTION")
Expand Down

0 comments on commit 4af545e

Please sign in to comment.