Skip to content

Commit

Permalink
Avoid NSE Exceptions (#1037)
Browse files Browse the repository at this point in the history
Summary:
X-link: facebookexternal/meta-ar-mr-tooling#93

Pull Request resolved: #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
  • Loading branch information
AdamMcCormick authored and facebook-github-bot committed Jan 17, 2025
1 parent f4ba17b commit 5a87e4e
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ constructor(
}

private fun findViewWithPredicateOrNull(viewTree: ViewTree, predicate: Predicate<View>): View? {
return viewTree.findChild(predicate)?.last()
return viewTree.findChild(predicate)?.lastOrNull()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ internal constructor(
}

private fun findViewWithPredicateOrNull(viewTree: ViewTree, predicate: Predicate<View>): View? {
return viewTree.findChild(predicate)?.last()
return viewTree.findChild(predicate)?.lastOrNull()
}

/** Returns the first [LazyCollection] from the ComponentTree, or null if not found. */
Expand Down

0 comments on commit 5a87e4e

Please sign in to comment.