JS-1802 Fix S3801 false positives for React effect cleanups#7127
JS-1802 Fix S3801 false positives for React effect cleanups#7127francois-mora-sonarsource wants to merge 3 commits into
Conversation
🤖 Generated with GitHub Actions
Code Review ✅ ApprovedUpdates rule S3801 to exempt React effect hook callbacks, correctly allowing both cleanup functions and early returns in OptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change:
Was this helpful? React with 👍 / 👎 | Gitar |
|
Rule Profile
function getValue(condition) {
if (condition) {
return 42;
}
return;
}False Positive PatternThe false positives happen in React effect callbacks that return a cleanup function on one path and return nothing on another path. S3801 normally treats this as inconsistent API behavior, but React gives effect callback returns a special meaning: returning a function registers cleanup, while returning nothing means there is no cleanup for that effect run. useEffect(() => {
if (!enabled) {
return;
}
const subscription = subscribe();
return () => subscription.unsubscribe();
}, [enabled]);False Negative RiskThe risk is bounded because the fix does not suppress all functions with mixed returns, nor all callbacks. It suppresses only the first argument passed to Fix SummaryThe implementation tracks React imports, recognizes React effect hook calls, and skips S3801 reporting for the effect callback itself. This matches the React contract where cleanup returns are optional and should not be interpreted as inconsistent function return behavior.
|
|
For TypeScript, maybe this FP could be more general, I think it applies to any function that returns a union of (either See the signature for the useEffect callback argument: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/index.d.ts#L1665 Here the return type is |




Summary
Fixes a React false positive in S3801 by skipping callbacks passed to React effect hooks. Effect callbacks may return a cleanup function or return nothing, so an early
return;mixed withreturn () => cleanup()is valid React code rather than inconsistent API behavior.The exemption covers
useEffect,useLayoutEffect, anduseInsertionEffectimported fromreact, including aliased named imports andReact.useEffect-style calls.Validation
./node_modules/.bin/tsx --tsconfig packages/tsconfig.test.json --test packages/analysis/src/jsts/rules/S3801/unit.test.tsgit diff --checknpm run bridge:compilewas attempted, but this fresh worktree lacks the ignored generated rule metadata/index artifacts expected by the command, so it fails before branch-specific checking.