Kotlin“也" Android Studio中功能奇怪的警告 [英] Kotlin "also" function strange warning in Android Studio

查看:100
本文介绍了Kotlin“也" Android Studio中功能奇怪的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大概有两个相同的代码段:

There is two presumably identical snippets:

// Verbose version
val typedArray = context.obtainStyledAttributes(attrs, styleable)
block(typedArray)
typedArray.recycle()

// One-line version
context.obtainStyledAttributes(attrs, styleable).also(block).recycle()

我想知道为什么冗长的代码块在Android Studio中看起来不错,而单行版本突出显示 obtainStyledAttributes 并给出以下警告:

I'm wonder why verbose block of code looks fine to Android Studio, whereas one-line version highlights obtainStyledAttributes and gives following warning:

此TypedArray应与#recycle()一起使用后再利用

This TypedArray should be recycled after use with #recycle()

有人知道这仅仅是一个Android Studio的皮棉检查缺陷还是单行版本实际上有问题吗?

Does anyone know is it just an Android Studio lint check flaw or something is actually wrong with one-line version?

推荐答案

这有点猜测,但是我相信这是因为静态分析工具不能保证also()返回的TypedArray是与obtainedStyledAttributes()返回的实例相同.它看到获得了TypedArray,但不能保证它被回收,因此发出警告.

This is a bit of a guess, but I believe it's because the static analysis tool can't guarantee that the TypedArray returned by also() is the same instance as the one returned by obtainedStyledAttributes(). It sees that a TypedArray is obtained but can't guarantee that it is recycled, so it issues the warning.

我会毫不犹豫地称其为短毛绒中的错误(或缺陷"),但这确实意味着在这种情况下可以忽略警告.

I would hesitate to call this a bug (or "flaw") in the linter, but it does mean that the warning can be ignored in this case.

这篇关于Kotlin“也" Android Studio中功能奇怪的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆