Skip to content

Commit ec09525

Browse files
authored
Merge branch 'main' into feature/do-not-divide-by-0
2 parents 9dabe78 + 22aac67 commit ec09525

22 files changed

+1574
-125
lines changed

.github/auto-pr-tests/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
## How to Add and Document Tests in `auto-pr-tests`
2+
3+
The test script `scripts/auto-pr-helper.py` transforms an issue from JSON format into our `.rst` format.
4+
5+
This directory contains test issue files in JSON format along with their expected output snapshots. These tests are executed by the script `test_runner.py`.
6+
7+
### Adding a New Test
8+
9+
1. **Create Input JSON File**
10+
11+
First, obtain the JSON data for the GitHub issue you want to use as a test case. Name the file test_issue_XX.json, where XX is a number, Instructions on how to get this JSON data are provided in the next section.
12+
13+
2. **Generate Expected Output Snapshot**
14+
Run the following command to generate the corresponding `.snapshot` file automatically:
15+
16+
```bash
17+
cat .github/auto-pr-tests/test_issue_XX.json | uv run scripts/auto-pr-helper.py > .github/auto-pr-tests/test_issue_XX.snapshot
18+
```
19+
It is better to run this command and manually verify the output, rather than creating the snapshot manually.
20+
3. **Add Test to the Test List**
21+
Add your new JSON and snapshot file paths to the tests dictionary inside test_runner.py(line 47). This registers the new test so it will be run.
22+
4. Run Tests
23+
Execute test_runner.py to verify that the output matches the expected snapshots.
24+
25+
26+
### How to Get Issue JSON from GitHub API
27+
28+
To create the input JSON file (`test_issue_XX.json`), you can fetch the issue data directly from the GitHub API:
29+
30+
1. Find the issue number and repository where the issue is located.
31+
32+
2. Use a tool like `curl` or any HTTP client to request the issue JSON data:
33+
34+
```bash
35+
curl https://api.github.com/repos/OWNER/REPO/issues/ISSUE_NUMBER > test_issue_XX.json
36+
```
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
{
2+
"url": "https://api.github.com/repos/rustfoundation/safety-critical-rust-coding-guidelines/issues/4",
3+
"repository_url": "https://api.github.com/repos/rustfoundation/safety-critical-rust-coding-guidelines",
4+
"labels_url": "https://api.github.com/repos/rustfoundation/safety-critical-rust-coding-guidelines/issues/4/labels{/name}",
5+
"comments_url": "https://api.github.com/repos/rustfoundation/safety-critical-rust-coding-guidelines/issues/4/comments",
6+
"events_url": "https://api.github.com/repos/rustfoundation/safety-critical-rust-coding-guidelines/issues/4/events",
7+
"html_url": "https://github.com/rustfoundation/safety-critical-rust-coding-guidelines/issues/4",
8+
"id": 3104390263,
9+
"node_id": "I_kwDOOMMjbs65CTx3",
10+
"number": 4,
11+
"title": "[Coding Guideline]: testtt",
12+
"user": {
13+
"login": "rustfoundation",
14+
"id": 14003018,
15+
"node_id": "MDQ6VXNlcjE0MDAzMDE4",
16+
"avatar_url": "https://avatars.githubusercontent.com/u/14003018?v=4",
17+
"gravatar_id": "",
18+
"url": "https://api.github.com/users/rustfoundation",
19+
"html_url": "https://github.com/rustfoundation",
20+
"followers_url": "https://api.github.com/users/rustfoundation/followers",
21+
"following_url": "https://api.github.com/users/rustfoundation/following{/other_user}",
22+
"gists_url": "https://api.github.com/users/rustfoundation/gists{/gist_id}",
23+
"starred_url": "https://api.github.com/users/rustfoundation/starred{/owner}{/repo}",
24+
"subscriptions_url": "https://api.github.com/users/rustfoundation/subscriptions",
25+
"organizations_url": "https://api.github.com/users/rustfoundation/orgs",
26+
"repos_url": "https://api.github.com/users/rustfoundation/repos",
27+
"events_url": "https://api.github.com/users/rustfoundation/events{/privacy}",
28+
"received_events_url": "https://api.github.com/users/rustfoundation/received_events",
29+
"type": "User",
30+
"user_view_type": "public",
31+
"site_admin": false
32+
},
33+
"labels": [
34+
{
35+
"id": 8703664686,
36+
"node_id": "LA_kwDOOMMjbs8AAAACBsdiLg",
37+
"url": "https://api.github.com/repos/rustfoundation/safety-critical-rust-coding-guidelines/labels/category:%20advisory",
38+
"name": "category: advisory",
39+
"color": "ededed",
40+
"default": false,
41+
"description": null
42+
},
43+
{
44+
"id": 8703664688,
45+
"node_id": "LA_kwDOOMMjbs8AAAACBsdiMA",
46+
"url": "https://api.github.com/repos/rustfoundation/safety-critical-rust-coding-guidelines/labels/status:%20draft",
47+
"name": "status: draft",
48+
"color": "ededed",
49+
"default": false,
50+
"description": null
51+
},
52+
{
53+
"id": 8703664689,
54+
"node_id": "LA_kwDOOMMjbs8AAAACBsdiMQ",
55+
"url": "https://api.github.com/repos/rustfoundation/safety-critical-rust-coding-guidelines/labels/decidability:%20decidable",
56+
"name": "decidability: decidable",
57+
"color": "ededed",
58+
"default": false,
59+
"description": null
60+
},
61+
{
62+
"id": 8703686409,
63+
"node_id": "LA_kwDOOMMjbs8AAAACBse3CQ",
64+
"url": "https://api.github.com/repos/rustfoundation/safety-critical-rust-coding-guidelines/labels/chapter:%20concurrency",
65+
"name": "chapter: concurrency",
66+
"color": "ededed",
67+
"default": false,
68+
"description": null
69+
},
70+
{
71+
"id": 8703686412,
72+
"node_id": "LA_kwDOOMMjbs8AAAACBse3DA",
73+
"url": "https://api.github.com/repos/rustfoundation/safety-critical-rust-coding-guidelines/labels/scope:%20crate",
74+
"name": "scope: crate",
75+
"color": "ededed",
76+
"default": false,
77+
"description": null
78+
},
79+
{
80+
"id": 8703732885,
81+
"node_id": "LA_kwDOOMMjbs8AAAACBshslQ",
82+
"url": "https://api.github.com/repos/rustfoundation/safety-critical-rust-coding-guidelines/labels/accepted",
83+
"name": "accepted",
84+
"color": "6AABE8",
85+
"default": false,
86+
"description": ""
87+
}
88+
],
89+
"state": "open",
90+
"locked": false,
91+
"assignee": null,
92+
"assignees": [
93+
94+
],
95+
"milestone": null,
96+
"comments": 0,
97+
"created_at": "2025-05-30T22:24:07Z",
98+
"updated_at": "2025-05-30T22:48:17Z",
99+
"closed_at": null,
100+
"author_association": "OWNER",
101+
"active_lock_reason": null,
102+
"sub_issues_summary": {
103+
"total": 0,
104+
"completed": 0,
105+
"percent_completed": 0
106+
},
107+
"body": "### Chapter\n\nConcurrency\n\n### Guideline Title\n\ntest ga\n\n### Category\n\nAdvisory\n\n### Status\n\nDraft\n\n### Release Begin\n\n1.1.1\n\n### Release End\n\n1.1.1\n\n### FLS Paragraph ID\n\nfls_fsdjkfslkdfj\n\n### Decidability\n\nDecidable\n\n### Scope\n\nCrate\n\n### Tags\n\ntest gatest ga\n\n### Amplification\n\nhehehehe\n\n### Exception(s)\n\n_No response_\n\n### Rationale\n\ntest ga\n\n### Non-Compliant Example - Prose\n\ntest ga\n\n### Non-Compliant Example - Code\n\ndfhsdfkjshdfskdjhftest ga\n\n### Compliant Example - Prose\n\ntest ga\n\n### Compliant Example - Code\n\ntest ga",
108+
"closed_by": null,
109+
"reactions": {
110+
"url": "https://api.github.com/repos/rustfoundation/safety-critical-rust-coding-guidelines/issues/4/reactions",
111+
"total_count": 0,
112+
"+1": 0,
113+
"-1": 0,
114+
"laugh": 0,
115+
"hooray": 0,
116+
"confused": 0,
117+
"heart": 0,
118+
"rocket": 0,
119+
"eyes": 0
120+
},
121+
"timeline_url": "https://api.github.com/repos/rustfoundation/safety-critical-rust-coding-guidelines/issues/4/timeline",
122+
"performed_via_github_app": null,
123+
"state_reason": null
124+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
=====CONTENT=====
2+
3+
.. guideline:: test ga
4+
:id: gui_3Wl9gOTto7p2
5+
:category: advisory
6+
:status: draft
7+
:release: 1.1.1-1.1.1
8+
:fls: fls_fsdjkfslkdfj
9+
:decidability: decidable
10+
:scope: crate
11+
:tags: test gatest ga
12+
13+
hehehehe
14+
15+
.. rationale::
16+
:id: rat_PVjXjtM9G383
17+
:status: draft
18+
19+
test ga
20+
21+
.. non_compliant_example::
22+
:id: non_compl_ex_ABSVsT97MwLG
23+
:status: draft
24+
25+
test ga
26+
27+
.. code-block:: rust
28+
29+
dfhsdfkjshdfskdjhftest ga
30+
31+
.. compliant_example::
32+
:id: compl_ex_Z9TjeDsRTnwL
33+
:status: draft
34+
35+
test ga
36+
37+
.. code-block:: rust
38+
39+
test ga
40+
41+
=====CONTENT=END=====
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
{
2+
"url": "https://api.github.com/repos/rustfoundation/safety-critical-rust-coding-guidelines/issues/4",
3+
"repository_url": "https://api.github.com/repos/rustfoundation/safety-critical-rust-coding-guidelines",
4+
"labels_url": "https://api.github.com/repos/rustfoundation/safety-critical-rust-coding-guidelines/issues/4/labels{/name}",
5+
"comments_url": "https://api.github.com/repos/rustfoundation/safety-critical-rust-coding-guidelines/issues/4/comments",
6+
"events_url": "https://api.github.com/repos/rustfoundation/safety-critical-rust-coding-guidelines/issues/4/events",
7+
"html_url": "https://github.com/rustfoundation/safety-critical-rust-coding-guidelines/issues/4",
8+
"id": 3104390263,
9+
"node_id": "I_kwDOOMMjbs65CTx3",
10+
"number": 4,
11+
"title": "[Coding Guideline]: testtt",
12+
"user": {
13+
"login": "rustfoundation",
14+
"id": 14003018,
15+
"node_id": "MDQ6VXNlcjE0MDAzMDE4",
16+
"avatar_url": "https://avatars.githubusercontent.com/u/14003018?v=4",
17+
"gravatar_id": "",
18+
"url": "https://api.github.com/users/rustfoundation",
19+
"html_url": "https://github.com/rustfoundation",
20+
"followers_url": "https://api.github.com/users/rustfoundation/followers",
21+
"following_url": "https://api.github.com/users/rustfoundation/following{/other_user}",
22+
"gists_url": "https://api.github.com/users/rustfoundation/gists{/gist_id}",
23+
"starred_url": "https://api.github.com/users/rustfoundation/starred{/owner}{/repo}",
24+
"subscriptions_url": "https://api.github.com/users/rustfoundation/subscriptions",
25+
"organizations_url": "https://api.github.com/users/rustfoundation/orgs",
26+
"repos_url": "https://api.github.com/users/rustfoundation/repos",
27+
"events_url": "https://api.github.com/users/rustfoundation/events{/privacy}",
28+
"received_events_url": "https://api.github.com/users/rustfoundation/received_events",
29+
"type": "User",
30+
"user_view_type": "public",
31+
"site_admin": false
32+
},
33+
"labels": [
34+
{
35+
"id": 8703664686,
36+
"node_id": "LA_kwDOOMMjbs8AAAACBsdiLg",
37+
"url": "https://api.github.com/repos/rustfoundation/safety-critical-rust-coding-guidelines/labels/category:%20advisory",
38+
"name": "category: advisory",
39+
"color": "ededed",
40+
"default": false,
41+
"description": null
42+
},
43+
{
44+
"id": 8703664688,
45+
"node_id": "LA_kwDOOMMjbs8AAAACBsdiMA",
46+
"url": "https://api.github.com/repos/rustfoundation/safety-critical-rust-coding-guidelines/labels/status:%20draft",
47+
"name": "status: draft",
48+
"color": "ededed",
49+
"default": false,
50+
"description": null
51+
},
52+
{
53+
"id": 8703664689,
54+
"node_id": "LA_kwDOOMMjbs8AAAACBsdiMQ",
55+
"url": "https://api.github.com/repos/rustfoundation/safety-critical-rust-coding-guidelines/labels/decidability:%20decidable",
56+
"name": "decidability: decidable",
57+
"color": "ededed",
58+
"default": false,
59+
"description": null
60+
},
61+
{
62+
"id": 8703686409,
63+
"node_id": "LA_kwDOOMMjbs8AAAACBse3CQ",
64+
"url": "https://api.github.com/repos/rustfoundation/safety-critical-rust-coding-guidelines/labels/chapter:%20concurrency",
65+
"name": "chapter: concurrency",
66+
"color": "ededed",
67+
"default": false,
68+
"description": null
69+
},
70+
{
71+
"id": 8703686412,
72+
"node_id": "LA_kwDOOMMjbs8AAAACBse3DA",
73+
"url": "https://api.github.com/repos/rustfoundation/safety-critical-rust-coding-guidelines/labels/scope:%20crate",
74+
"name": "scope: crate",
75+
"color": "ededed",
76+
"default": false,
77+
"description": null
78+
},
79+
{
80+
"id": 8703732885,
81+
"node_id": "LA_kwDOOMMjbs8AAAACBshslQ",
82+
"url": "https://api.github.com/repos/rustfoundation/safety-critical-rust-coding-guidelines/labels/accepted",
83+
"name": "accepted",
84+
"color": "6AABE8",
85+
"default": false,
86+
"description": ""
87+
}
88+
],
89+
"state": "open",
90+
"locked": false,
91+
"assignee": null,
92+
"assignees": [
93+
94+
],
95+
"milestone": null,
96+
"comments": 0,
97+
"created_at": "2025-05-30T22:24:07Z",
98+
"updated_at": "2025-05-30T22:48:17Z",
99+
"closed_at": null,
100+
"author_association": "OWNER",
101+
"active_lock_reason": null,
102+
"sub_issues_summary": {
103+
"total": 0,
104+
"completed": 0,
105+
"percent_completed": 0
106+
},
107+
"body": "### Chapter\n\nAssociated Items\n\n### Guideline Title\n\nRecursive function are not allowed\n\n### Category\n\nRequired\n\n### Status\n\nDraft\n\n### Release Begin\n\n1.3.0\n\n### Release End\n\nlatest\n\n### FLS Paragraph ID\n\nfls_vjgkg8kfi93\n\n### Decidability\n\nUndecidable\n\n### Scope\n\nSystem\n\n### Tags\n\nreduce-human-error\n\n### Amplification\n\nAny function shall not call itself directly or indirectly\n\n### Exception(s)\n\nRecursion may be permitted under the following conditions:\n- The recursion termination condition is simple, explicit, and well-defined.\n- The function calls itself directly, or with strictly limited and clearly documented indirection.\n- The maximum recursion depth is statically bounded and justified, ensuring no risk of stack overflow.\n- The rationale for using recursion, rather than iteration, is clearly documented and reviewed.\n- The code is accompanied by tests that exercise the recursion boundary conditions.\n\n### Rationale\n\nRecursive functions can easily cause stack overflows, which may result in exceptions or, in some cases, undefined behavior (typically some embedded systems). Although the Rust compiler supports [tail call optimization](https://en.wikipedia.org/wiki/Tail_call), this optimization is not guaranteed and depends on the specific implementation and function structure. There is an [open RFC to guarantee tail call optimization in the Rust compiler](https://github.com/phi-go/rfcs/blob/guaranteed-tco/text/0000-explicit-tail-calls.md), but this feature has not yet been stabilized. Until tail call optimization is guaranteed and stabilized, developers should avoid using recursive functions to prevent potential stack overflows and ensure program reliability.\n\n### Non-Compliant Example - Prose\n\nThe below function `concat_strings` is not complaint because it call itself and depending on depth of data provided as input it could generate an stack overflow exception or undefine behavior.\n\n### Non-Compliant Example - Code\n\n```rust\n// Recursive enum to represent a string or a list of `MyEnum`\nenum MyEnum {\n Str(String),\n List(Vec<MyEnum>),\n}\n\n// Concatenates strings from a nested structure of `MyEnum` using recursion.\nfn concat_strings(input: &[MyEnum]) -> String {\n let mut result = String::new();\n for item in input {\n match item {\n MyEnum::Str(s) => result.push_str(s),\n MyEnum::List(list) => result.push_str(&concat_strings(list)),\n }\n }\n result\n}\n```\n\n### Compliant Example - Prose\n\nThe following code implements the same functionality using iteration instead of recursion. The `stack` variable is used to maintain the processing context at each step of the loop. This approach provides explicit control over memory usage. If the stack grows beyond a predefined limit due to the structure or size of the input, the function returns an error rather than risking a stack overflow or out-of-memory exception. This ensures more predictable and robust behavior in resource-constrained environments.\n\n### Compliant Example - Code\n\n```rust\n// Recursive enum to represent a string or a list of `MyEnum`\nenum MyEnum {\n Str(String),\n List(Vec<MyEnum>),\n}\n\n/// Concatenates strings from a nested structure of `MyEnum` without using recursion.\n/// Returns an error if the stack size exceeds `MAX_STACK_SIZE`.\nfn concat_strings_non_recursive(input: &[MyEnum]) -> Result<String, &'static str> {\n const MAX_STACK_SIZE: usize = 1000;\n let mut result = String::new();\n let mut stack = Vec::new();\n\n // Add all items to the stack\n stack.extend(input.iter());\n\n while let Some(item) = stack.pop() {\n match item {\n MyEnum::Str(s) => result.insert_str(0, s),\n MyEnum::List(list) => {\n // Add list items to the stack\n for sub_item in list.iter() {\n stack.push(sub_item);\n if stack.len() > MAX_STACK_SIZE {\n return Err(\"Too big structure\");\n }\n }\n }\n }\n }\n Ok(result)\n}\n```",
108+
"closed_by": null,
109+
"reactions": {
110+
"url": "https://api.github.com/repos/rustfoundation/safety-critical-rust-coding-guidelines/issues/4/reactions",
111+
"total_count": 0,
112+
"+1": 0,
113+
"-1": 0,
114+
"laugh": 0,
115+
"hooray": 0,
116+
"confused": 0,
117+
"heart": 0,
118+
"rocket": 0,
119+
"eyes": 0
120+
},
121+
"timeline_url": "https://api.github.com/repos/rustfoundation/safety-critical-rust-coding-guidelines/issues/4/timeline",
122+
"performed_via_github_app": null,
123+
"state_reason": null
124+
}

0 commit comments

Comments
 (0)