r/Kotlin • • 11d ago

Exploring Kotlin static analysis inside an IntelliJ Platform plugin using UAST

I've been exploring static code analysis for Android projects while building an IntelliJ-based developer tool.

One interesting challenge has been identifying potential lifecycle and memory-related issues by analyzing Kotlin source code rather than relying entirely on runtime heap analysis.

For example, consider patterns such as:

  • A singleton retaining an Activity or Fragment reference.
  • Coroutine scopes that outlive the lifecycle of the component that launched them.
  • Long-lived callbacks retaining UI-related objects.
  • Incorrect ownership of objects across lifecycle boundaries.

The tricky part is that detecting these patterns reliably isn't always straightforward.

Kotlin introduces several language features that make source analysis interesting: extension functions, lambdas, inline functions, delegated properties, higher-order functions, and coroutine scopes.

I've been experimenting with UAST-based inspections to analyze these patterns within the IntelliJ Platform.

A few questions for Kotlin developers who have worked on static analysis:

  1. How do you approach analyzing Kotlin-specific language constructs when building inspections?
  2. Do you prefer PSI directly, UAST, or a combination of both for Kotlin analysis?
  3. How do you handle false positives when detecting lifecycle or object-retention patterns through source code?
  4. Are there Kotlin compiler APIs or analysis tools you'd recommend exploring for this kind of work?

I'm particularly interested in approaches that go beyond simple syntax matching and can reason about object ownership and lifecycle relationships.

3 Upvotes

10 comments sorted by

2

u/paulhasreadittoo 11d ago

You can take a look at Detekt which also does static code analysis for Kotlin/Android. With Kotlin 2.5 you will get access to the Kotlin Analysis Api which might be helpful, but I don't know any specifics about it

1

u/Content-Jaguar-578 11d ago

Thanks for the suggestions! Detekt is definitely on my list to explore more deeply, especially to understand how it handles Kotlin-specific analysis and rule design.

I've been using UAST for some of the inspections so far, but I think there's a lot more to explore beyond syntax-level checks, particularly around understanding references, ownership, and lifecycle-related patterns.

I'll also look into the Kotlin Analysis API. Being able to reason about Kotlin code more accurately could be really useful for this kind of analysis. Appreciate you pointing me in that direction!

1

u/ClassKlutzy7633 11d ago

Worth reading the source of Android Lint's StaticFieldLeak check, since Lint is built on UAST and already deals with the singleton-holding-a-Context case. For false positives I'd start narrow: only flag when the field's resolved type is a subtype of Activity, Fragment or View and the holder is an object declaration or companion object, and skip anything that comes from applicationContext. The coroutine scope cases are much harder from source alone, so I'd report those as weak warnings rather than errors until you've seen how noisy they are on a few real projects.

1

u/Content-Jaguar-578 11d ago

This is really helpful, especially the suggestion to look at Android Lint's "StaticFieldLeak" implementation. I hadn't considered using its existing checks as a reference for designing the initial inspection rules.

I agree with starting narrow rather than trying to detect every possible ownership or lifecycle issue from source code. Resolved types and the holder context seem like good starting points, and distinguishing strong findings from heuristic warnings is important to avoid overwhelming developers with false positives.

The coroutine scope cases are particularly interesting because source-level patterns don't always tell the complete story about runtime lifetimes.

I'll definitely spend some time studying the Lint implementation. Thanks for sharing such a concrete direction!

1

u/ClassKlutzy7633 10d ago

Glad it helped. One thing I'd add from poking at it: Lint's detector leans on resolving the field's declared type and checking it against Context subclasses, so you get a lot of precision for very little code. If you copy that approach for coroutine scopes, flagging a stored CoroutineScope inside a long-lived holder is probably the cleanest first rule before you try anything flow-sensitive.

1

u/Content-Jaguar-578 10d ago

Sure, let me give a try ... I would love to invite you to collaborate to my open source plug-in .

1

u/ClassKlutzy7633 9d ago

Appreciate the invite. My time is a bit limited right now, but I'm happy to look at the repo, open issues where I spot false positives, and maybe pick up a small inspection once the rule structure settles.

1

u/Content-Jaguar-578 8d ago

u/ClassKlutzy7633 it's pleasure, link is here in case you wanna explore.
https://github.com/dev-vikas-soni/leak-lens 🙌🏻

1

u/ClassKlutzy7633 8d ago

Appreciate you sharing it. I'll go through the repo this weekend and start with how the inspections handle coroutine scopes, then open an issue if I spot anything useful.

1

u/Content-Jaguar-578 8d ago

Looking forward 😄