Skip to content

[compiler] Fix set-state-in-effect false negative with NewExpression default param#36107

Open
sleitor wants to merge 1 commit intofacebook:mainfrom
sleitor:fix-36101
Open

[compiler] Fix set-state-in-effect false negative with NewExpression default param#36107
sleitor wants to merge 1 commit intofacebook:mainfrom
sleitor:fix-36101

Conversation

@sleitor
Copy link
Contributor

@sleitor sleitor commented Mar 19, 2026

Summary

Fixes #36101

When a component function has a destructured prop with a NewExpression default value (e.g. { value = new Number() }), the React Compiler bails out during HIR construction when trying to lower the default value via lowerReorderableExpression. This causes validateNoSetStateInEffects to never run, silently suppressing the set-state-in-effect diagnostic.

Root cause: isReorderableExpression did not have a case for NewExpression, so it fell through to the default: return false branch. lowerReorderableExpression then recorded a Todo error and aborted compilation of the function before any validation passes ran.

Fix: Add a NewExpression case to isReorderableExpression that mirrors the existing CallExpression case — the expression is safe to reorder when the callee and all arguments are themselves reorderable (e.g. global identifiers and literals).

How did you test this change?

Added a new compiler fixture invalid-setState-in-useEffect-new-expression-default-param that reproduces the bug from the issue. The fixture verifies that the EffectSetState diagnostic is correctly emitted for a component with a NewExpression default prop value.

All 1720 compiler snapshot tests pass.

…default param

When a component function has a destructured prop with a NewExpression
default value (e.g. `{ value = new Number() }`), the compiler would
bail out during BuildHIR when trying to lower the default value via
`lowerReorderableExpression`. This caused `validateNoSetStateInEffects`
to never run, silently suppressing the set-state-in-effect diagnostic.

The fix adds a 'NewExpression' case to `isReorderableExpression`, treating
it the same as 'CallExpression' — safe to reorder when the callee and all
arguments are themselves reorderable (e.g., global identifiers and literals).

Fixes facebook#36101
@meta-cla meta-cla bot added the CLA Signed label Mar 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: react-hooks/set-state-in-effect fails to flag violation if prop has a NewExpression default value

1 participant