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

Missing test for EmptyElseBlock rule #4206

Closed
3flex opened this issue Oct 30, 2021 · 5 comments · Fixed by #4349
Closed

Missing test for EmptyElseBlock rule #4206

3flex opened this issue Oct 30, 2021 · 5 comments · Fixed by #4349
Labels
good first issue Issue that is easy to pickup for people that are new to the project help wanted housekeeping Marker for housekeeping tasks and refactorings rules

Comments

@3flex
Copy link
Member

3flex commented Oct 30, 2021

Expected Behavior of the rule

The empty else block is reported in tests:

                fun f() {
                    var i = 0
                    if (i == 0) {
                        println(i)
                    } else {
                        
                    }
                }

Context

EmptyIfBlock is only reporting empty then blocks, not empty else blocks.
There is no test for EmptyElseBlock rule.

@3flex 3flex added the rules label Oct 30, 2021
@cortinico cortinico added false-negative good first issue Issue that is easy to pickup for people that are new to the project help wanted labels Oct 31, 2021
@severn-everett
Copy link
Contributor

There wasn't a test for EmptyElseBlock, so I created this:

class EmptyElseBlockSpec : Spek({

    val subject by memoized { EmptyElseBlock(Config.empty) }

    describe("EmptyElseBlock rule") {
        it("reports empty else block") {
            val code = """
                fun f() {
                    val i = 0
                    if (i == 0) {
                        println(i)
                    } else {
                        
                    }
                }
            """
            Assertions.assertThat(subject.compileAndLint(code)).hasSize(1)
        }
    }
})

The test passes. Has this been resolved in the month since this was posted? In any case, it'd be good to have a spec file for EmptyElseBlock in the test suite.

@3flex
Copy link
Member Author

3flex commented Dec 1, 2021

Whoops. How embarrassing... the rule indeed exists and is enabled.

If you wouldn't mind opening a PR with your test, that would be great. I was reviewing some other code and saw "else" wasn't handled in the EmptyIfBlock rule, not realising there was a separate rule to check the else block.

@3flex 3flex added housekeeping Marker for housekeeping tasks and refactorings and removed false-negative labels Dec 1, 2021
@3flex 3flex changed the title EmptyIfBlock doesn't report empty else blocks Missing test for EmptyElseBlock rule Dec 1, 2021
@severn-everett
Copy link
Contributor

Well, it might be necessary to put that original tag back on the ticket, as this fails:

        it("reports empty else blocks that are punctuated by a semicolon") {
            val code = """
                fun f() {
                    val i = 0
                    if (i == 0) {
                        println(i)
                    } else ;
                }
            """
            Assertions.assertThat(subject.compileAndLint(code)).hasSize(1)
        }

Is this correct, or is it desired behavior for the EmptyElseBlock rule to not flag this?

@3flex
Copy link
Member Author

3flex commented Dec 1, 2021

Probably a matter of opinion. Strictly speaking there's no else block there, so there's no need for this rule to flag it (since it doesn't exist).

I'm personally ok with that behaviour, though your example should probably be picked up by a rule somewhere, just not sure if that should be this rule or a new one.

@chao2zhang
Copy link
Member

Just to confirm that this issue is NOT urgent for 1.19.1, the only behavior change in the related PR is else ;.

@chao2zhang chao2zhang linked a pull request Dec 2, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Issue that is easy to pickup for people that are new to the project help wanted housekeeping Marker for housekeeping tasks and refactorings rules
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants