Skip to content

Commit

Permalink
Generic Function Removes Return Type T
Browse files Browse the repository at this point in the history
### What's done:
* fixed bug in WRONG_NEWLINES
* now don't remove return type
Closes #965
  • Loading branch information
Cheshiriks committed Jul 30, 2021
1 parent da86140 commit 9b1da63
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -335,18 +335,10 @@ 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
val expression = node.findChildByType(RETURN_KEYWORD)!!.nextCodeSibling()!!
funNode.apply {
if (needsExplicitType) {
removeRange(returnType!!.treeNext, null)
} else {
removeRange(if (colon.treePrev.elementType == WHITE_SPACE) colon.treePrev else colon, null)
}
removeRange(returnType!!.treeNext, null)
addChild(PsiWhiteSpaceImpl(" "), null)
addChild(LeafPsiElement(EQ, "="), null)
addChild(PsiWhiteSpaceImpl(" "), null)
Expand Down
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,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

0 comments on commit 9b1da63

Please sign in to comment.