Skip to content

feat: treat inequality comparisons as magic-string decision points - #215

Merged
dbrattli merged 2 commits into
mainfrom
feat/magic-string-inequality
Nov 22, 2026
Merged

dbrattli merged 2 commits into
mainfrom
feat/magic-string-inequality

Conversation

@dbrattli

Copy link
Copy Markdown
Collaborator

Summary

Last follow-up from #212. A string repeated as an inequality operand now counts toward the magic-string threshold, just like ==. This covers !=, !==, F#'s <>, and Erlang's /= and =/=, including Rust's kind != Some("x"). A typo in status != "archived" silently flips a condition exactly as one in == does.

Design

EqualityComparison gains a Polarity: ComparisonPolarity (Equal | NotEqual), so each adapter still reads comparison operators in one place:

  • TypeScript, Kotlin, C#, C++ had four identical copies of "binary node with operand, operator token, operand". They now share binaryComparisons with a per-language ComparisonOperators record.
  • Python (chained comparison_operator), F# (infix_op with const unwrapping), Erlang (lhs/rhs fields) and Rust (operator field, with the Some("x") unwrapping) keep their shapes and learn the inequality operator.

Other detectors are unchanged. Match opportunities and stringly-typed control flow (primitive obsession) keep only Equal. An inequality excludes one value rather than selecting a branch or one of a variable's states. Both filters have ADC decision: comments, and both detector docs state this.

Separate commit

The new rule found the import keyword compared twice with <> in PythonTypeInfo/Imports.fs. It's now a named constant, and just analyze reports the same findings as main.

Test plan

  • just test: 628 passed. New matrix scenarios:
    • Magic strings: flaggedInequality in Python, TypeScript/TSX, F#, Kotlin, C++ and C#, plus flagged_inequality in Erlang and Rust.
    • Match opportunity: cleanInequalityChain in every language, an != if/else chain that must not suggest a match.
    • Primitive obsession: cleanInequalityChecks / clean_inequality, one variable != three distinct strings, which must not count as stringly typed.
  • Mutation check: removing the two Equal filters fails 15 tests, one per language per detector. This confirms every adapter emits NotEqual and the filters are what keep those detectors unchanged.
  • just lint, just md-lint
  • just analyze: no new findings compared with main
  • Real Rust workspace: 5 more magic-string findings, all genuine (key != "notify" next to a ("notify", …) pattern, != "---" next to == "---", and so on)

🤖 Generated with Claude Code

dbrattli and others added 2 commits November 22, 2026 12:20
The `import` keyword was compared twice as a bare string while splitting
a from-import into module and names. Name it once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A string repeated as an inequality operand (`!=`, `!==`, F#'s `<>`,
Erlang's `/=` and `=/=`) now counts toward the magic-string threshold,
including Rust's `kind != Some("x")`.

EqualityComparison gains a Polarity (Equal | NotEqual) so each adapter
still reads comparison operators in one place. TypeScript, Kotlin, C#,
and C++ share a new binaryComparisons helper in place of four copies of
the same operand/operator/operand extraction.

Match opportunities and stringly-typed control flow keep only Equal: an
inequality excludes one value rather than selecting a branch or a
state, so their findings are unchanged. Matrix rows cover the flagged
magic string and both clean scenarios in every language.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

Energy State Diff vs origin/main

File Base Head Δ Status
src/Core/Detectors/MatchOpportunitySupport.fs 0 0 0 ⚪ unchanged
src/Core/Detectors/PrimitiveObsession.fs 4 4 0 ⚪ unchanged
src/Core/LanguageAdapter.fs 0 0 0 ⚪ unchanged
src/Core/PythonTypeInfo/Imports.fs 1 0 -1 🟢 improved
src/Languages/CPlusPlus.fs 0 0 0 ⚪ unchanged
src/Languages/CSharp.fs 0 0 0 ⚪ unchanged
src/Languages/Erlang.fs 0 0 0 ⚪ unchanged
src/Languages/FSharp.fs 0 0 0 ⚪ unchanged
src/Languages/Kotlin.fs 0 0 0 ⚪ unchanged
src/Languages/Python.fs 0 0 0 ⚪ unchanged
src/Languages/Rust.fs 0 0 0 ⚪ unchanged
src/Languages/TypeScript.fs 0 0 0 ⚪ unchanged
tests/DetectorFixtureMatrixTests.fs 4 4 0 ⚪ unchanged

13 files changed, 0 worsened, 1 improved, 0 new.

@dbrattli
dbrattli merged commit bbfd0d5 into main Nov 22, 2026
7 checks passed
@dbrattli
dbrattli deleted the feat/magic-string-inequality branch November 22, 2026 11:22
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.

1 participant