Skip to content
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

False positive with single expression functions that use multiple return keywords #1617

Merged
merged 2 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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