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.
Rust agent-skillsclaude-codeclicode-migrationcodexffigolangjavajavascriptmcpmigrationnapiperformanceportingpyo3pythonrewriterubyrusttypescript
Count shuffle and random picks from a collection as randomness (#1276)
jakalijeyccba5fc0435d
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 58a40a7b..fa94e9c1 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -2,6 +2,11 @@## Unreleased+- Picking at random from a collection counts as randomness: Swift `.shuffled()` and+ `.randomElement()` (the entries never matched, since a name after `.` is skipped), Ruby `.shuffle`+ and `.sample`, and Java and Kotlin `.shuffled()`, `.random()` and `Collections.shuffle(`. Before,+ such a function was bridged with one recorded case the port could never match again (#1257 by+ @jakalijey).- The website home page lists Swift: in the language grid (Tested), the hero animation's "also"line, the page description and the languages without batches, and the stat says 11 languages(#1265 by @jakalijey).diff --git a/docs/how-it-works.md b/docs/how-it-works.mdindex ca74e259..c0a0243e 100644--- a/docs/how-it-works.md+++ b/docs/how-it-works.md@@ -304,7 +304,7 @@ bad it usually is and gives one example. The severities are the ones `analyze` s| Already compiled (numba, Cython, JAX) | medium | Python: decorated code that is not plain Python any more || numpy array parameters | high | Python: `np.ndarray` arguments and returns || Platform-specific C (asm, volatile, bitfields, packing) | high | C: `asm`, `volatile`, `:3` bitfields, `#pragma pack` |-| Clock and random numbers | low | TS/JS `Date.now`/`Math.random`, Python `time.time`/`random`/`os.urandom` (also `from time import time`), Go `time.Now`/`rand`, Java `System.currentTimeMillis`, Ruby `Time.now`, PHP `date`/`time`, C# `DateTime.Now`, Swift `Date()`, PowerShell `Get-Date`/`Get-Random`/`New-Guid`: `record --pin-clock` covers TS/JS with the napi bridge and Python; elsewhere compare by hand |+| Clock and random numbers | low | TS/JS `Date.now`/`Math.random`, Python `time.time`/`random`/`os.urandom` (also `from time import time`), Go `time.Now`/`rand`, Java `System.currentTimeMillis`, Java and Kotlin `.shuffled()`/`.random()`/`Collections.shuffle`, Ruby `Time.now`/`.shuffle`/`.sample`, PHP `date`/`time`, C# `DateTime.Now`, Swift `Date()`/`.shuffled()`/`.randomElement()`, PowerShell `Get-Date`/`Get-Random`/`New-Guid`: `record --pin-clock` covers TS/JS with the napi bridge and Python; elsewhere compare by hand || Implicit output and the pipeline | medium | PowerShell: `Write-Output`, `echo` and values left uncaptured go to the pipeline || Case-insensitive comparison | medium | PowerShell `-eq`, `-like`, `-match`, `-contains` and `switch` ignore case || Implicit type conversion | medium | PowerShell: casts such as `[int]"abc"`, and `/` on integers giving a double |diff --git a/src/lang/effects.rs b/src/lang/effects.rsindex d38b1b5e..3786046e 100644--- a/src/lang/effects.rs+++ b/src/lang/effects.rs@@ -218,6 +218,11 @@ fn clock_and_random(lang: Lang) -> &'static [&'static str] {"Math.random(","UUID.randomUUID(","Random.",+ ".shuffled(",+ ".random(",+ "Collections.shuffle(",+ // `java.util.Collections.shuffle(`: a name after `.` is skipped unless the entry starts with one.+ ".Collections.shuffle(",],Lang::Ruby => &["Time.now",@@ -226,6 +231,8 @@ fn clock_and_random(lang: Lang) -> &'static [&'static str] {"rand(","Random.","SecureRandom",+ ".shuffle",+ ".sample",],Lang::Php => &["time(",@@ -251,8 +258,8 @@ fn clock_and_random(lang: Lang) -> &'static [&'static str] {"Date.now",".random","UUID()",- "randomElement(",- "shuffled(",+ ".randomElement(",+ ".shuffled(",],Lang::C => &["time(", "rand(", "clock("],}@@ -534,14 +541,17 @@ fn is_ident(c: char) -> bool {c.is_alphanumeric() || c == '_' || c == '$'}-/// Positions of `needle` in `hay` that do not continue a longer name before them.+/// Positions of `needle` in `hay` that do not continue a longer name before them, nor, for a+/// method name such as `.sample`, after them (`.sample_rate`, `.samples`).fn find_all<'a>(hay: &'a str, needle: &'a str) -> impl Iterator<Item = usize> + 'a {+ let method = needle.starts_with('.') && needle.chars().next_back().is_some_and(is_ident);hay.match_indices(needle).filter_map(move |(i, _)| {let before = hay[..i].chars().next_back();let first = needle.chars().next().unwrap_or(' ');// `pprint(` is not `print(`, `x.print(` is not a global print.let continues = before.is_some_and(|c| (is_ident(first) && is_ident(c)) || c == '.');- (!continues || needle.starts_with('$')).then_some(i)+ let longer = method && hay[i + needle.len()..].starts_with(is_ident);+ ((!continues || needle.starts_with('$')) && !longer).then_some(i)})}@@ -1390,6 +1400,48 @@ mod tests {fn scans_list_environment_clock_and_hash_order_in_every_spelling() {use crate::risks::Risk;let cases: &[(Lang, &str, &str, Risk)] = &[+ (+ Lang::Swift,+ "Deck.swift",+ "let suits = [\"hearts\", \"spades\"]\npublic func shuffledSuits() -> [String] {\n return suits.shuffled()\n}\n",+ Risk::ClockAndRandom,+ ),+ (+ Lang::Ruby,+ "deck.rb",+ "module Deck\n SUITS = %w[hearts spades].freeze\n def self.shuffled_suits\n SUITS.shuffle\n end\nend\n",+ Risk::ClockAndRandom,+ ),+ (+ Lang::Ruby,+ "deck.rb",+ "module Deck\n SUITS = %w[hearts spades].freeze\n def self.any_suit\n SUITS.sample\n end\nend\n",+ Risk::ClockAndRandom,+ ),+ (+ Lang::Kotlin,+ "Deck.kt",+ "fun shuffledSuits(): List<String> = listOf(\"hearts\", \"spades\").shuffled()\n",+ Risk::ClockAndRandom,+ ),+ (+ Lang::Kotlin,+ "Deck.kt",+ "fun anySuit(): String = listOf(\"hearts\", \"spades\").random()\n",+ Risk::ClockAndRandom,+ ),+ (+ Lang::Java,+ "Deck.java",+ "class Deck { static void f(java.util.List<String> xs) { Collections.shuffle(xs); } }",+ Risk::ClockAndRandom,+ ),+ (+ Lang::Java,+ "Deck.java",+ "class Deck { static java.util.List<String> f() { java.util.List<String> xs = new java.util.ArrayList<>(java.util.List.of(\"a\", \"b\")); java.util.Collections.shuffle(xs); return xs; } }",+ Risk::ClockAndRandom,+ ),(Lang::Go,"x.go",@@ -1498,6 +1550,30 @@ mod tests {// An instance variable is a class's own state, not a risk of the file.let facts = crate::lang::scan(Lang::Ruby, "x.rb", "class A\n def f\n @x\n end\nend\n");assert!(!facts.risks.contains_key(&Risk::SharedState));+ for code in [+ "module A\n def self.rate\n Audio.sample_rate\n end\nend\n",+ "module A\n def self.n\n DATA.samples.size\n end\nend\n",+ ] {+ let facts = crate::lang::scan(Lang::Ruby, "x.rb", code);+ assert!(+ !facts.risks.contains_key(&Risk::ClockAndRandom),+ "{code}: {:?}",+ facts.risks+ );+ }+ let facts = crate::lang::scan(+ Lang::Ruby,+ "x.rb",+ "module A\n def self.f\n SUITS.dup.shuffle!\n end\nend\n",+ );+ assert!(facts.risks.contains_key(&Risk::ClockAndRandom));+ assert!(reason(Lang::Ruby, &[], "def self.rate\n Audio.sample_rate\nend\n").is_none());+ let facts = crate::lang::scan(+ Lang::Swift,+ "x.swift",+ "func f() -> String? { return [\"a\", \"b\"].randomElement() }\n",+ );+ assert!(facts.risks.contains_key(&Risk::ClockAndRandom));}#[test]