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

Also check USELESS_ELVIS in UnreachableCode #6624

Merged
merged 3 commits into from
Jan 3, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ private val SAMPLE_ACTIVITY_USING_VIEW_BINDING = """

override fun onCreate(savedInstanceState: Bundle?) {
binding = ActivitySampleBinding.inflate(LayoutInflater.from(this))
binding.sampleView ?: return
binding.sampleView
schalkms marked this conversation as resolved.
Show resolved Hide resolved
setContentView(binding.root)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ class UnreachableCode(config: Config = Config.empty) : Rule(config) {

override fun visitExpression(expression: KtExpression) {
super.visitExpression(expression)
if (bindingContext.diagnostics.forElement(expression).any { it.factory == Errors.UNREACHABLE_CODE }) {
if (bindingContext.diagnostics.forElement(expression)
.any { it.factory == Errors.UNREACHABLE_CODE || it.factory == Errors.USELESS_ELVIS }
) {
report(
CodeSmell(
issue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,16 @@ class UnreachableCodeSpec(private val env: KotlinCoreEnvironment) {
""".trimIndent()
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}

@Test
fun `reports unreachable code after elvis`() {
val code = """
fun test() {
val a = 2 ?: run {
3
}
}
""".trimIndent()
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
}