feat(lint): report a discarded destructive sequence result - #118
Merged
Conversation
CLHS 17.1 lets `sort` and its relatives destroy the argument and use it to construct the result, so the caller must use the return value. The classic bug is `(sort xs #'<)` as a bare statement, after which `xs` is garbage. The premise held for only six of the twenty-two functions, and the check is why. SBCL 2.6.0 already signals a `STYLE-WARNING` for eleven of them -- `nreverse`, `nreconc`, `delete`, `delete-if`, `delete-if-not`, `delete-duplicates`, `nunion`, `nintersection`, `nset-difference`, `nset-exclusive-or` and `merge` -- so those defects do not survive in code compiled daily. `failure-p` stays `NIL`; it is `warnings-p` that goes `T`. An early probe muffled the warnings and reported `NIL`, which is worth recording: muffling falsifies `warnings-p`. `sort` and `stable-sort` are the real gap, and their silence depends on type inference. SBCL warns once it can prove listness and picks the `STABLE-SORT-LIST` transform, so a `(declare (list xs))` parameter warns while an undeclared one -- the common case -- does not, and a `(declare (vector xs))` one is silent because sorting a vector really is in place. That removes five more heads: discarding `(sort v #'<)` on a vector, or `(nstring-upcase s)`, is silent *and* harmless, so reporting them would be noise. What is left is the six that are both silent under SBCL and able to return an object different from their argument: `sort`, `stable-sort`, `nconc`, `nbutlast`, `nsublis`, `nsubst`. The rule anchors on the twenty body forms with an implicit `progn`, not on `sort`. Anchoring on the call and walking up cost 3.9 seconds on a 200-function fixture with no findings, because `root_view()` materializes every node and the *correct* idiom `(setf xs (sort xs #'<))` passes any cheap head-and-argument test -- so every correct call paid a full materialization. From the body form the parent-child relation is local: 3,250x faster at n=250 and 14,553x at n=2000, the factor growing because this shape is linear and the other quadratic. On a file where every call is a finding the rule is itself quadratic, which is recorded in `cost_tests.rs` rather than hidden; it is invisible on correct code, and the 28 MB audit sweeps in 0.69 seconds. A second proposed rule was dropped as a true duplicate: `lint-sequence`'s `destructive_literal` already covers 23 heads with per-head destroyed-argument indices, and its tests assert exactly the examples that motivated it. Audited over 1619 files (SBCL 2.6.0 plus 721 Quicklisp files, 28 MB): 0 findings, and the funnel explains the zero rather than leaving it suspicious. 56,731 body forms dispatched, 295 destructive calls, 122 on a bare variable, 1 in a discarding position, 0 read by a later form. The near-miss is `sbcl/src/code/globals.lisp:70`, where `list` is a header cons and `nconc` mutates in place, so discarding is deliberate -- the last condition declined it, which is the only evidence that condition earns its place. Mutation: 16 guards, all killed, over four rounds. Every mutation was first verified to change the file -- one regex silently no-op'd and reported a false survivor. Four live guards had no test, including the `is_unevaluated_at` suppression that no unit test could reach, because the test helper filters data before the rule sees it; it needed an engine-level test. Three guards were genuinely dead and were removed with the reasoning recorded where it is reachable.
takeokunn
force-pushed
the
feat/lint-destructive-sequence
branch
from
August 4, 2026 03:45
7f190d8 to
d074bc0
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
CLHS 17.1 lets
sortand its relatives destroy the argument and use it to construct the result, so the caller must use the return value.(sort xs #'<)as a bare statement leavesxsas garbage.discarded-destructive-sequence-resultDeadCodeWarningReportOnlyCOMMON_LISP_ONLYHeads are the 20 body forms with an implicit
progn—progn prog prog* let let* flet labels macrolet symbol-macrolet lambda when unless dolist dotimes block with-open-file with-slots defun defmethod defmacro— not the destructive calls. See cost, below.The premise held for 6 of 22 functions, and the check is the point
SBCL 2.6.0 already warns on eleven of them, unconditionally:
failure-pstaysNIL; it iswarnings-pthat goesT. Worth recording: an early probe muffled the warnings and reportedNIL, appearing to confirm the premise. Muffling falsifieswarnings-p.sort/stable-sortare the real gap, and their silence is conditional on type inference:SBCL warns only once it can prove listness and picks the
STABLE-SORT-LISTtransform. Undeclared parameters — the common case — get nothing.That last line also removes five heads: discarding
(sort v #'<)on a vector is silent and harmless, since sorting a vector is genuinely in place. Same fornstring-upcase. Reporting them would be pure noise.Net: 6 heads survive — silent under SBCL and able to return an object different from the argument:
sort,stable-sort,nconc,nbutlast,nsublis,nsubst.Measured, not assumed — the post-discard state:
(sort xs #'<)on(5 4 3 2 1)(4 5)— a two-element interior tail(sort xs #'<)on(3 1 2)(1 2 3)— accidentally correct(nreverse xs)on(1 2 3 4 5)(1)— the first consA second proposed rule was a true duplicate
lint-sequence/src/destructive_literal/rule.rs:18already ships with 23 heads (a superset), per-head destroyed-argument indices, and tests asserting exactly the motivating examples — plusdoes_not_flag_a_variable_sequencepinning(sort xs #'<)as clean. Not built.Near-misses read and cleared:
copy_before_destructive.rs:34,destructive_function_naming.rs:33,macro_body_destroys_argument_form/domain.rs:117,unnecessary_sort_before_extremum_extraction.rs:82(whose module doc argues against parent-walks onsort— the objection this design had to answer),sort_not_guaranteed_stable.rs:91.The cost trap, caught by measurement
The first design anchored on
sortand walked up viaroot_view(). 3.9 seconds on a 200-function fixture with zero findings —root_view()materializes every node, and the correct idiom(setf xs (sort xs #'<))passes any cheap head-and-argument test, so every correct call paid a full materialization.Inverting the anchor to the body form makes the parent-child relation local. Release, load average 3.32:
cost-control-wrong-orderdiscarded-destructive-…-resultcost-control-shipped-local3,250× at n=250 → 14,553× at n=2000; the factor grows because the shipped shape is linear and the rejected one quadratic.
Stated weakness: on a file where every call is a finding the rule is itself quadratic (5.37 s), since
is_unevaluated_atruns per reporting body form. Recorded incost_tests.rsrather than hidden — it is invisible on correct code, and the 28 MB audit sweeps in 0.69 s.Corpus audit: 1619 files, and the zero is explained
Condition 2 does the cutting — only 1 of 122 destructive calls on a variable sits in a discarding position. That near-miss is
sbcl/src/code/globals.lisp:70,(nconc list (list (list symbol initform))), which is correct code:listis a header cons the function reads via(cdr list), sonconcmutates in place and discarding is deliberate. Condition 3 declined it — the only evidence that condition earns its place.No real bugs found, and the honest reading is that for 11 of 22 functions SBCL warns, so those defects don't survive in SBCL-compiled code. What the audit establishes is a false-positive rate of 0 over 295 candidates, verified by a self-test plus an end-to-end plant into alexandria's
control-flow.lisp.Positions
Discarded = non-final form at or after body-start. Used = last child, or any argument of a plain call — which is why
setf/push/return-fromare structurally unreportable. Ambiguous, never reported =tagbody,loop,cond/caseclauses,unwind-protectcleanups,prog1/prog2.Mutation: 16 guards, all killed, 4 rounds
Every mutation was first verified to actually change the file — one regex silently no-op'd and reported a false "survived".
is_unevaluated_atsuppression that no unit test could reach, because the test helper filters data before the rule sees it. It needed an engine-level test.subtree_mentions'sis_bare_symbolcall (unreachable —atom_textcarries the reader prefix),discarded_range'sstart < last(an empty range is already inert), andvalue_is_discarded, a second spelling of a predicate the rule inlined.Counts
RULE_COUNT357 → 358,warning_count254 → 255, catalogue 341 → 342, preset-filtered warnings 238 → 239.cargo build --workspace/fmt --check/clippy --all-targets --all-features -D warnings/test --workspaceall exit 0.