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 21d073b
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 11 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
@@ -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
}

0 comments on commit 21d073b

Please sign in to comment.