Skip to content

Conversation

mtreinish
Copy link
Member

Summary

This commit fixes some issues the clippy on nightly has identified as issues in the code. These will all become CI failures at some future date (after we raise the MSRV to match the current rules nightly is adding). However, most of the changes are good improvements and in this case most are code style issues that make things a bit more concise. The bulk of the changes here are just moving to use captured identifiers in format strings.

Details and comments

This commit fixes some issues the clippy on nightly has identified as
issues in the code. These will all become CI failures at some future
date (after we raise the MSRV to match the current rules nightly is
adding). However, most of the changes are good improvements and in this
case most are code style issues that make things a bit more concise. The
bulk of the changes here are just moving to use captured identifiers in
format strings.
@mtreinish mtreinish requested a review from a team as a code owner May 23, 2025 21:29
@mtreinish mtreinish added Changelog: None Do not include in changelog Rust This PR or issue is related to Rust code in the repository labels May 23, 2025
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core
  • @levbishop

@coveralls
Copy link

coveralls commented May 23, 2025

Pull Request Test Coverage Report for Build 15960219247

Details

  • 129 of 244 (52.87%) changed or added relevant lines in 46 files are covered.
  • 4 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.07%) to 87.742%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/accelerate/src/optimize_1q_gates.rs 0 1 0.0%
crates/circuit/src/circuit_instruction.rs 0 1 0.0%
crates/circuit/src/object_registry.rs 2 3 66.67%
crates/circuit/src/operations.rs 1 2 50.0%
crates/circuit/src/register_data.rs 3 4 75.0%
crates/circuit/src/symbol_parser.rs 1 2 50.0%
crates/quantum_info/src/unitary_compose.rs 1 2 50.0%
crates/synthesis/src/clifford/utils.rs 0 1 0.0%
crates/synthesis/src/linear/utils.rs 0 1 0.0%
crates/transpiler/src/equivalence.rs 2 3 66.67%
Files with Coverage Reduction New Missed Lines %
crates/transpiler/src/passes/unitary_synthesis.rs 1 93.43%
crates/qasm2/src/lex.rs 3 92.01%
Totals Coverage Status
Change from base Build 15925725626: 0.07%
Covered Lines: 80988
Relevant Lines: 92302

💛 - Coveralls

alexanderivrii
alexanderivrii previously approved these changes May 25, 2025
Copy link
Member

@alexanderivrii alexanderivrii left a comment

Choose a reason for hiding this comment

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

This looks straightforward and imho the new style is easier to read. I had one minor question (but no changes are required).

Are we planning to include this for 2.1? (Personally, I think it would be good to do so, but the PR is not marked for 2.1). In any case, I am approving it, and feel free to merge it in case this is for 2.1.

Comment on lines -6529 to +6505
std::mem::swap(self, &mut dag_builder.build());
*self = dag_builder.build();
Copy link
Member

Choose a reason for hiding this comment

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

This change makes sense. I am wondering though why this line had to change, but the line a few lines above it let mut dag_builder = replacement_dag.into_builder(); did not. A more global question, why do we even need to do the extending in a different replacement_dag DAG and not in the self DAG itself?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is a future clippy rule: https://rust-lang.github.io/rust-clippy/master/index.html#swap_with_temporary the builder line didn't change because it doesn't match the rule. But functional the semantics of .into_builder() are different, it creates a dag builder out of dag.

The dag builder is setup so that it avoids adding edges to the output node until the very end. For example if you have the dag A->B->C and want to add 2 nodes D and E between B and C. Instead of adding edges into C for D and then removing it when E is added the dag builder tracks the op nodes for each output in a vec and adds the final edges when .build() is called. It reduces the number of edge operations when building a dag up.

As for why there is a temporary dag created I think it's because the dag builder takes ownership of the dag to avoid any other mutation or reads while it's being built up. But the try_extend() method uses a mutable borrow so it can't call .into_builder() on self since the function doesn't own self. I didn't write this method but this is my guess from reading the code.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the detailed explanation!

@mtreinish
Copy link
Member Author

Are we planning to include this for 2.1? (Personally, I think it would be good to do so, but the PR is not marked for 2.1). In any case, I am approving it, and feel free to merge it in case this is for 2.1.

I didn't feel it was critical or necessary for 2.1 I only opened this because I was trying nightly to test something locally and encountered the warnings for this and the changes were easy to make.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can't comment on it since it isn't something you've changed but I found another one!
Line 881 in this file. Should change from:

            return Self::from_label(&label).map_err(PyErr::from);

to

            return Self::from_label(&label);

Copy link
Member Author

Choose a reason for hiding this comment

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

This is actually a present clippy error; I see this warning on current stable. It's not on this file though but crates/quantum_info/src/pauli_linblad_map/qubit_sparse_pauli.rs which was added in #14283 (which merged after I opened this).

@mtreinish
Copy link
Member Author

I've rebased the PR on main and updated it for the new places that violated the new rules. With the release of Rust 1.88 earlier today some of the rules I fixed in this PR are no longer "future" clippy rules as they've been stabilized and clippy will start flagging them in 1.88 now.

@mtreinish mtreinish requested a review from alexanderivrii June 26, 2025 23:20
@alexanderivrii
Copy link
Member

alexanderivrii commented Jun 29, 2025

Thanks Matthew. I am not sure what is the correct way to check for the new upcoming clippy warnings.

The following command cargo +nightly clippy -Z unstable-options --all-targets --all-features still flags a few problems (but significantly less than without this PR). In particular I see the following warnings:

  • warning: lifetime flowing from input to output with different syntax can be confusing (multiple times)
  • warning: variables can be used directly in the `format!` string (multiple times, including in target/mod.rs, slice.rs)
  • warning: using `clone` on type `Type` which implements the `Copy` trait` (in expr/expr.rs)

@mtreinish
Copy link
Member Author

I've just been running cargo +nightly clippy locally. I didn't enable the unstable rules or on all the targets (I probably should have done the latter). I can look at those other rules. I haven't updated my nightly version in a while so they might have added new rule, I'll take a look. I'm mostly interested in fixing the rules introduced in the 1.88 release from last week at this point because I tend to work with the latest stable locally.

A new nightly rule was added to ensure that lifetime syntaxes are
matched in function signatures. In many cases we were excluding the
elided anonymous lifetime parameter being returned in the source which
clippy asserts is potentially confusing. This commit adds the explicit
anonymous paramter to these places to fix the warning.
@mtreinish mtreinish force-pushed the once-and-future-clippy branch from a194ead to bf7fa04 Compare June 29, 2025 22:49
@mtreinish
Copy link
Member Author

I've updated the PR with fixes for the new nightly rules. I'm not seeing any failures anymore. I will say this is a rolling thing and there is a reason we don't want to run nightly clippy regularly. So I'm not super concerned if this fixes every future rule. I only did this PR because I knew there were some big rules coming in 1.88 (mainly the string formatting one) and I was trying to get this merged before 1.88 released to minimize the disruption for those of us that build from the latest stable locally. That obviously didn't work though since this took > 1 month and rust 1.88 is released now with that rule included.

Copy link
Member

@alexanderivrii alexanderivrii left a comment

Choose a reason for hiding this comment

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

Thanks for the additional fixes!

@alexanderivrii alexanderivrii added this pull request to the merge queue Jul 1, 2025
Merged via the queue into Qiskit:main with commit 9aee400 Jul 1, 2025
26 checks passed
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 changelog Rust This PR or issue is related to Rust code in the repository
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants