-
-
Notifications
You must be signed in to change notification settings - Fork 755
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
VarCouldBeVal
reported on private lateinit var
#4731
Comments
I'm getting the same issue |
Do those classes have any type of difference from any other? An annotation, an interface, a name pattern... Something? |
Same issue in simplest test class: internal class LoaderTest {
private lateinit var wireMockServer: WireMockServer
// test methods there
} Type resolution enabled. Field initialized via reflection (not in |
In my case, the only difference is the presence of a |
I don't like the idea to just ignore But if there is not other way to suppres those we could add a flag |
Actually, I can't imagine such real false negatives: class C {
private lateinit var a: String
init {
a = "1"
}
} But there the problem is not So because there is no |
I agree with @vladimirfx, I can't see any use case for |
A lot of android devs use If a project uses a library that forces you to use lateinit without setters it's a one time configuration. But, if you don't use such a library, you will never change that configuration to For that reason I think that |
The only time I ever see it used is with Dagger(-esque) member injection. What other cases have you seen? Expecting use of a setter is a bit strange as then it must be nullable and changes semantics. Or they make get() possibly throw if it's not been set yet, and which point it's just reimplementing |
The use cases that I usually see are like this: private lateinit var itemId: String
fun onCreate(...) {
itemId = intent.getExtra(...)
} I agree that that usage of And, for this rule, I think that it's better to add |
Steps to Reproduce
Context
Per #4664 (comment), I'm not sure our use-case is mainstream enough to warrant a change in detekt, but I'd like to bring it up as a datapoint anyways in case there there is additional discussion in the future.
As part of my work I develop plugins for JetBrains IDEs.
We have a number of older Kotlin source files backing visual forms designed using the JetBrains GUI Designer. I'm fuzzy on all the details, but how this generally works is that there's instrumentation at compile time that injects constructors and all the necessary method calls needed to setup the Swing components correctly.
For example, the code snippet above would result in decompiled bytecode that might look like the following:
Obviously since detekt acts on source code and not bytecode, it can't know that the fields actually end up initialized.
The
VarCouldBeVal
rule is still very useful for us, so as a workaround to this regression we'd likely either mark the ~60 or so instances in our codebase with an annotation or throw it in a baseline file and call it a day.Your Environment
The text was updated successfully, but these errors were encountered: