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

UnnecessaryApply false positive cases #2435

Closed
chao2zhang opened this issue Mar 15, 2020 · 2 comments · Fixed by #2442
Closed

UnnecessaryApply false positive cases #2435

chao2zhang opened this issue Mar 15, 2020 · 2 comments · Fixed by #2442

Comments

@chao2zhang
Copy link
Member

chao2zhang commented Mar 15, 2020

Expected Behavior

                    val content = if (5 >= 3) {
                        Intent().apply { putExtra("", 1) }
                    } else {
                        Intent()
                    }

// Used as an assignment

                    fun printCountry(block: () -> Intent) {
                        println(block())
                    }
                    fun main() {
                        printCountry {
                            println("Does nothing")
                            Intent().apply { putExtra("COUNTRY", "us") }
                        }
                    }

//Used as the last statement of a block inside lambda.

Both cases should not report an error as the receiver of the apply is used.

Observed Behavior

Both cases have an error reported.

Context

I am applying detekt to my company's codebase and find these unexpected behaviors.

Your Environment

  • Version of detekt used: 1.7.0-beta2 (Latest trunk)
@chao2zhang
Copy link
Member Author

Thank you for the quick turnaround @t-kameyama!

@BraisGabin
Copy link
Member

You are right, this should not be flagged by detekt. But apply is not necessary in your specific case because putExtra implements a fluent api.

I know very little about how far can we go with type resolution but maybe we can even detect the fluent apis (methods that returns this) and flag the uses of apply there.

@arturbosch arturbosch added this to the 1.7.0 milestone Mar 19, 2020
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