Restrict unsafe blocks to single statements where possible.#15806
Open
Cryoris wants to merge 1 commit intoQiskit:mainfrom
Open
Restrict unsafe blocks to single statements where possible.#15806Cryoris wants to merge 1 commit intoQiskit:mainfrom
unsafe blocks to single statements where possible.#15806Cryoris wants to merge 1 commit intoQiskit:mainfrom
Conversation
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.
Collaborator
|
One or more of the following people are relevant to this code:
|
gadial
reviewed
Mar 15, 2026
Contributor
gadial
left a comment
There was a problem hiding this comment.
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. | |||
Contributor
There was a problem hiding this comment.
Does this cover the mut_ptr_as_ref(op) operation?
Collaborator
Author
There was a problem hiding this comment.
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 |
Contributor
There was a problem hiding this comment.
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Following up on #15106 (comment) by reducing the sizes of
unsafestatements in the codebase.Details and comments
To see which part of the code is unsafe, it is good to restrict
unsafeblocks 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.