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

Kotlin: exclude loop variables on ranges from 'unused locals' check #11032

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tamasvajk
Copy link
Contributor

@tamasvajk tamasvajk commented Oct 28, 2022

Based on #11014.

@tamasvajk tamasvajk changed the title otlin: exclude loop variables on ranges from 'unused locals' check Kotlin: exclude loop variables on ranges from 'unused locals' check Oct 28, 2022
@tamasvajk tamasvajk force-pushed the kotlin-unused-for-loop-var branch from 816f99a to 9662750 Compare Oct 28, 2022
@@ -26,5 +26,10 @@ where
not exists(getARead(v)) and
// Discarded exceptions are covered by another query.
not exists(CatchClause cc | cc.getVariable().getVariable() = v) and
// Exclude common Kotlin pattern: `for(i in x..y) { ... }`
Copy link
Contributor

@smowton smowton Oct 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clarify what we're excluding? I guess not i; something synthetic instead?

Copy link
Contributor Author

@tamasvajk tamasvajk Oct 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're excluding i. I've seen the below pattern in a couple of variations:

for (i in 1..5) {
  val frame = input.readFrame(Long.MAX_VALUE, 0)
  ... // not using i
}

Copy link
Contributor

@smowton smowton Oct 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a bad pattern to me considering https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/repeat.html exists, but I guess if it happens often enough it's more likely to be noise.

Copy link
Contributor Author

@tamasvajk tamasvajk Oct 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found it a couple of times in ktor, that's why I thought I'd exclude it. Also, I don't think there's a real alternative. I haven't seen repeat before, but repeat takes a lambda (Int) -> Unit, where we wouldn't use the Int parameter, so it's the same issue. (I guess in a lambda we can use an implicit parameter, or a parameter named _, so the useless parameter check would not trigger, but in essence it's the same issue)

@tamasvajk tamasvajk force-pushed the kotlin-unused-for-loop-var branch from 9662750 to b221a6a Compare Oct 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants