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

Generic Function Removes Return Type T #1017

Merged
merged 7 commits into from
Aug 2, 2021
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 @@ -318,7 +318,7 @@ class NewlinesRule(configRules: List<RulesConfig>) : DiktatRule(
}
}

@Suppress("UnsafeCallOnNullableType")
@Suppress("UnsafeCallOnNullableType", "AVOID_NULL_CHECKS")
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
Expand All @@ -335,19 +335,16 @@ class NewlinesRule(configRules: List<RulesConfig>) : DiktatRule(
WRONG_NEWLINES.warnAndFix(configRules, emitWarn, isFixMode,
"functions with single return statement should be simplified to expression body", node.startOffset, node) {
val funNode = blockNode.treeParent
// if return type is not Unit, then there should be type specification
// otherwise code won't compile and colon being null is correctly invalid
val colon = funNode.findChildByType(COLON)!!
val returnType = (funNode.psi as? KtNamedFunction)?.typeReference?.node
val needsExplicitType = returnType != null && (funNode.psi as? KtNamedFunction)?.isRecursive() == true
Cheshiriks marked this conversation as resolved.
Show resolved Hide resolved
val expression = node.findChildByType(RETURN_KEYWORD)!!.nextCodeSibling()!!
val blockNode = funNode.findChildByType(BLOCK)
funNode.apply {
if (needsExplicitType) {
removeRange(returnType!!.treeNext, null)
} else {
removeRange(if (colon.treePrev.elementType == WHITE_SPACE) colon.treePrev else colon, null)
if (returnType != null) {
Cheshiriks marked this conversation as resolved.
Show resolved Hide resolved
removeRange(returnType.treeNext, null)
addChild(PsiWhiteSpaceImpl(" "), null)
} else if (blockNode != null) {
removeChild(blockNode)
}
addChild(PsiWhiteSpaceImpl(" "), null)
addChild(LeafPsiElement(EQ, "="), null)
addChild(PsiWhiteSpaceImpl(" "), null)
addChild(expression.clone() as ASTNode, null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1117,4 +1117,14 @@ class NewlinesRuleWarnTest : LintTestBase(::NewlinesRule) {
rulesConfigList = rulesConfigListShort
)
}

@Test
@Tag(WarningNames.WRONG_NEWLINES)
fun `not complaining on fun without return type`() {
lintMethod(
"""
|fun foo(x: Int, y: Int) = x + y
""".trimMargin(),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin",
LintError(9, 3, "$DIKTAT_RULE_SET_ID:empty-block-structure", EMPTY_BLOCK_STRUCTURE_ERROR.warnText() +
" empty blocks are forbidden unless it is function with override keyword", false),
LintError(12, 10, "$DIKTAT_RULE_SET_ID:kdoc-formatting", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false),
LintError(14, 3, "$DIKTAT_RULE_SET_ID:kdoc-formatting", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false),
LintError(19, 15, "$DIKTAT_RULE_SET_ID:kdoc-formatting", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false)
LintError(14, 8, "$DIKTAT_RULE_SET_ID:kdoc-formatting", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false),
LintError(19, 20, "$DIKTAT_RULE_SET_ID:kdoc-formatting", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false)
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package test.paragraph3.newlines

fun foo() = "lorem ipsum"
fun foo(): String = "lorem ipsum"

fun foo() = "lorem ipsum"
fun foo():String = "lorem ipsum"

fun foo() = "lorem ipsum"
fun foo() : String = "lorem ipsum"

fun recFoo(): String = "lorem " + recFoo()

fun recFoo():String = "lorem " + recFoo()

fun recFoo(): String = "lorem " + recFoo()

fun foo() = "lorem ipsum"
fun foo() = "lorem ipsum"

fun foo() = println("Logging")
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ fun recFoo(): String{
return "lorem " + recFoo()
}

fun foo() = "lorem ipsum"
fun foo() = "lorem ipsum"

fun foo() {
return println("Logging")
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fun foo(list: List<Bar>?) {
?:foobar
}

fun bar(x :Int,y:Int) = x+ y
fun bar(x :Int,y:Int) :Int = x+ y

fun goo() {
x.map()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package test.paragraph3.newlines

class Example {
fun doubleA() = 2 * a
fun doubleA() = 2 * a
fun doubleA(): Int = 2 * a
fun doubleA(): Int = 2 * a
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Example {
* @param y
* @return
*/
fun bar(x: Int, y: Int) = x + y
fun bar(x: Int, y: Int): Int = x + y

/**
* @param sub
Expand All @@ -36,7 +36,7 @@ class Example {
* @return
*/
fun foo(x: Int,
y: Int) = x +
y: Int): Int = x +
(y +
bar(x, y)
)
Expand Down