Skip to content

Commit

Permalink
Merge branch 'master' into bugfix/incorrect_warning_offset
Browse files Browse the repository at this point in the history
  • Loading branch information
kgevorkyan authored Aug 3, 2021
2 parents 78d7c2f + 88f5d64 commit 7186c4a
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 22 deletions.
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
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) {
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

0 comments on commit 7186c4a

Please sign in to comment.