Skip to content

Report dead catch in a trait only when it is dead in every using class - #7016

Merged
ondrejmirtes merged 3 commits into
2.3.xfrom
dead-catch-in-trait
Nov 9, 2026
Merged

ondrejmirtes merged 3 commits into
2.3.xfrom
dead-catch-in-trait

Conversation

@ondrejmirtes

@ondrejmirtes ondrejmirtes commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

CatchWithUnthrownExceptionRule now follows the other constant-condition rules for code in traits: outside traits it reports directly, inside a trait it records the outcome through ConstantConditionInTraitHelper, and ConstantConditionInTraitRule reports the dead catch only when it is dead in every class using the trait.

CatchWithUnthrownExceptionNode is emitted only for dead catch types, so the rule had no way to record "this catch is live here". This adds a CatchTypeNode virtual node, emitted for every catch type with isThrown(), which the rule now listens to. CatchWithUnthrownExceptionNode is still emitted for third-party rules and is deprecated in favour of the new node; its remaining construction in TryCatchHandler is in the baseline. The native TryCatchHandler in turbo-ext emits the new node too.

ConstantConditionInTraitHelper also accepts a Name (each catch type is keyed by its own name node, so the types of one multi-catch are tracked separately).

Closes phpstan/phpstan#10315

@ondrejmirtes
ondrejmirtes merged commit 8b969c7 into 2.3.x Nov 9, 2026
888 of 892 checks passed
@ondrejmirtes
ondrejmirtes deleted the dead-catch-in-trait branch November 9, 2026 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False positive: Dead catch

1 participant