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

Support KDoc generation for rethrown exceptions #990

Merged
merged 2 commits into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -32,6 +32,7 @@ import org.cqfn.diktat.ruleset.utils.splitPathToDirs
import com.pinterest.ktlint.core.ast.ElementType
import com.pinterest.ktlint.core.ast.ElementType.BLOCK
import com.pinterest.ktlint.core.ast.ElementType.CALL_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.CATCH
import com.pinterest.ktlint.core.ast.ElementType.COLON
import com.pinterest.ktlint.core.ast.ElementType.DOT_QUALIFIED_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.EQ
Expand All @@ -51,8 +52,12 @@ import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl
import org.jetbrains.kotlin.com.intellij.psi.tree.TokenSet
import org.jetbrains.kotlin.kdoc.parser.KDocKnownTag
import org.jetbrains.kotlin.kdoc.psi.impl.KDocTag
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtCatchClause
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtThrowExpression
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
import org.jetbrains.kotlin.psi.psiUtil.referenceExpression

/**
Expand Down Expand Up @@ -92,7 +97,7 @@ class KdocMethods(configRules: List<RulesConfig>) : DiktatRule(

val (missingParameters, kDocMissingParameters) = getMissingParameters(node, kdocTags)

val explicitlyThrownExceptions = getExplicitlyThrownExceptions(node)
val explicitlyThrownExceptions = getExplicitlyThrownExceptions(node) + getRethrownExceptions(node)
val missingExceptions = explicitlyThrownExceptions
.minus(kdocTags
?.filter { it.knownTag == KDocKnownTag.THROWS }
Expand Down Expand Up @@ -154,12 +159,38 @@ class KdocMethods(configRules: List<RulesConfig>) : DiktatRule(
val codeBlock = node.getFirstChildWithType(BLOCK)
val throwKeywords = codeBlock?.findAllDescendantsWithSpecificType(THROW)
return throwKeywords
?.asSequence()
?.map { it.psi as KtThrowExpression }
?.filter {
// we only take freshly created exceptions here: `throw IAE("stuff")` vs `throw e`
it.thrownExpression is KtCallExpression
}
?.mapNotNull { it.thrownExpression?.referenceExpression()?.text }
?.toSet()
?: emptySet()
}

@Suppress("UnsafeCallOnNullableType")
private fun getRethrownExceptions(node: ASTNode) = node.findAllDescendantsWithSpecificType(CATCH).flatMap { catchClauseNode ->
// `parameterList` is `@Nullable @IfNotParsed`
(catchClauseNode.psi as KtCatchClause).parameterList!!.parameters
.filter {
// `catch (_: Exception)` - parameter can be anonymous
it.name != "_"
}
.filter { param ->
// check whether caught parameter is rethrown in the same catch clause
(catchClauseNode.psi as KtCatchClause).catchBody?.collectDescendantsOfType<KtThrowExpression>()?.any {
it.thrownExpression?.referenceExpression()?.text == param.name
} == true
}
.map {
// parameter in catch statement `catch (e: Type)` should always have type
it.typeReference!!.text
}
}
.toSet()

@Suppress("UnsafeCallOnNullableType")
private fun handleParamCheck(node: ASTNode,
kdoc: ASTNode?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,22 @@ class Example {
if (true) throw IllegalStateException("Lorem ipsum")
else throw IllegalAccessException("Dolor sit amet")
}

/**
* @throws RuntimeException
*/
fun test6() {
try {
foo()
} catch (_: NullPointerException) {
println("NPE!")
} catch (e: RuntimeException) {
println("Whoops...")
throw e
} catch (e: Error) {
val x = IllegalStateException()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be create issue on subject to improve missingExceptions detection to cover this case? When exception throws via local variable?

Or at least add TODO in code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

println("Nothing to do here")
throw x
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,19 @@ class Example {
if (true) throw IllegalStateException("Lorem ipsum")
else throw IllegalAccessException("Dolor sit amet")
}

fun test6() {
try {
foo()
} catch (_: NullPointerException) {
println("NPE!")
} catch (e: RuntimeException) {
println("Whoops...")
throw e
} catch (e: Error) {
val x = IllegalStateException()
println("Nothing to do here")
throw x
}
}
}