From aaad7e083d069f0fa4418d7269e426dcb0bd75ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateo=20Guzm=C3=A1n?= Date: Fri, 20 Jun 2025 16:03:34 -0700 Subject: [PATCH] Kotlin: fix static code analysis weak warnings (1/n) (#52153) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Static code analysis reports several weak warnings, many of which seem to be leftovers after Kotlin migration. This PR addresses quite a few: - [Convert to primary constructor](https://www.jetbrains.com/help/inspectopedia/ConvertSecondaryConstructorToPrimary.html) - [If-Then foldable to '?.'](https://www.jetbrains.com/help/inspectopedia/IfThenToSafeAccess.html) - [Non-canonical modifier order](https://www.jetbrains.com/help/inspectopedia/SortModifiers.html) ## Changelog: [INTERNAL] - Kotlin: fix static code analysis weak warnings (1/n) Pull Request resolved: https://github.com/facebook/react-native/pull/52153 Test Plan: ```sh yarn android yarn test-android ``` Reviewed By: christophpurrer Differential Revision: D77042260 Pulled By: arushikesarwani94 fbshipit-source-id: ea210976ccbcecbe4843ff5205238a83fc75d43b --- .../facebook/react/MemoryPressureRouter.kt | 6 ++-- .../react/bridge/BaseActivityEventListener.kt | 4 +-- .../facebook/react/bridge/GuardedAsyncTask.kt | 2 +- .../react/devsupport/DevSupportManagerBase.kt | 4 +-- .../AccessibilityInfoModule.kt | 30 +++++++------------ .../SimpleSpringInterpolator.kt | 10 ++----- .../ReactTextViewAccessibilityDelegate.kt | 16 +++++----- 7 files changed, 29 insertions(+), 43 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/MemoryPressureRouter.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/MemoryPressureRouter.kt index a0a94ffe35a..ad3a7757e69 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/MemoryPressureRouter.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/MemoryPressureRouter.kt @@ -37,15 +37,15 @@ public class MemoryPressureRouter(context: Context) : ComponentCallbacks2 { listeners.remove(listener) } - override public fun onTrimMemory(level: Int) { + public override fun onTrimMemory(level: Int) { dispatchMemoryPressure(level) } - override public fun onConfigurationChanged(newConfig: Configuration): Unit = Unit + public override fun onConfigurationChanged(newConfig: Configuration): Unit = Unit @Deprecated( "onLowMemory is deprecated, use onTrimMemory instead.", ReplaceWith("onTrimMemory(level)")) - override public fun onLowMemory(): Unit = Unit + public override fun onLowMemory(): Unit = Unit private fun dispatchMemoryPressure(level: Int) { for (listener in listeners) { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/BaseActivityEventListener.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/BaseActivityEventListener.kt index e20ffeae8d9..d583548c5a6 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/BaseActivityEventListener.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/BaseActivityEventListener.kt @@ -18,12 +18,12 @@ public open class BaseActivityEventListener : ActivityEventListener { ReplaceWith("onActivityResult(activity, requestCode, resultCode, data)")) public open fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent): Unit = Unit - override public fun onActivityResult( + public override fun onActivityResult( activity: Activity, requestCode: Int, resultCode: Int, data: Intent? ): Unit = Unit - override public fun onNewIntent(intent: Intent): Unit = Unit + public override fun onNewIntent(intent: Intent): Unit = Unit } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/GuardedAsyncTask.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/GuardedAsyncTask.kt index bcf4496a6c1..de3fe3cd402 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/GuardedAsyncTask.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/GuardedAsyncTask.kt @@ -25,7 +25,7 @@ protected constructor(private val exceptionHandler: JSExceptionHandler) : protected constructor(reactContext: ReactContext) : this(reactContext.exceptionHandler) @Deprecated("AsyncTask is deprecated.") - override protected final fun doInBackground(vararg params: Params): Void? { + protected final override fun doInBackground(vararg params: Params): Void? { try { doInBackgroundGuarded(*params) } catch (e: RuntimeException) { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.kt index bee375e0aec..b30b646b5df 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.kt @@ -465,9 +465,7 @@ public abstract class DevSupportManagerBase( devOptionsDialog?.show() val reactContext = currentReactContext - if (reactContext != null) { - reactContext.getJSModule(RCTNativeAppEventEmitter::class.java).emit("RCTDevMenuShown", null) - } + reactContext?.getJSModule(RCTNativeAppEventEmitter::class.java)?.emit("RCTDevMenuShown", null) } override fun onNewReactContextCreated(reactContext: ReactContext) { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/accessibilityinfo/AccessibilityInfoModule.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/accessibilityinfo/AccessibilityInfoModule.kt index 2f11879a277..928a1f66c23 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/accessibilityinfo/AccessibilityInfoModule.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/accessibilityinfo/AccessibilityInfoModule.kt @@ -181,10 +181,8 @@ internal class AccessibilityInfoModule(context: ReactApplicationContext) : val isReduceMotionEnabled = isReduceMotionEnabledValue if (reduceMotionEnabled != isReduceMotionEnabled) { reduceMotionEnabled = isReduceMotionEnabled - val reactApplicationContext = getReactApplicationContextIfActiveOrWarn() - if (reactApplicationContext != null) { - reactApplicationContext.emitDeviceEvent(REDUCE_MOTION_EVENT_NAME, reduceMotionEnabled) - } + getReactApplicationContextIfActiveOrWarn() + ?.emitDeviceEvent(REDUCE_MOTION_EVENT_NAME, reduceMotionEnabled) } } @@ -192,10 +190,8 @@ internal class AccessibilityInfoModule(context: ReactApplicationContext) : val isInvertColorsEnabled = isInvertColorsEnabledValue if (invertColorsEnabled != isInvertColorsEnabled) { invertColorsEnabled = isInvertColorsEnabled - val reactApplicationContext = getReactApplicationContextIfActiveOrWarn() - if (reactApplicationContext != null) { - reactApplicationContext.emitDeviceEvent(INVERT_COLOR_EVENT_NAME, invertColorsEnabled) - } + getReactApplicationContextIfActiveOrWarn() + ?.emitDeviceEvent(INVERT_COLOR_EVENT_NAME, invertColorsEnabled) } } @@ -203,13 +199,11 @@ internal class AccessibilityInfoModule(context: ReactApplicationContext) : val isHighTextContrastEnabled = isHighTextContrastEnabledValue if (highTextContrastEnabled != isHighTextContrastEnabled) { highTextContrastEnabled = isHighTextContrastEnabled - val reactApplicationContext = getReactApplicationContextIfActiveOrWarn() - if (reactApplicationContext != null) { - reactApplicationContext.emitDeviceEvent( - HIGH_TEXT_CONTRAST_EVENT_NAME, - highTextContrastEnabled, - ) - } + getReactApplicationContextIfActiveOrWarn() + ?.emitDeviceEvent( + HIGH_TEXT_CONTRAST_EVENT_NAME, + highTextContrastEnabled, + ) } } @@ -239,10 +233,8 @@ internal class AccessibilityInfoModule(context: ReactApplicationContext) : val isGrayscaleModeEnabled = isGrayscaleEnabledValue if (grayscaleModeEnabled != isGrayscaleModeEnabled) { grayscaleModeEnabled = isGrayscaleModeEnabled - val reactApplicationContext = getReactApplicationContextIfActiveOrWarn() - if (reactApplicationContext != null) { - reactApplicationContext.emitDeviceEvent(GRAYSCALE_MODE_EVENT_NAME, grayscaleModeEnabled) - } + getReactApplicationContextIfActiveOrWarn() + ?.emitDeviceEvent(GRAYSCALE_MODE_EVENT_NAME, grayscaleModeEnabled) } } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/layoutanimation/SimpleSpringInterpolator.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/layoutanimation/SimpleSpringInterpolator.kt index a0e93258c76..fa42a2b2767 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/layoutanimation/SimpleSpringInterpolator.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/layoutanimation/SimpleSpringInterpolator.kt @@ -17,13 +17,9 @@ import com.facebook.react.common.annotations.internal.LegacyArchitectureLogger /** Simple spring interpolator */ // TODO(7613736): Improve spring interpolator with friction and damping variable support @LegacyArchitecture(logLevel = LegacyArchitectureLogLevel.ERROR) -internal class SimpleSpringInterpolator : Interpolator { - private val _springDamping: Float - - @JvmOverloads - constructor(springDamping: Float = FACTOR) { - _springDamping = springDamping - } +internal class SimpleSpringInterpolator @JvmOverloads constructor(springDamping: Float = FACTOR) : + Interpolator { + private val _springDamping: Float = springDamping override fun getInterpolation(input: Float): Float = // Using mSpringDamping in this equation is not really the exact mathematical springDamping, diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt index adadc6c114a..c062643505f 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt @@ -21,17 +21,17 @@ import com.facebook.react.R import com.facebook.react.uimanager.ReactAccessibilityDelegate import com.facebook.react.views.text.internal.span.ReactClickableSpan -internal class ReactTextViewAccessibilityDelegate : ReactAccessibilityDelegate { - constructor( - view: View, - originalFocus: Boolean, - originalImportantForAccessibility: Int - ) : super(view, originalFocus, originalImportantForAccessibility) { +internal class ReactTextViewAccessibilityDelegate( + view: View, + originalFocus: Boolean, + originalImportantForAccessibility: Int +) : ReactAccessibilityDelegate(view, originalFocus, originalImportantForAccessibility) { + private var accessibilityLinks: AccessibilityLinks? = null + + init { accessibilityLinks = hostView.getTag(R.id.accessibility_links) as AccessibilityLinks? } - private var accessibilityLinks: AccessibilityLinks? = null - companion object { fun setDelegate(view: View, originalFocus: Boolean, originalImportantForAccessibility: Int) { // if a view already has an accessibility delegate, replacing it could cause