diff --git a/backend/src/lib.rs b/backend/src/lib.rs index 04d94aaa..ab781bc4 100644 --- a/backend/src/lib.rs +++ b/backend/src/lib.rs @@ -612,6 +612,8 @@ pub extern "C" fn __quantum__qis__reset__body(qubit: *mut c_void) { /// QIR API for measuring the given qubit and storing the measured value with the given result identifier, /// then resetting it in the computational basis. +#[allow(clippy::missing_panics_doc)] +// reason="Panics can only occur if the result that was just collected is not found in the BitVec, which should not happen." #[no_mangle] pub extern "C" fn __quantum__qis__mresetz__body(qubit: *mut c_void, result: *mut c_void) { SIM_STATE.with(|sim_state| { @@ -637,6 +639,8 @@ pub extern "C" fn __quantum__qis__mresetz__body(qubit: *mut c_void, result: *mut } /// QIR API for measuring the given qubit in the computation basis and storing the measured value with the given result identifier. +#[allow(clippy::missing_panics_doc)] +// reason="Panics can only occur if the result index is not found in the BitVec after resizing, which should not happen." #[no_mangle] pub extern "C" fn __quantum__qis__mz__body(qubit: *mut c_void, result: *mut c_void) { SIM_STATE.with(|sim_state| { @@ -658,6 +662,8 @@ pub extern "C" fn __quantum__qis__mz__body(qubit: *mut c_void, result: *mut c_vo /// QIR API that reads the Boolean value corresponding to the given result identifier, where true /// indicates a |1⟩ state and false indicates a |0⟩ state. +#[allow(clippy::missing_panics_doc)] +// reason="Panics can only occur if the result index is not found in the BitVec after resizing, which should not happen." #[no_mangle] pub extern "C" fn __quantum__qis__read_result__body(result: *mut c_void) -> bool { SIM_STATE.with(|sim_state| { @@ -813,6 +819,8 @@ pub mod legacy_output { SIM_STATE, }; + #[allow(clippy::missing_panics_doc)] + // reason="Panics can only occur if the result index is not found in the BitVec after resizing, which should not happen." #[allow(non_snake_case)] pub extern "C" fn __quantum__rt__result_record_output(result: *mut c_void) { SIM_STATE.with(|sim_state| { @@ -835,6 +843,8 @@ pub mod legacy_output { } /// QIR API for recording the given result into the program output. +#[allow(clippy::missing_panics_doc)] +// reason="Panics can only occur if the result index is not found in the BitVec after resizing, which should not happen." #[no_mangle] pub extern "C" fn __quantum__rt__result_record_output(result: *mut c_void, tag: *mut c_char) { SIM_STATE.with(|sim_state| { @@ -894,6 +904,8 @@ pub extern "C" fn __quantum__rt__qubit_allocate() -> *mut c_void { } /// QIR API for allocating the given number of qubits in the simulation, returning them as a runtime managed array. +/// # Panics +/// This function will panic if the requested array size is too large to be described with the system pointer size. #[allow(clippy::cast_ptr_alignment)] #[no_mangle] pub extern "C" fn __quantum__rt__qubit_allocate_array(size: u64) -> *const QirArray { @@ -936,6 +948,8 @@ pub extern "C" fn __quantum__rt__qubit_release(qubit: *mut c_void) { } /// QIR API for getting the string interpretation of a qubit identifier. +/// # Panics +/// This function will panic if memory cannot be allocated for the underyling string. #[no_mangle] pub extern "C" fn __quantum__rt__qubit_to_string(qubit: *mut c_void) -> *const CString { unsafe { diff --git a/backend/src/result_bool.rs b/backend/src/result_bool.rs index 2272bfa1..98f5851e 100644 --- a/backend/src/result_bool.rs +++ b/backend/src/result_bool.rs @@ -25,6 +25,8 @@ pub extern "C" fn __quantum__rt__result_update_reference_count(_res: *mut c_void // no-op } +/// # Panics +/// This function panics if the memory cannot be allocated for the result string. #[no_mangle] pub extern "C" fn __quantum__rt__result_to_string(res: *mut c_void) -> *const CString { unsafe { diff --git a/rust-toolchain.toml b/rust-toolchain.toml index a3ed53b3..06c996bc 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,3 +1,3 @@ [toolchain] -channel = "1.70" +channel = "1.75" components = [ "rustfmt", "clippy", "llvm-tools-preview" ] \ No newline at end of file diff --git a/sparsesim/src/lib.rs b/sparsesim/src/lib.rs index e6183ce2..6deee3f9 100644 --- a/sparsesim/src/lib.rs +++ b/sparsesim/src/lib.rs @@ -88,6 +88,7 @@ impl QuantumSim { /// Returns a sorted copy of the current sparse state as a vector of pairs of indices and complex numbers, along with /// the total number of currently allocated qubits to help in interpreting the sparse state. + #[allow(clippy::missing_panics_doc)] // reason="Panics can only occur if the keys are not present in the map, which should not happen." #[must_use] pub fn get_state(&mut self) -> (Vec<(BigUint, Complex64)>, usize) { // Swap all the entries in the state to be ordered by qubit identifier. This makes @@ -188,6 +189,7 @@ impl QuantumSim { /// Prints the current state vector to standard output with integer labels for the states, skipping any /// states with zero amplitude. + #[allow(clippy::missing_panics_doc)] // reason="Panics can only occur if the keys are not present in the map, which should not happen." pub fn dump(&mut self) { // Swap all the entries in the state to be ordered by qubit identifier. This makes // interpreting the state easier for external consumers that don't have access to the id map. @@ -446,7 +448,7 @@ impl QuantumSim { pub(crate) fn check_for_duplicates(ids: &[usize]) { let mut unique = FxHashSet::default(); - for id in ids.iter() { + for id in ids { assert!( unique.insert(id), "Duplicate qubit id '{id}' found in application." @@ -617,6 +619,7 @@ impl QuantumSim { } /// Multi-controlled Y gate. + #[allow(clippy::missing_panics_doc)] // reason="Panics can only occur if ctrls are empty, which is handled at the top of the function." pub fn mcy(&mut self, ctls: &[usize], target: usize) { if ctls.is_empty() { self.y(target); diff --git a/stdlib/src/arrays.rs b/stdlib/src/arrays.rs index e4fa160a..b6cab8a9 100644 --- a/stdlib/src/arrays.rs +++ b/stdlib/src/arrays.rs @@ -69,8 +69,7 @@ pub unsafe extern "C" fn __quantum__rt__array_concatenate( new_array.data.resize(array1.data.len(), 0_u8); new_array.data.copy_from_slice(array1.data.as_slice()); - let mut copy = Vec::new(); - copy.resize(array2.data.len(), 0_u8); + let mut copy = vec![0; array2.data.len()]; copy.copy_from_slice(array2.data.as_slice()); new_array.data.append(&mut copy); diff --git a/stdlib/src/bigints.rs b/stdlib/src/bigints.rs index 5fa800fc..1baf2360 100644 --- a/stdlib/src/bigints.rs +++ b/stdlib/src/bigints.rs @@ -172,7 +172,7 @@ mod tests { fn test_bigint_create_from_int() { let bigint_0 = __quantum__rt__bigint_create_i64(42); unsafe { - assert_eq!(*bigint_0, (42).try_into().unwrap()); + assert_eq!(*bigint_0, (42).into()); // Note that the test must decrement the reference count on any created items to ensure // they are cleaned up and tests can pass with address sanitizer. __quantum__rt__bigint_update_reference_count(bigint_0, -1); @@ -185,10 +185,7 @@ mod tests { unsafe { let bigint_1 = __quantum__rt__bigint_create_array(bytes.len().try_into().unwrap(), bytes.as_ptr()); - assert_eq!( - *bigint_1, - (9_223_372_036_854_775_807_i64).try_into().unwrap() - ); + assert_eq!(*bigint_1, (9_223_372_036_854_775_807_i64).into()); __quantum__rt__bigint_update_reference_count(bigint_1, -1); } } @@ -199,19 +196,19 @@ mod tests { let bigint_1 = __quantum__rt__bigint_create_i64(3); unsafe { let bigint_2 = __quantum__rt__bigint_add(bigint_0, bigint_1); - assert_eq!(*bigint_2, (45).try_into().unwrap()); + assert_eq!(*bigint_2, (45).into()); let bigint_3 = __quantum__rt__bigint_subtract(bigint_2, bigint_1); - assert_eq!(*bigint_3, (42).try_into().unwrap()); + assert_eq!(*bigint_3, (42).into()); let bigint_4 = __quantum__rt__bigint_divide(bigint_3, bigint_1); - assert_eq!(*bigint_4, (14).try_into().unwrap()); + assert_eq!(*bigint_4, (14).into()); let bigint_5 = __quantum__rt__bigint_multiply(bigint_4, bigint_1); - assert_eq!(*bigint_5, (42).try_into().unwrap()); + assert_eq!(*bigint_5, (42).into()); let bigint_6 = __quantum__rt__bigint_modulus(bigint_5, bigint_1); - assert_eq!(*bigint_6, (0).try_into().unwrap()); + assert_eq!(*bigint_6, (0).into()); let bigint_7 = __quantum__rt__bigint_negate(bigint_5); - assert_eq!(*bigint_7, (-42).try_into().unwrap()); + assert_eq!(*bigint_7, (-42).into()); let bigint_8 = __quantum__rt__bigint_power(bigint_7, 3); - assert_eq!(*bigint_8, (-74088).try_into().unwrap()); + assert_eq!(*bigint_8, (-74088).into()); __quantum__rt__bigint_update_reference_count(bigint_8, -1); __quantum__rt__bigint_update_reference_count(bigint_7, -1); __quantum__rt__bigint_update_reference_count(bigint_6, -1); @@ -230,17 +227,17 @@ mod tests { let bigint_1 = __quantum__rt__bigint_create_i64(3); unsafe { let bigint_2 = __quantum__rt__bigint_bitand(bigint_0, bigint_1); - assert_eq!(*bigint_2, (2).try_into().unwrap()); + assert_eq!(*bigint_2, (2).into()); let bigint_3 = __quantum__rt__bigint_bitor(bigint_0, bigint_1); - assert_eq!(*bigint_3, (43).try_into().unwrap()); + assert_eq!(*bigint_3, (43).into()); let bigint_4 = __quantum__rt__bigint_bitxor(bigint_0, bigint_3); - assert_eq!(*bigint_4, (1).try_into().unwrap()); + assert_eq!(*bigint_4, (1).into()); let bigint_5 = __quantum__rt__bigint_bitnot(bigint_4); - assert_eq!(*bigint_5, (-2).try_into().unwrap()); + assert_eq!(*bigint_5, (-2).into()); let bigint_6 = __quantum__rt__bigint_shiftleft(bigint_0, 2); - assert_eq!(*bigint_6, (168).try_into().unwrap()); + assert_eq!(*bigint_6, (168).into()); let bigint_7 = __quantum__rt__bigint_shiftright(bigint_6, 3); - assert_eq!(*bigint_7, (21).try_into().unwrap()); + assert_eq!(*bigint_7, (21).into()); __quantum__rt__bigint_update_reference_count(bigint_7, -1); __quantum__rt__bigint_update_reference_count(bigint_6, -1); __quantum__rt__bigint_update_reference_count(bigint_5, -1); diff --git a/stdlib/src/range_support.rs b/stdlib/src/range_support.rs index 66b82252..84e41e0f 100644 --- a/stdlib/src/range_support.rs +++ b/stdlib/src/range_support.rs @@ -51,8 +51,7 @@ pub unsafe extern "C" fn quantum__rt__array_slice_1d( let index = i .try_into() .expect("Item index too large for `usize` type on this platform."); - let mut copy = Vec::new(); - copy.resize(array.elem_size, 0_u8); + let mut copy = vec![0; array.elem_size]; copy.copy_from_slice(&array.data[index..index + array.elem_size]); slice.data.append(&mut copy); }