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 5 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 All @@ -62,6 +65,7 @@ import org.jetbrains.kotlin.psi.KtFunction
import org.jetbrains.kotlin.psi.KtThrowExpression
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
import org.jetbrains.kotlin.psi.psiUtil.referenceExpression
import java.lang.Class.forName

/**
* This rule checks that whenever the method has arguments, return value, can throw exceptions,
Expand Down Expand Up @@ -197,11 +201,42 @@ 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
}
diphtongue marked this conversation as resolved.
Show resolved Hide resolved
val nodeName = node.findAllDescendantsWithSpecificType(IDENTIFIER).firstOrNull() ?: return false

if (parent?.elementType == TRY) {
val catchNodes = parent.getAllChildrenWithType(CATCH)
val findNodeWithMatchingName = catchNodes.firstOrNull { catchNode ->
val matchingNameForCatchNode = catchNode.findAllDescendantsWithSpecificType(IDENTIFIER)
diphtongue marked this conversation as resolved.
Show resolved Hide resolved
.firstOrNull { catchNodeName ->
nodeName.text == catchNodeName.text || try {
val nodeClass = forName("java.lang.${nodeName.text}")
val nodeInstance = nodeClass.getDeclaredConstructor().newInstance()
val catchNodeClass = forName("java.lang.${catchNodeName.text}")
catchNodeClass.isInstance(nodeInstance)
kgevorkyan marked this conversation as resolved.
Show resolved Hide resolved
} catch (e: ClassNotFoundException) {
false
}
}
matchingNameForCatchNode != null
}
return findNodeWithMatchingName != 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 inside try-catch`() {
fixAndCompare("KdocWithoutThrowsTagExpected.kt", "KdocWithoutThrowsTagTested.kt")
DrAlexD marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,81 @@ class KdocMethodsTest : LintTestBase(::KdocMethods) {
)
}

@Test
@Tag(WarningNames.KDOC_WITHOUT_THROWS_TAG)
fun `No warning when catch without matching throw`() {
diphtongue marked this conversation as resolved.
Show resolved Hide resolved
lintMethod(
"""
/**
* Test method
* @param a: Int - dummy integer
* @return doubled value
*/
fun foo(a: Int): Int {
try {
if (a < 0)
throw NumberFormatExecption()
} catch (e: ArrayIndexOutOfBounds) {
print(1)
} catch (e: NullPointerException) {
print(2)
} catch (e: NumberFormatExecption) {
print(3)
}
return 2 * a
}
""".trimIndent())
}

@Test
@Tag(WarningNames.KDOC_WITHOUT_THROWS_TAG)
fun `Warning when throw doesn't have matching catch`() {
lintMethod(
"""
/**
* Test method
* @param a: Int - dummy integer
* @return doubled value
*/
fun foo(a: Int): Int {
try {
if (a < 0)
throw NumberFormatException()
throw NullPointerException()
throw NoSuchElementException()
} catch (e: NoSuchElementException) {
print(1)
} catch (e: IllegalArgumentException) {
print(2)
}
return 2 * a
}
""".trimIndent(),
DiktatError(1, 1, ruleId, "${KDOC_WITHOUT_THROWS_TAG.warnText()} foo (NullPointerException)", true))
}

@Test
@Tag(WarningNames.KDOC_WITHOUT_THROWS_TAG)
fun `No warning when throw has matching catch, which is parent exception to throw`() {
lintMethod(
"""
/**
* Test method
* @param a: Int - dummy integer
* @return doubled value
*/
fun foo(a: Int): Int {
try {
if (a < 0)
throw NumberFormatException()
} catch (e: IllegalArgumentException) {
print(1)
}
return 2 * a
}
""".trimIndent())
}

@Test
@Tag(WarningNames.MISSING_KDOC_TOP_LEVEL)
fun `do not force documentation on standard methods`() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package test.paragraph2.kdoc.`package`.src.main.kotlin.com.saveourtool.diktat.kdoc.methods

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

onSuccess(input)
} catch (e: IllegalArgumentException) {
onFailure()
} catch (e: NullPointerException) {
onFailure()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package test.paragraph2.kdoc.`package`.src.main.kotlin.com.saveourtool.diktat.kdoc.methods
diphtongue marked this conversation as resolved.
Show resolved Hide resolved

/**
* @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()
throw ArrayIndexOutOfBounds()
throw NullPointerException()

onSuccess(input)
} catch (e: IllegalArgumentException) {
onFailure()
} catch (e: NullPointerException) {
onFailure()
}
}
Loading