-
-
Notifications
You must be signed in to change notification settings - Fork 792
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
Add UseRequireNotNull/UseCheckNotNull rules #3003
Conversation
Codecov Report
@@ Coverage Diff @@
## master #3003 +/- ##
============================================
- Coverage 79.76% 79.75% -0.02%
- Complexity 2493 2503 +10
============================================
Files 423 427 +4
Lines 7488 7518 +30
Branches 1409 1418 +9
============================================
+ Hits 5973 5996 +23
Misses 767 767
- Partials 748 755 +7
Continue to review full report at Codecov.
|
*/ | ||
class UseCheckNotNull(config: Config = Config.empty) : Rule(config) { | ||
companion object { | ||
private val requireFunctionFqName = FqName("kotlin.check") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private val requireFunctionFqName = FqName("kotlin.check") | |
private val checkFunctionFqName = FqName("kotlin.check") |
|
||
override fun visitCallExpression(expression: KtCallExpression) { | ||
super.visitCallExpression(expression) | ||
if (expression.isCallingWithNonNullCheckArgument(requireFunctionFqName, bindingContext)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (expression.isCallingWithNonNullCheckArgument(requireFunctionFqName, bindingContext)) { | |
if (expression.isCallingWithNonNullCheckArgument(checkFunctionFqName, bindingContext)) { |
) | ||
|
||
override fun visitCallExpression(expression: KtCallExpression) { | ||
super.visitCallExpression(expression) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a
if (bindingContext == BindingContext.EMPTY) return
just after this statement
) | ||
|
||
override fun visitCallExpression(expression: KtCallExpression) { | ||
super.visitCallExpression(expression) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a
if (bindingContext == BindingContext.EMPTY) return
just after this statement
it("reports a `require` call with a non-null check") { | ||
val code = """ | ||
fun test(i: Int?) { | ||
require(i != null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you write a test also for require(null != i)
?
it("reports a `check` call with a non-null check") { | ||
val code = """ | ||
fun test(i: Int?) { | ||
check(i != null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you write a test also for check(null != i)
?
Thanks for the great addition @t-kameyama 🙏 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍 Just a small nit
val code = """ | ||
fun test(i: Int?, j: Int?) { | ||
require(i != null) | ||
require(null != j) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: please split this in two tests. It gives us more granularity in understanding what breaks once a test fails
Cool! One rule less on my todo list xD, thanks |
Fixes #2998