Skip to content

Replace arc.clone() with Arc::clone and reject the former in clippy (plus enforce clippy in tests) #3851

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 13, 2025

Conversation

TheBlueMatt
Copy link
Collaborator

`arc.clone()` leaves the code somewhat ambiguous to whether we're
doing an expensive deep-clone operation or if we're doing a
(relatively) cheap pointer-copy-and-atomic-increment operation.

More importantly, it leaves entirely unclear what the semantics of
the object we just created are - does updating it update the
original or leave it be?

`arc.clone()` leaves the code somewhat ambiguous to whether we're
doing an expensive deep-clone operation or if we're doing a
(relatively) cheap pointer-copy-and-atomic-increment operation.

More importantly, it leaves entirely unclear what the semantics of
the object we just created are - does updating it update the
original or leave it be?

Thus, here, we replace `arc.clone()` calls with `Arc::clone()` in
`lightning-net-tokio/src/lib.rs`
`arc.clone()` leaves the code somewhat ambiguous to whether we're
doing an expensive deep-clone operation or if we're doing a
(relatively) cheap pointer-copy-and-atomic-increment operation.

More importantly, it leaves entirely unclear what the semantics of
the object we just created are - does updating it update the
original or leave it be?

Thus, here, we replace `arc.clone()` calls with `Arc::clone()` in
`lightning-background-processor/src/lib.rs`
`arc.clone()` leaves the code somewhat ambiguous to whether we're
doing an expensive deep-clone operation or if we're doing a
(relatively) cheap pointer-copy-and-atomic-increment operation.

More importantly, it leaves entirely unclear what the semantics of
the object we just created are - does updating it update the
original or leave it be?

Thus, here we require `Arc::clone` via clippy enforcement in CI
In a coming commit we'll enable `clippy` linting in our test code.
However, there's some things we do in tests (which we might
reasonably do in non-test code in the future) that clippy is
unhappy with, which we explicitly allow here.
In a coming commit we'll enable `clippy` linting in our test code.
Here we prepare for this by addressing the lints we'll enforce in
test codein the `lightning` crate.
@ldk-reviews-bot
Copy link

ldk-reviews-bot commented Jun 12, 2025

👋 Thanks for assigning @tnull as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

@TheBlueMatt TheBlueMatt changed the title Replace arc.clone() with Arc::clone and reject the former in clippy Replace arc.clone() with Arc::clone and reject the former in clippy (plus enforce clippy in tests) Jun 12, 2025
In a coming commit we'll enable `clippy` linting in our test code.
Here we prepare for this by addressing the lints we'll enforce in
test code in remaining crates.
`arc.clone()` leaves the code somewhat ambiguous to whether we're
doing an expensive deep-clone operation or if we're doing a
(relatively) cheap pointer-copy-and-atomic-increment operation.

More importantly, it leaves entirely unclear what the semantics of
the object we just created are - does updating it update the
original or leave it be?

Thus, here, we replace `arc.clone()` calls with `Arc::clone()` in
`lightning-background-processor` tests.
`arc.clone()` leaves the code somewhat ambiguous to whether we're
doing an expensive deep-clone operation or if we're doing a
(relatively) cheap pointer-copy-and-atomic-increment operation.

More importantly, it leaves entirely unclear what the semantics of
the object we just created are - does updating it update the
original or leave it be?

Thus, here, we replace `arc.clone()` calls with `Arc::clone()` in
remaining tests.
@TheBlueMatt TheBlueMatt force-pushed the 2025-06-no-arc-clones branch from 6c06771 to 211871b Compare June 12, 2025 15:48
@ldk-reviews-bot ldk-reviews-bot requested a review from jkczyz June 12, 2025 15:55
tnull
tnull previously approved these changes Jun 12, 2025
wpaulino
wpaulino previously approved these changes Jun 12, 2025
@TheBlueMatt TheBlueMatt dismissed stale reviews from wpaulino and tnull via 0dcea98 June 12, 2025 23:07
@TheBlueMatt TheBlueMatt force-pushed the 2025-06-no-arc-clones branch from 211871b to 0dcea98 Compare June 12, 2025 23:07
@TheBlueMatt
Copy link
Collaborator Author

Disabled the broken shellcheck lint:

$ git diff-tree -U1 211871bdcb 0dcea98439
diff --git a/ci/check-lint.sh b/ci/check-lint.sh
index 33aef91335..cd013dfd17 100755
--- a/ci/check-lint.sh
+++ b/ci/check-lint.sh
@@ -5,2 +5,3 @@ set -x
 CLIPPY() {
+	# shellcheck disable=SC2086
 	RUSTFLAGS='-D warnings' cargo clippy $1 -- $2 \

@TheBlueMatt TheBlueMatt requested review from wpaulino and tnull and removed request for jkczyz June 12, 2025 23:07
wpaulino
wpaulino previously approved these changes Jun 12, 2025
tnull
tnull previously approved these changes Jun 13, 2025
Copy link
Contributor

@tnull tnull left a comment

Choose a reason for hiding this comment

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

LGTM

-A clippy::io_other_error `# to be removed once we hit MSRV 1.74`

CLIPPY() {
# shellcheck disable=SC2086
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if we shouldn't just fix it instead of disabling the check, but not going to block this because of it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The lack of quotes is deliberate, we need to pass multiple things to clippy via one argument.

Copy link
Contributor

@tnull tnull left a comment

Choose a reason for hiding this comment

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

Ah, wait, rustfmt is unhappy.

Clippy points out that "format specifiers have no effect on
`format_args!()`", i.e. that our attempt to fix the log context
width to 55 chars doesn't do anything because its applied to the
result of `format_args`ing the context.

This appears to have broken when we optimized out a previous
`format`, which we revert here.
Now that we've fixed many of the lints clippy cares about for our
test code, switch to enforcing the lints in `--tests` as well.

We disable a few lints that require substantial number of changes
to our existing tests, sadly, though they'd probably be good
changes to make at some point.
@TheBlueMatt
Copy link
Collaborator Author

Fixed.

@TheBlueMatt TheBlueMatt requested a review from tnull June 13, 2025 12:11
@TheBlueMatt
Copy link
Collaborator Author

Can probably be landed with one ACK, the diff is just:

$ git diff-tree -U1 0dcea98439 07590e4d5a
diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs
index b1864dbe77..b26794a30c 100644
--- a/lightning/src/util/test_utils.rs
+++ b/lightning/src/util/test_utils.rs
@@ -1437,6 +1437,4 @@ impl Logger for TestLogger {
 	fn log(&self, record: Record) {
-		let context = format!(
-			"{} {} [{}:{}]",
-			self.id, record.level, record.module_path, record.line
-		);
+		let context =
+			format!("{} {} [{}:{}]", self.id, record.level, record.module_path, record.line);
 		let s = format!("{:<55} {}", context, record.args);

@wpaulino wpaulino merged commit ed16b5f into lightningdevkit:main Jun 13, 2025
27 of 28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants