Skip to content

Restrict unsafe blocks to single statements where possible.#15806

Open
Cryoris wants to merge 1 commit intoQiskit:mainfrom
Cryoris:unsafe-surgeon-mode
Open

Restrict unsafe blocks to single statements where possible.#15806
Cryoris wants to merge 1 commit intoQiskit:mainfrom
Cryoris:unsafe-surgeon-mode

Conversation

@Cryoris
Copy link
Copy Markdown
Collaborator

@Cryoris Cryoris commented Mar 13, 2026

Summary

Following up on #15106 (comment) by reducing the sizes of unsafe statements in the codebase.

Details and comments

To see which part of the code is unsafe, it is good to restrict unsafe blocks to the smallest sensible code unit. Clippy also has a feature to check this, cargo clippy -- -W clippy::multiple_unsafe_ops_per_block. This commit reduces some pretty large unsafe blocks to smaller ones to improve our code structure. Clippy actually flags any snippet that has more than a single statement, but I left unsafe blocks in place where every single statement was actually unsafe.

To see which part of the code is unsafe, it is good to restrict `unsafe` blocks to the smallest sensible code unit. Clippy also has a feature to check this, `cargo clippy -- -W clippy::multiple_unsafe_ops_per_block`. This commit reduces some pretty large unsafe blocks to smaller ones to improve our code structure. Clippy actually flags _any_ snippet that has more than a single statement, but I left unsafe blocks in place where every single statement was actually unsafe.
@Cryoris Cryoris added this to the 2.5.0 milestone Mar 13, 2026
@Cryoris Cryoris requested a review from a team as a code owner March 13, 2026 12:52
@Cryoris Cryoris added type: qa Issues and PRs that relate to testing and code quality Changelog: None Do not include in the GitHub Release changelog. labels Mar 13, 2026
@qiskit-bot
Copy link
Copy Markdown
Collaborator

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core

Copy link
Copy Markdown
Contributor

@gadial gadial left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good to me. Left minor comments.

@@ -1569,26 +1568,22 @@ pub unsafe extern "C" fn qk_target_op_clear(op: *mut CTargetOp) {

// SAFETY: As per documentation, data from pointers contained in CTargetOp
// originates from rust code and are constructed internally with vecs and CStrings.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this cover the mut_ptr_as_ref(op) operation?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not! Let me adjust 🙂

};
new_node.index() as u32
}
// SAFETY: Per the documentation the qubits pointer is an arrays of num_qubits() elements
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// SAFETY: Per the documentation the qubits pointer is an arrays of num_qubits() elements
// SAFETY: Per the documentation the qubits pointer is an array of num_qubits() elements

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Changelog: None Do not include in the GitHub Release changelog. type: qa Issues and PRs that relate to testing and code quality

Projects

Status: Ready

Development

Successfully merging this pull request may close these issues.

3 participants