Skip to content

Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.

fix(analyze): no hash-order risk for a set tested where it is written (#1236)

iQuantumc62935a81ae1
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0f7bdd4c..1d470ec1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,9 @@
## Unreleased
+- `analyze` no longer reports "Hash iteration order" for a Python set that is tested where it
+ is written (`c in {'a', 'b'}`, `c not in set(x)`): no code can see its order. A set held by a
+ name is reported as before (#1235).
- `bridge --as-json` explains its TS/JS scope when used on another language. The CLI and MCP help
name that scope as well (#1201, #1226 by @Plastic-time).
- `app drift` lists a case whose recording was edited, marked skipped or removed under "Recording
diff --git a/src/lang/python.rs b/src/lang/python.rs
index 38534191..a4366126 100644
--- a/src/lang/python.rs
+++ b/src/lang/python.rs
@@ -429,6 +429,28 @@ fn resolve_alias(callee: &str, aliases: &HashMap<String, String>) -> String {
}
}
+/// A set written as the last operand of `in` or `not in` is gone once it has answered, so its order
+/// is never seen. In `x in {'a'} in y` the set goes on to `y`, and a set held by a name may be
+/// iterated anywhere, so both stay a risk.
+fn only_tested(set: Node) -> bool {
+ let mut node = set;
+ while let Some(parent) = node.parent()
+ && parent.kind() == "parenthesized_expression"
+ {
+ node = parent;
+ }
+ // Comments and line continuations are siblings too.
+ let code = |n: &Node| !n.is_extra();
+ node.parent()
+ .is_some_and(|p| p.kind() == "comparison_operator")
+ && std::iter::successors(node.next_sibling(), Node::next_sibling)
+ .find(code)
+ .is_none()
+ && std::iter::successors(node.prev_sibling(), Node::prev_sibling)
+ .find(code)
+ .is_some_and(|op| matches!(op.kind(), "in" | "not in"))
+}
+
pub fn scan(source: &str) -> FileFacts {
let mut facts = FileFacts {
loc: syntax::loc(source, crate::lang::Lang::Python),
@@ -509,7 +531,9 @@ pub fn scan(source: &str) -> FileFacts {
_ if callee.starts_with("random.") || callee.starts_with("secrets.") => {
add(&mut facts, Risk::ClockAndRandom, node);
}
- "set" | "frozenset" => add(&mut facts, Risk::UnorderedIteration, node),
+ "set" | "frozenset" if !only_tested(node) => {
+ add(&mut facts, Risk::UnorderedIteration, node)
+ }
"threading.Thread"
| "Thread"
| "ThreadPoolExecutor"
@@ -572,7 +596,9 @@ pub fn scan(source: &str) -> FileFacts {
_ => {}
}
}
- "set" | "set_comprehension" => add(&mut facts, Risk::UnorderedIteration, node),
+ "set" | "set_comprehension" if !only_tested(node) => {
+ add(&mut facts, Risk::UnorderedIteration, node)
+ }
"decorator" if crate::risks::compiled_by(text(node, source)).is_some() => {
add(&mut facts, Risk::AlreadyCompiled, node)
}
@@ -2293,6 +2319,34 @@ mod tests {
);
}
+ #[test]
+ fn a_set_tested_where_it_is_written_has_no_order_to_keep() {
+ let risk = |source: &str| scan(source).risks.get(&Risk::UnorderedIteration).cloned();
+ for source in [
+ "x = c in {'a', 'b'}\n",
+ "x = c not in {'a'}\n",
+ "x = c in ({'a'})\n",
+ "x = c in set('ab') or c in frozenset(s)\n",
+ "x = c in {v for v in 'ab'}\n",
+ "x = (c in # why\n {'a'})\n",
+ "x = c in \\\n {'a'}\n",
+ "x = (c in {'a'} # why\n)\n",
+ ] {
+ assert_eq!(risk(source), None, "{source}");
+ }
+ for source in [
+ "s = {'a'}\nx = c in s\n",
+ "x = [c for c in {'a', 'b'}]\n",
+ "for c in set('ab'):\n pass\n",
+ "x = {'a'} in groups\n",
+ "x = c in {'a'} in groups\n",
+ "x = c == {'a'}\n",
+ "x = c in list({'a'})\n",
+ ] {
+ assert!(risk(source).is_some(), "{source}");
+ }
+ }
+
#[test]
fn infers_numbers_from_arithmetic() {
let src = "def rgb_to_hsv(r, g, b):\n maxc = max(r, g, b)\n return maxc - r\n\n\