-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature. Auto-fixes for null-checks (#681)
* feature/null-checks-autofixes(#538) ### What's done: * Added fix tests * Added fix logic
- Loading branch information
Showing
10 changed files
with
218 additions
and
6 deletions.
There are no files selected for viewing
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
22 changes: 22 additions & 0 deletions
22
diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter4/NullChecksRuleFixTest.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.cqfn.diktat.ruleset.chapter4 | ||
|
||
import org.cqfn.diktat.ruleset.rules.NullChecksRule | ||
import org.cqfn.diktat.util.FixTestBase | ||
|
||
import generated.WarningNames | ||
import org.junit.jupiter.api.Tag | ||
import org.junit.jupiter.api.Test | ||
|
||
class NullChecksRuleFixTest : FixTestBase("test/paragraph4/null_checks", ::NullChecksRule) { | ||
@Test | ||
@Tag(WarningNames.AVOID_NULL_CHECKS) | ||
fun `should fix if conditions`() { | ||
fixAndCompare("IfConditionNullCheckExpected.kt", "IfConditionNullCheckTest.kt") | ||
} | ||
|
||
@Test | ||
@Tag(WarningNames.AVOID_NULL_CHECKS) | ||
fun `should fix require function`() { | ||
fixAndCompare("RequireFunctionExpected.kt", "RequireFunctionTest.kt") | ||
} | ||
} |
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
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
33 changes: 33 additions & 0 deletions
33
diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckExpected.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package test.paragraph4.null_checks | ||
|
||
fun test() { | ||
val some: Int? = null | ||
some ?: run { | ||
println("some") | ||
bar() | ||
} | ||
|
||
some?.let { | ||
println("some") | ||
bar() | ||
} | ||
|
||
if (some == null && true) { | ||
print("asd") | ||
} | ||
|
||
some?.let { | ||
print("qwe") | ||
} | ||
?: run { | ||
print("asd") | ||
} | ||
|
||
some?.let { | ||
print("qqq") | ||
} | ||
?: run { | ||
print("www") | ||
} | ||
} | ||
|
31 changes: 31 additions & 0 deletions
31
diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckTest.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package test.paragraph4.null_checks | ||
|
||
fun test() { | ||
val some: Int? = null | ||
if (some == null) { | ||
println("some") | ||
bar() | ||
} | ||
|
||
if (some != null) { | ||
println("some") | ||
bar() | ||
} | ||
|
||
if (some == null && true) { | ||
print("asd") | ||
} | ||
|
||
if (some == null) { | ||
print("asd") | ||
} else { | ||
print("qwe") | ||
} | ||
|
||
if (some != null) { | ||
print("qqq") | ||
} else { | ||
print("www") | ||
} | ||
} | ||
|
8 changes: 8 additions & 0 deletions
8
diktat-rules/src/test/resources/test/paragraph4/null_checks/RequireFunctionExpected.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package test.paragraph4.null_checks | ||
|
||
fun test() { | ||
val some: Int? = null | ||
|
||
requireNotNull(some) | ||
} | ||
|
8 changes: 8 additions & 0 deletions
8
diktat-rules/src/test/resources/test/paragraph4/null_checks/RequireFunctionTest.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package test.paragraph4.null_checks | ||
|
||
fun test() { | ||
val some: Int? = null | ||
|
||
require(some != null) | ||
} | ||
|
23 changes: 23 additions & 0 deletions
23
diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example7Expected.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.cqfn.diktat | ||
|
||
fun foo() { | ||
val prop: Int = 0 | ||
|
||
prop ?: run { | ||
println("prop is null") | ||
bar() | ||
} | ||
|
||
prop?.let { | ||
baz() | ||
gaz() | ||
} | ||
|
||
prop?.let { | ||
doAnotherSmth() | ||
} | ||
?: run { | ||
doSmth() | ||
} | ||
} | ||
|
22 changes: 22 additions & 0 deletions
22
diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example7Test.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.cqfn.diktat | ||
|
||
fun foo() { | ||
val prop: Int? = null | ||
|
||
if (prop == null) { | ||
println("prop is null") | ||
bar() | ||
} | ||
|
||
if (prop != null) { | ||
baz() | ||
gaz() | ||
} | ||
|
||
if (prop == null) { | ||
doSmth() | ||
} else { | ||
doAnotherSmth() | ||
} | ||
} | ||
|
ccfceb4
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.
I wasn't able to retrieve PDD puzzles from the code base and submit them to GitHub. If you think that it's a bug on our side, please submit it to yegor256/0pdd:
Please, copy and paste this stack trace to GitHub: