From 5a87e4e98b47a53a68878600903cb3cb848b3f0e Mon Sep 17 00:00:00 2001 From: Adam Woods-Mccormick Date: Fri, 17 Jan 2025 09:58:47 -0800 Subject: [PATCH] Avoid NSE Exceptions (#1037) Summary: X-link: https://github.com/facebookexternal/meta-ar-mr-tooling/pull/93 Pull Request resolved: https://github.com/facebook/litho/pull/1037 Crashes like https://fburl.com/logview/p23dzrkw (https://fburl.com/logview/8z9nspug) happen when a list is non-null but empty. I reason that if null was a reasonable return, then an empty list can be a null instead of crashing. This isn't common, but spiked during SEV 0 S475626. Basically `?.first()` -> `?.firstOrNull()` as a dumb codemod: ``` for i in `xbgs -l -f '.*\.kt' '?.last()' | sed -e 's/^fbsource\///'`; do sed -i '' 's/\?\.last()/?.lastOrNull()/g' $i; done for i in `xbgs -l -f '.*\.kt' '?.first()' | sed -e 's/^fbsource\///'`; do sed -i '' 's/\?\.first()/?.firstOrNull()/g' $i; done arc f ``` Reviewed By: adityasharat Differential Revision: D67113975 fbshipit-source-id: d226a84179a822c59310410593b1ed8f7b12cfa0 --- .../main/java/com/facebook/litho/testing/LegacyLithoViewRule.kt | 2 +- .../src/main/java/com/facebook/litho/testing/TestLithoView.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/litho-testing/src/main/java/com/facebook/litho/testing/LegacyLithoViewRule.kt b/litho-testing/src/main/java/com/facebook/litho/testing/LegacyLithoViewRule.kt index 7ffef11b525..7b55c03df70 100644 --- a/litho-testing/src/main/java/com/facebook/litho/testing/LegacyLithoViewRule.kt +++ b/litho-testing/src/main/java/com/facebook/litho/testing/LegacyLithoViewRule.kt @@ -427,7 +427,7 @@ constructor( } private fun findViewWithPredicateOrNull(viewTree: ViewTree, predicate: Predicate): View? { - return viewTree.findChild(predicate)?.last() + return viewTree.findChild(predicate)?.lastOrNull() } /** diff --git a/litho-testing/src/main/java/com/facebook/litho/testing/TestLithoView.kt b/litho-testing/src/main/java/com/facebook/litho/testing/TestLithoView.kt index 75559b8255e..47f670a9988 100644 --- a/litho-testing/src/main/java/com/facebook/litho/testing/TestLithoView.kt +++ b/litho-testing/src/main/java/com/facebook/litho/testing/TestLithoView.kt @@ -394,7 +394,7 @@ internal constructor( } private fun findViewWithPredicateOrNull(viewTree: ViewTree, predicate: Predicate): View? { - return viewTree.findChild(predicate)?.last() + return viewTree.findChild(predicate)?.lastOrNull() } /** Returns the first [LazyCollection] from the ComponentTree, or null if not found. */