.use extension checking

141 views
Skip to first unread message

Ruslan Magzyumov

unread,
Nov 29, 2021, 1:29:50 AM11/29/21
to lint-dev
I need to check that use extension is used for Closable interface.

private val file = File("path")
val fileOutputStream = file.outputStream().write(999) //WARNING

private val file = File("path")
val fileOutputStream = file.outputStream().use { //NORMAL
// Some methods
}

I started off by looking for a method that returned type is inherits from Closable (in the example above is outputStream()) . How do I find the method that references on outputStream(), then to find its name?

Tor Norbye

unread,
Jan 27, 2022, 11:41:01 AMJan 27
to lint-dev
The best way to do this would be to use the data flow analyzer, since you want to not just hardcode this to work with the exact above pattern -- e.g. what if somebody wrote it as
val stream = file.outputStream()
Log.i(something)
stream.write(...)

Here you want to track the output stream across the variable assignment.


-- Tor

Reply all
Reply to author
Forward
0 new messages