Skip to content

Fix assertions in chemistry.rs that could never fail (#551) - #665

Merged
NSoiffer merged 1 commit into
daisy:mainfrom
yasumorishima:fix-chemistry-assertions-551
Aug 10, 2026
Merged

NSoiffer merged 1 commit into
daisy:mainfrom
yasumorishima:fix-chemistry-assertions-551

Conversation

@yasumorishima

Copy link
Copy Markdown
Contributor

Fixes #551.

The defect

assert!(!elements.len() > 1) parses as (!elements.len()) > 1. len() returns usize, so ! is a bitwise complement rather than a logical negation — !0 is usize::MAX and !1 is usize::MAX - 1, both greater than 1. The assertion held for every possible input and checked nothing.

Three sites, all carrying the same // already handled comment: is_structural, is_alphabetical and is_ordered_by_electronegativity.

Why elements.len() > 1 is the intended condition

Read off the bodies rather than inferred from the comment alone:

  • is_alphabetical indexes elements[0] and elements[1], so fewer than two elements would panic on the index itself.
  • is_ordered_by_electronegativity is elements.windows(2).all(...), which vacuously returns true for a slice shorter than 2 — a meaningless answer rather than a crash.
  • is_structural compares a de-duplicated set size against elements.len(), which can only differ once there are at least two.

Why the corrected assertion does not start firing

The // already handled comment refers to the sole caller, is_order_ok:

let n_elements = elements.len();
if n_elements < 2 {
    return true;
} else if has_noble_element(&elements) {
    return false;
} else {
    return ... is_structural(&elements) || is_alphabetical(&elements) || ...

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_structural and test_electronegativity_order has 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 a bool and the negation is correct, so they are untouched: assert!(!script_children.len().is_multiple_of(2)) and assert!(!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 sets RUSTFLAGS: -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.

`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()`.
@NSoiffer

Copy link
Copy Markdown
Collaborator

Thanks (or thanks to your AI :-) for these. They were leftover from an earlier !elements.is_empty() test.

@NSoiffer
NSoiffer merged commit acd5cfb into daisy:main Aug 10, 2026
@github-project-automation github-project-automation Bot moved this from Triage to Done in MathCAT Project Board Aug 10, 2026
yasumorishima added a commit to yasumorishima/yasumorishima that referenced this pull request Aug 11, 2026
@yasumorishima
yasumorishima deleted the fix-chemistry-assertions-551 branch November 10, 2026 23:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

bug in some assertions in chemistry.rs

2 participants