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

Prevent multiple statements on the same line #1078

Closed
hiqua opened this issue Feb 11, 2021 · 3 comments · Fixed by #2065
Closed

Prevent multiple statements on the same line #1078

hiqua opened this issue Feb 11, 2021 · 3 comments · Fixed by #2065

Comments

@hiqua
Copy link

hiqua commented Feb 11, 2021

Duplicates

I could not find a matching issue, but I find it hard to believe that it doesn't exist yet. Apologies if this is indeed a duplicate.

Expected Rule behavior

Do not allow multiple statements on the same line. ktlint does not complain with the following:

class MissingRules {
    fun f() {
        val a = 3; val b = 2
    }
}

Here, a and b should be declared separately.

Additional information

One statement per line
Each statement is followed by a line break. Semicolons are not used.
@paul-dingemans
Copy link
Collaborator

I am not aware of any language constructs in Kotlin that use ; as separator between elements.

@atulgpt
Copy link
Contributor

atulgpt commented May 15, 2023

Hi, @paul-dingemans Should we disallow the semicolon itself? Even code like the below is possible in Kotlin. Which are not reported by ktlint

public fun f1() {
    val a = 3; val b = 2
}; public fun f2() {
    val a = 3; val b = 2
}
f1(); f2()
public class A {
    
}; public class B {
    
}
public class A; public class B

@paul-dingemans
Copy link
Collaborator

Semi colons can not be removed unconditionally. Ktlint already has a NoSemicolonsRule which removes redundant semi colons. That rule contains references to two interesting exceptions in which the semi colon is actually relevant:

I think it would be best that the statement wrapping rule is just wrapping (e.g. inserting a new line after the semi colon) and not removing the semi colon. And the NoSemicolonRule will then take care of removal of semi colons like it currently does. For this it has to be enforce that the NoSemicolon runs after the statement wrapping rule. This can be done by adding a VisitorModifier.RunAfterRule in the NoSemicolonRule (this rule may run regardless whether the statement wrapping rule is loaded/enabled).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants