Skip to content

Commit 76e2ab4

Browse files
mateoguzmanafacebook-github-bot
authored andcommitted
Kotlin: fix static code analysis weak warnings (3/n) (#52206)
Summary: Static code analysis reports several weak warnings, many of which seem to be leftovers after Kotlin migration. This PR addresses quite a few: - [Return or assignment can be lifted out](https://www.jetbrains.com/help/inspectopedia/LiftReturnOrAssignment.html) - [Verbose nullability and emptiness check](https://www.jetbrains.com/help/inspectopedia/VerboseNullabilityAndEmptiness.html) - [Size check can be replaced with 'isNotEmpty()'](https://www.jetbrains.com/help/inspectopedia/ReplaceSizeCheckWithIsNotEmpty.html) ## Changelog: [INTERNAL] - Kotlin: fix static code analysis weak warnings (3/n) Pull Request resolved: #52206 Test Plan: ```sh yarn android yarn test-android ``` Reviewed By: javache Differential Revision: D77209766 Pulled By: cortinico fbshipit-source-id: c154ec6578125c16ad37c9dd15295a2edcacee4a
1 parent 35dba09 commit 76e2ab4

10 files changed

Lines changed: 44 additions & 49 deletions

File tree

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/JavaOnlyArray.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,14 @@ public class JavaOnlyArray : ReadableArray, WritableArray {
134134

135135
override fun toString(): String = backingList.toString()
136136

137-
override fun equals(other: Any?): Boolean {
138-
if (this === other) {
139-
return true
140-
} else if (other == null || javaClass != other.javaClass) {
141-
return false
142-
} else {
143-
return backingList == (other as JavaOnlyArray).backingList
144-
}
145-
}
137+
override fun equals(other: Any?): Boolean =
138+
if (this === other) {
139+
true
140+
} else if (other == null || javaClass != other.javaClass) {
141+
false
142+
} else {
143+
backingList == (other as JavaOnlyArray).backingList
144+
}
146145

147146
override fun hashCode(): Int = backingList.hashCode()
148147
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeMap.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ public open class ReadableNativeMap protected constructor() : NativeMap(), Reada
7979

8080
private inline fun <reified T> getNullableValue(name: String, type: Class<T>): T? {
8181
val res = getNullableValue(name)
82-
if (res == null) {
83-
return null
82+
return if (res == null) {
83+
null
8484
} else {
85-
return checkInstance(name, res, type)
85+
checkInstance(name, res, type)
8686
}
8787
}
8888

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/LongStreamingStats.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ internal class LongStreamingStats {
3737
}
3838

3939
len++
40-
if (len == 1) {
41-
average = n.toDouble()
42-
} else {
43-
average = (average / (len / (len - 1))) + (n / len)
44-
}
40+
average =
41+
if (len == 1) {
42+
n.toDouble()
43+
} else {
44+
(average / (len / (len - 1))) + (n / len)
45+
}
4546

4647
max = (if (n > max) n else max)
4748
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/accessibilityinfo/AccessibilityInfoModule.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,13 @@ internal class AccessibilityInfoModule(context: ReactApplicationContext) :
121121
}
122122

123123
private val isInvertColorsEnabledValue: Boolean
124-
get() {
125-
try {
126-
return Settings.Secure.getInt(
127-
contentResolver, Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED) == 1
128-
} catch (e: Settings.SettingNotFoundException) {
129-
return false
130-
}
131-
}
124+
get() =
125+
try {
126+
Settings.Secure.getInt(
127+
contentResolver, Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED) == 1
128+
} catch (e: Settings.SettingNotFoundException) {
129+
false
130+
}
132131

133132
private val isGrayscaleEnabledValue: Boolean
134133
get() {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/intent/IntentModule.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public open class IntentModule(reactContext: ReactApplicationContext) :
108108
* @param url the URL to open
109109
*/
110110
override fun openURL(url: String?, promise: Promise) {
111-
if (url == null || url.isEmpty()) {
111+
if (url.isNullOrEmpty()) {
112112
promise.reject(JSApplicationIllegalArgumentException("Invalid URL: $url"))
113113
return
114114
}
@@ -131,7 +131,7 @@ public open class IntentModule(reactContext: ReactApplicationContext) :
131131
* @param promise a promise that is always resolved with a boolean argument
132132
*/
133133
override fun canOpenURL(url: String?, promise: Promise) {
134-
if (url == null || url.isEmpty()) {
134+
if (url.isNullOrEmpty()) {
135135
promise.reject(JSApplicationIllegalArgumentException("Invalid URL: $url"))
136136
return
137137
}
@@ -189,7 +189,7 @@ public open class IntentModule(reactContext: ReactApplicationContext) :
189189
* @param extras An array of extras [{ String, String | Number | Boolean }]
190190
*/
191191
override fun sendIntent(action: String?, extras: ReadableArray?, promise: Promise) {
192-
if (action == null || action.isEmpty()) {
192+
if (action.isNullOrEmpty()) {
193193
promise.reject(JSApplicationIllegalArgumentException("Invalid Action: $action."))
194194
return
195195
}
@@ -214,19 +214,16 @@ public open class IntentModule(reactContext: ReactApplicationContext) :
214214
ReadableType.String -> {
215215
intent.putExtra(name, map.getString(EXTRA_MAP_KEY_FOR_VALUE))
216216
}
217-
218217
ReadableType.Number -> {
219218
// We cannot know from JS if is an Integer or Double
220219
// See: https://github.com/facebook/react-native/issues/4141
221220
// We might need to find a workaround if this is really an issue
222221
val number = map.getDouble(EXTRA_MAP_KEY_FOR_VALUE)
223222
intent.putExtra(name, number)
224223
}
225-
226224
ReadableType.Boolean -> {
227225
intent.putExtra(name, map.getBoolean(EXTRA_MAP_KEY_FOR_VALUE))
228226
}
229-
230227
else -> {
231228
promise.reject(
232229
JSApplicationIllegalArgumentException("Extra type for $name not supported."))

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/ProgressiveStringDecoder.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ internal class ProgressiveStringDecoder(charset: Charset) {
6262
}
6363

6464
val hasRemainder = decoded && remainderLength > 0
65-
if (hasRemainder) {
66-
remainder =
65+
remainder =
66+
if (hasRemainder) {
6767
ByteArray(remainderLength).apply {
6868
System.arraycopy(decodeData, length - remainderLength, this, 0, remainderLength)
6969
}
70-
} else {
71-
remainder = null
72-
}
70+
} else {
71+
null
72+
}
7373

7474
if (!decoded) {
7575
FLog.w(ReactConstants.TAG, "failed to decode string from byte array")

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/permissions/PermissionsModule.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public class PermissionsModule(reactContext: ReactApplicationContext?) :
8080
object : Callback {
8181
override operator fun invoke(vararg args: Any?) {
8282
val results = args[0] as IntArray
83-
if (results.size > 0 && results[0] == PackageManager.PERMISSION_GRANTED) {
83+
if (results.isNotEmpty() && results[0] == PackageManager.PERMISSION_GRANTED) {
8484
promise.resolve(GRANTED)
8585
} else {
8686
val callbackActivity = args[1] as PermissionAwareActivity

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public class WebSocketModule(context: ReactApplicationContext) :
131131
protocolsValue.append(",")
132132
}
133133
}
134-
if (protocolsValue.length > 0) {
134+
if (protocolsValue.isNotEmpty()) {
135135
protocolsValue.replace(protocolsValue.length - 1, protocolsValue.length, "")
136136
builder.addHeader("Sec-WebSocket-Protocol", protocolsValue.toString())
137137
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/PointerEvents.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@ public enum class PointerEvents {
2929
public companion object {
3030

3131
@JvmStatic
32-
public fun parsePointerEvents(pointerEventsStr: String?): PointerEvents {
33-
if (pointerEventsStr == null) {
34-
return AUTO
35-
} else {
36-
return PointerEvents.valueOf(pointerEventsStr.uppercase(Locale.US).replace("-", "_"))
37-
}
38-
}
32+
public fun parsePointerEvents(pointerEventsStr: String?): PointerEvents =
33+
if (pointerEventsStr == null) {
34+
AUTO
35+
} else {
36+
PointerEvents.valueOf(pointerEventsStr.uppercase(Locale.US).replace("-", "_"))
37+
}
3938

4039
@JvmStatic
4140
public fun canBeTouchTarget(pointerEvents: PointerEvents): Boolean {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/layoutanimation/LayoutUpdateAnimation.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ internal class LayoutUpdateAnimation : AbstractLayoutAnimation() {
3232
): Animation? {
3333
val animateLocation = view.x.toInt() != x || view.y.toInt() != y
3434
val animateSize = view.width != width || view.height != height
35-
if (!animateLocation && !animateSize) {
36-
return null
35+
return if (!animateLocation && !animateSize) {
36+
null
3737
} else if (animateLocation && !animateSize && USE_TRANSLATE_ANIMATION) {
3838
// Use GPU-accelerated animation, however we loose the ability to resume interrupted
3939
// animation where it was left off. We may be able to listen to animation interruption
4040
// and set the layout manually in this case, so that next animation kicks off smoothly.
41-
return TranslateAnimation(view.x - x, 0f, view.y - y, 0f)
41+
TranslateAnimation(view.x - x, 0f, view.y - y, 0f)
4242
} else {
4343
// Animation is sub-optimal for perf, but scale transformation can't be use in this case.
44-
return PositionAndSizeAnimation(view, x, y, width, height)
44+
PositionAndSizeAnimation(view, x, y, width, height)
4545
}
4646
}
4747

0 commit comments

Comments
 (0)