Repository navigation
Fix assertions in chemistry.rs that could never fail (#551) - #665
Merged
Merged
Conversation
`assert!(!elements.len() > 1)` parses as `(!elements.len()) > 1`. `len()` is a `usize`, so `!` is a bitwise complement rather than a logical negation: `!0` is `usize::MAX` and `!1` is `usize::MAX - 1`, both of which are greater than 1. The assertion therefore held for every input and checked nothing. The three sites are in `is_structural`, `is_alphabetical` and `is_ordered_by_electronegativity`, and their bodies show the intended precondition: `is_alphabetical` indexes `elements[1]`, so it needs at least two elements, and the other two are only meaningful for two or more. Their shared `// already handled` comment refers to the guard in the sole caller, `is_order_ok`, which returns early when `n_elements < 2`, so the corrected assertion is not reachable from there. Two nearby assertions are left alone because their `!` is already a logical negation on a `bool`: `!script_children.len().is_multiple_of(2)` and `!elements.is_empty()`.
Collaborator
|
Thanks (or thanks to your AI :-) for these. They were leftover from an earlier |
yasumorishima
added a commit
to yasumorishima/yasumorishima
that referenced
this pull request
Aug 11, 2026
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.
Fixes #551.
The defect
assert!(!elements.len() > 1)parses as(!elements.len()) > 1.len()returnsusize, so!is a bitwise complement rather than a logical negation —!0isusize::MAXand!1isusize::MAX - 1, both greater than 1. The assertion held for every possible input and checked nothing.Three sites, all carrying the same
// already handledcomment:is_structural,is_alphabeticalandis_ordered_by_electronegativity.Why
elements.len() > 1is the intended conditionRead off the bodies rather than inferred from the comment alone:
is_alphabeticalindexeselements[0]andelements[1], so fewer than two elements would panic on the index itself.is_ordered_by_electronegativityiselements.windows(2).all(...), which vacuously returnstruefor a slice shorter than 2 — a meaningless answer rather than a crash.is_structuralcompares a de-duplicated set size againstelements.len(), which can only differ once there are at least two.Why the corrected assertion does not start firing
The
// already handledcomment refers to the sole caller,is_order_ok:so the three functions are unreachable with fewer than two elements. The unit tests call them directly, and every input in
test_is_alphabetical,test_is_structuralandtest_electronegativity_orderhas two or more elements (C+H, C+H+O, B+C+O, P+B+O, H+O+H, H+C+C+H, N+H, O+F).Left alone deliberately
Two nearby assertions also begin with
!, but there the operand is abooland the negation is correct, so they are untouched:assert!(!script_children.len().is_multiple_of(2))andassert!(!elements.is_empty()). The patch was applied by a script that asserted both survive unchanged, so this is checked rather than claimed.Verification
Run on a fork at this commit — all jobs green:
Build and Test (default),Build and Test (no-unsafe),Clippy/Lint check (default),Clippy/Lint check (no-unsafe), and the rules-zip job. Note the workflow setsRUSTFLAGS: -Dwarnings, so the corrected expression introduces no new warning.Per AGENTS.md I kept the change focused and did not run
cargo fmt; the diff is three characters removed.