这之前的文章中,我们介绍过如果通过Kotlin编译器参数实现将所有的warnings按照errors对待,主要的实现方法是这样
//Code to be added
kotlinOptions {
allWarningsAsErrors = true
}
那么问题可能就会被提出来,开启这一选项有什么好处呢,毕竟我需要修改很多文件。
通常情况下,开启后的作用,我们可以归纳为如下
如下,我们会通过一些实践来说明一些问题
@Suppress("NOTHING_TO_INLINE")
inline fun String?.isNotNullNorEmpty(): Boolean {
//Expected performance impact of inlining
// 'public inline fun String?.isNotNullNorEmpty(): Boolean
// defined in com.example.warningsaserrorscases in file NothingToInlineWarnings.kt'
// is insignificant.
// Inlining works best for functions with parameters of functional types
return this != null && this.isNotEmpty()
}
@Suppress("NOTHING_TO_INLINE")
压制编译器警告public class RequestManager {
public static RequestManager sInstance = new RequestManager();
private static class TimelineRequest {
public String from;
}
public TimelineRequest getTimelineRequest() {
return new TimelineRequest();
}
}
fun testInaccessibleType() {
//Type RequestManager.TimelineRequest! is inaccessible in this context
// due to: private open class TimelineRequest defined
// in com.example.warningsaserrorscases.RequestManager
@Suppress("INACCESSIBLE_TYPE")
RequestManager.sInstance.timelineRequest
}
testInaccessibleType
无法访问TimelineRequest
的属性和方法TimelineRequest
为public,而非private@Suppress("INACCESSIBLE_TYPE")
压制警告fun <T> Any.toType(): T? {
//Unchecked cast: Any to T
@Suppress("UNCHECKED_CAST")
return this as? T
}
this as? T
属于未检查的类型转换,可能在运行时抛出转换异常@Suppress("UNCHECKED_CAST")
压制警告//a better way
inline fun <reified T> Any.toType(): T? {
return if (this is T) {
this
} else {
null
}
}
fun testEnum1() {
//Enum argument can be null in Java, but exhaustive when contains no null branch
when(SeasonUtil.getCurrentSeason()) {
Season.SPRING -> println("Spring")
Season.SUMMER -> println("Summer")
Season.FALL -> println("Fall")
Season.WINTER -> println("Winter")
//else -> println("unknown")
}
}
fun testEnum2() {
//Enum argument can be null in Java, but exhaustive when contains no null branch
@Suppress("WHEN_ENUM_CAN_BE_NULL_IN_JAVA")
when(SeasonUtil.getCurrentSeason()) {
Season.SPRING -> println("Spring")
Season.SUMMER -> println("Summer")
Season.FALL -> println("Fall")
Season.WINTER -> println("Winter")
}
}
SeasonUtil.getCurrentSeason()
可能为nullelse -> println("unknown")
处理when的缺省情况@Suppress("WHEN_ENUM_CAN_BE_NULL_IN_JAVA")
压制警告interface OnViewClickedListener {
fun onViewClicked(viewId: Int)
}
fun testParameterNameChangedOnOverride() {
// The corresponding parameter in the supertype 'OnViewClickedListener'
// is named 'viewId'.
// This may cause problems when calling this function with named arguments.
object : OnViewClickedListener {
override fun onViewClicked(@Suppress("PARAMETER_NAME_CHANGED_ON_OVERRIDE") id: Int) {
println("onViewClicked id=$id")
}
}
}
fun testNameShadowing(message: String) {
run {
//Name shadowed: message
@Suppress("NAME_SHADOWING") val message = "Hello World"
println(message)
}
}
testNameShadowing
的message命名一致时,就发生了所谓的Name shadowing(命名遮挡)fun testUnnecessarySafeCall(message: String) {
@Suppress("UNNECESSARY_SAFE_CALL")
println(message?.toIntOrNull())
}
Intrinsics
做参数非空的与判断fun testSenselessComparison(message: String) {
//Condition 'message != null' is always 'true'
@Suppress("SENSELESS_COMPARISON")
if (message != null) {
}
}
Intrinsics
做参数非空的与判断fun testUncessaryNotNullAssertion(message: String) {
//Unnecessary non-null assertion (!!) on a non-null receiver
// of type String
@Suppress("UNNECESSARY_NOT_NULL_ASSERTION")
println(message!!.toIntOrNull())
}
Intrinsics
做参数非空的与判断fun testUselessIsCheck(message: String) {
//Check for instance is always 'true'
@Suppress("USELESS_IS_CHECK")
if (message is String) {
}
}
Intrinsics
做参数非空的与判断fun testVariableWithRedundantInitializer() {
//Variable 'message' initializer is redundant
@Suppress("VARIABLE_WITH_REDUNDANT_INITIALIZER") var message: String? = null;
message = System.currentTimeMillis().toString()
println(message)
}
fun testGetDrawable(context: Context) {
@Suppress("DEPRECATION")
context.resources.getDrawable(R.mipmap.ic_launcher)
}
建议的方法是寻找替代弃用方法的其他方法,以getDrawable为例,我们可以使用
ContextCompat.getDrawable(getActivity(), R.drawable.name);
ResourcesCompat.getDrawable(getResources(), R.drawable.name, null);
ResourcesCompat.getDrawable(getResources(), R.drawable.name, anotherTheme);
fun showMessage(message: String, extra: String?) {
println(message)
}
解决方法
@Suppress("UNUSED_PARAMETER")
压制警告fun forEachList() {
listOf<String>("Hello", "World").forEachIndexed { index, s ->
println(s)
}
}
index
改成占位符_
@Suppress("UNUSED_ANONYMOUS_PARAMETER")
压制警告fun unusedVariable() {
@Suppress("UNUSED_VARIABLE") val currentTimeStamp = System.currentTimeMillis()
println("unusedVariable")
}
fun test(status: Int) {
when(status) {
1 -> "First"
2 -> "Second"
else -> "Else"
}
}
Suppress
压制警告fun testUnusedValue() {
// The value '"Hello"' assigned to 'var message: String?
// defined in com.example.warningsaserrorscases.test' is never used
@Suppress("ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE") var message: String? = null
@Suppress("UNUSED_VALUE")
message = "Hello"
}
Suppress
压制警告@Suppress
主要目的是显示警告的名称,而不是提倡大家使用压制的方式处理警告。以上。