Skip to content
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

clippy: arithmetic-side-effects #2493

Merged
merged 4 commits into from
Aug 11, 2024

Conversation

yihau
Copy link
Member

@yihau yihau commented Aug 8, 2024

(part of #2487)

Problem

Screenshot 2024-08-08 at 21 54 33

Summary of Changes

replace add_assign with saturating_add

@yihau yihau marked this pull request as ready for review August 8, 2024 16:35
@yihau yihau requested review from CriesofCarrots and HaoranYi and removed request for CriesofCarrots August 8, 2024 16:35
Comment on lines 45 to 47
assert!(self.distributed_rewards.saturating_add(amount) <= self.total_rewards);

self.distributed_rewards.add_assign(amount);
self.distributed_rewards = self.distributed_rewards.saturating_add(amount);

Choose a reason for hiding this comment

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

The lint seems unnecessary here, but the change to saturating add is innocuous. I don't think we need to do the addition multiple times, though
How about this:

pub fn distribute(&mut self, amount: u64) {
    let new_distributed_rewards = self.distributed_rewards.saturating_add(amount);
    assert!(new_distributed_rewards <= self.total_rewards);
    self.distributed_rewards = new_distributed_rewards;
}

Copy link

Choose a reason for hiding this comment

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

I like it.

Copy link
Member Author

Choose a reason for hiding this comment

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

thank you for the insight! yeah, good catch! just fixed it! 34f81e7

CriesofCarrots
CriesofCarrots previously approved these changes Aug 9, 2024
@CriesofCarrots
Copy link

🤔 I'll look at that test failure.

@yihau
Copy link
Member Author

yihau commented Aug 9, 2024

ah... sorry for missing the update for the test. just fixed it!

CriesofCarrots
CriesofCarrots previously approved these changes Aug 9, 2024
Copy link

@CriesofCarrots CriesofCarrots left a comment

Choose a reason for hiding this comment

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

Oh nice, thanks!

@yihau yihau merged commit dcee6df into anza-xyz:master Aug 11, 2024
50 checks passed
@yihau yihau deleted the potential-arithmetic_side_effects branch August 11, 2024 09:14
ray-kast pushed a commit to abklabs/agave that referenced this pull request Nov 27, 2024
* possible arithmetic_side_effects

* feedback

* fix test expected message

* fmt
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.

3 participants