JS-1995 Suppress S2486 on single-statement try blocks#7445
JS-1995 Suppress S2486 on single-statement try blocks#7445erwan-leforestier-sonarsource merged 6 commits into
Conversation
This comment has been minimized.
This comment has been minimized.
b19fe6a to
9008a7c
Compare
|
try {
{ foo(); bar(); }
} catch (err) {}I verified that locally with a RuleTester repro: the current rule reports 0 issues for that shape. That feels outside the intended single-operation try/fallback pattern and turns a multi-step catch body into a false negative. Could we keep reporting for block wrappers as well, and add an invalid regression for that boundary next to the existing loop/switch cases? |
|
@francois-mora-sonarsource |
Cast the catch clause parent to TryStatement at the call site and drop the redundant type check, and only resolve the parent when the caught variable is unused. Add fixture cases for assignment, variable declaration, throw and nested try single-statement bodies, plus an empty-try noncompliant boundary.
…bodies Loops and switch statements with an empty catch are not idiomatic best-effort patterns: they can hide multiple side-effecting operations that silently swallow errors. Exempt only when the single body statement is not a for/for-in/for-of/while/do-while/switch. Add fixture cases for each newly-flagged shape.
…emption A labeled loop like `outer: for (...)` has type LabeledStatement, not ForStatement, so it was slipping through the LOOP_OR_SWITCH_TYPES guard. Unwrap LabeledStatement nodes before the type check so that labeled loops and switches are correctly treated as non-exempt. Update ruling expected files to remove the 223 single-statement issues that S2486 no longer raises across the corpus. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
7c239bd to
f8b5e76
Compare
|
francois-mora-sonarsource
left a comment
There was a problem hiding this comment.
Looks good to me now.
The wrapped-block and labeled-loop boundaries are covered, the ruling delta stays aligned with the intended suppression scope, and the focused local regression check passes.
One non-blocking follow-up thought: depending on how we want to define “single statement”, we may later want to decide whether top-level IfStatement and nested TryStatement should also count as non-simple. I do not see that as a blocker for this PR as written.
Code Review ✅ Approved 1 resolved / 1 findingsS2486 now exempts try blocks containing a single top-level statement, resolving the loop and switch guard bypass issues while reducing noise. ✅ 1 resolved✅ Edge Case: Loop/switch guard bypassed by wrapping statements
OptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |




Summary
Suppress S2486 when a
tryblock contains a single top-level statement, which reduces noise on intentional try/fallback patterns. Keep reporting unused catch parameters for broadertryblocks.Changes
trybody has exactly one top-level statementmasterand the review branch on the same sampleSummary by Gitar
S2486to exempttryblocks containing exactly one top-level statement, accounting forLabeledStatementwrappers.build.ymlto improve JS coverage artifacts handling.rulingtest expectations following the logic change inS2486.This will update automatically on new commits.