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

False positive with single expression functions that use multiple return keywords #1588

Closed
Skeletonxf opened this issue Dec 16, 2022 · 2 comments · Fixed by #1617
Closed
Assignees
Labels
bug Something isn't working
Milestone

Comments

@Skeletonxf
Copy link

Describe the bug

Diktat rewrites the following code:

data class Demo(val value: String) {
    companion object {
        fun from(string: String?): Demo? {
            return Demo(string ?: return null)
        }
    }
}

into

data class Demo(val value: String) {
    companion object {
        fun from(string: String?): Demo? = Demo(string ?: return null)
    }
}

Expected behavior

Ditkat does not rewrite the above code, because it is not valid Kotlin to use the return keyword when a function is a single expression. The single expression function body is necessary to support the null input case.

Observed behavior

Diktat rewrites the code causing a compilation error

Steps to Reproduce

Write any function body which consists of a single expression but uses patterns like ?: return foo which would not be supported as a single expression function

Adding any comment inside the function body stops diktat from rewriting the code, but this is not ideal since there is not always a need for such a comment.

Environment information

  • diktat version: 1.2.1
  • build tool (maven/gradle): gradle
  • how is diktat run (CLI, plugin, etc.): spotless (spotlessApply task)
  • kotlin version: 1.7.10
  • operating system: macOS
  • link to a project (if your project is public): NA
@Skeletonxf Skeletonxf added the bug Something isn't working label Dec 16, 2022
@nulls
Copy link
Member

nulls commented Dec 16, 2022

hi @Skeletonxf,

Thanks for reporting the issue.
We will look into it

@orchestr7 orchestr7 self-assigned this Jan 9, 2023
@Cheshiriks Cheshiriks assigned Cheshiriks and unassigned orchestr7 Mar 13, 2023
Cheshiriks added a commit that referenced this issue Mar 13, 2023
…urn keywords

### What's done:
- added check for multiple return keywords

Closes #1588
@Cheshiriks
Copy link
Member

hi @Skeletonxf,

We have fixed this bug. This should be included in the next release.

@0x6675636b796f75676974687562 0x6675636b796f75676974687562 added this to the 1.2.5 milestone Mar 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants