Created
March 22, 2019 17:06
-
-
Save davidvavra/4f620ab3c5d3dc605439c3a2e2629e4c to your computer and use it in GitHub Desktop.
Lint check for detecting non-null assertion (!!) in your code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.android.tools.lint.client.api.UElementHandler | |
import com.android.tools.lint.detector.api.* | |
import org.jetbrains.uast.UElement | |
import org.jetbrains.uast.UPostfixExpression | |
class NonNullAssertionDetector : Detector(), Detector.UastScanner { | |
override fun getApplicableUastTypes(): List<Class<out UElement>>? { | |
return listOf(UPostfixExpression::class.java) | |
} | |
override fun createUastHandler(context: JavaContext): UElementHandler? { | |
return object : UElementHandler() { | |
override fun visitPostfixExpression(node: UPostfixExpression) { | |
if (node.asRenderString().contains("!!")) { | |
context.report( | |
NON_NULL_ASSERTION_ISSUE, | |
node, | |
context.getLocation(node), | |
"This code includes non-null assertion (!!)" | |
) | |
} | |
} | |
} | |
} | |
} | |
val NON_NULL_ASSERTION_ISSUE = Issue.create( | |
"NonNullAssertion", | |
"Non-null assertion (!!) used", | |
"Don't use non-null assertions (!!). Design your code better to avoid needing it in the first place. As a last resort, replace it with checkNotNull() function.", | |
Implementation(NonNullAssertionDetector::class.java, Scope.JAVA_FILE_SCOPE), | |
"https://android.jlelse.eu/how-to-remove-all-from-your-kotlin-code-87dc2c9767fb", | |
Category.CORRECTNESS, | |
5, | |
Severity.WARNING | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Loved your gist for checking NON_NULL_ASSERTION_ISSUE. Just added it to my project.
My only comment is that you should have a test for it as well! Feel free to use this: