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

KDOC_WITHOUT_THROWS_TAG add @throws annotation when throw inside try-catch #1862

Merged
merged 7 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -12,6 +12,7 @@ import com.saveourtool.diktat.ruleset.utils.KotlinParser
import com.saveourtool.diktat.ruleset.utils.appendNewlineMergingWhiteSpace
import com.saveourtool.diktat.ruleset.utils.findAllDescendantsWithSpecificType
import com.saveourtool.diktat.ruleset.utils.findChildAfter
import com.saveourtool.diktat.ruleset.utils.getAllChildrenWithType
import com.saveourtool.diktat.ruleset.utils.getBodyLines
import com.saveourtool.diktat.ruleset.utils.getFilePath
import com.saveourtool.diktat.ruleset.utils.getFirstChildWithType
Expand Down Expand Up @@ -40,6 +41,7 @@ import org.jetbrains.kotlin.KtNodeTypes.MODIFIER_LIST
import org.jetbrains.kotlin.KtNodeTypes.REFERENCE_EXPRESSION
import org.jetbrains.kotlin.KtNodeTypes.THIS_EXPRESSION
import org.jetbrains.kotlin.KtNodeTypes.THROW
import org.jetbrains.kotlin.KtNodeTypes.TRY
import org.jetbrains.kotlin.KtNodeTypes.TYPE_REFERENCE
import org.jetbrains.kotlin.com.intellij.lang.ASTFactory
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
Expand All @@ -54,6 +56,7 @@ import org.jetbrains.kotlin.kdoc.psi.impl.KDocTag
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.lexer.KtTokens.COLON
import org.jetbrains.kotlin.lexer.KtTokens.EQ
import org.jetbrains.kotlin.lexer.KtTokens.IDENTIFIER
import org.jetbrains.kotlin.lexer.KtTokens.WHITE_SPACE
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtCatchClause
Expand Down Expand Up @@ -197,11 +200,38 @@ class KdocMethods(configRules: List<RulesConfig>) : DiktatRule(
&& !hasReturnKdoc && !isReferenceExpressionWithSameName
}

private fun isThrowInTryCatchBlock(node: ASTNode?): Boolean {
node ?: return false
var parent = node.treeParent
while (parent != null && parent.elementType != TRY) {
parent = parent.treeParent
}
val nodeNameList = node.findAllDescendantsWithSpecificType(IDENTIFIER)
val nodeName = if (nodeNameList.isNotEmpty()) {
nodeNameList[0]
} else {
null
}

if (parent?.elementType == TRY) {
val catchNodes = parent.getAllChildrenWithType(CATCH)
val findName = catchNodes.find { catchNode ->
val catchNodeNames = catchNode.findAllDescendantsWithSpecificType(IDENTIFIER)
val matchingName = catchNodeNames.find { catchNodeName -> catchNodeName.text == nodeName?.text }
matchingName != null
}
return findName != null
}
return false
}

private fun getExplicitlyThrownExceptions(node: ASTNode): Set<String> {
val codeBlock = node.getFirstChildWithType(BLOCK)
val throwKeywords = codeBlock?.findAllDescendantsWithSpecificType(THROW)

return throwKeywords
?.asSequence()
?.filter { !isThrowInTryCatchBlock(it) }
?.map { it.psi as KtThrowExpression }
?.filter {
// we only take freshly created exceptions here: `throw IAE("stuff")` vs `throw e`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,10 @@ class KdocMethodsFixTest : FixTestBase("test/paragraph2/kdoc/package/src/main/ko
fun `KdocMethods rule should reformat code (full example)`() {
fixAndCompare("KdocMethodsFullExpected.kt", "KdocMethodsFullTested.kt")
}

@Test
@Tag(WarningNames.KDOC_WITHOUT_THROWS_TAG)
fun `@throws tag shouldn't be added`() {
fixAndCompare("KdocWithoutThrowsTagExpected.kt", "KdocWithoutThrowsTagTested.kt")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package test.paragraph2.kdoc.`package`.src.main.kotlin.com.saveourtool.diktat.kdoc.methods

/**
* @param onSuccess
* @param onFailure
*/
fun parseInputNumber(onSuccess: (number: Int) -> Unit, onFailure: () -> Unit) {
try {
val input: Int = binding.inputEditText.text.toString().toInt()
if (input < 0)
throw NumberFormatException()

onSuccess(input)
} catch (e: NumberFormatException) {
onFailure()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package test.paragraph2.kdoc.`package`.src.main.kotlin.com.saveourtool.diktat.kdoc.methods

fun parseInputNumber(onSuccess: (number: Int) -> Unit, onFailure: () -> Unit) {
try {
val input: Int = binding.inputEditText.text.toString().toInt()
if (input < 0)
throw NumberFormatException()

onSuccess(input)
} catch (e: NumberFormatException) {
onFailure()
}
}