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

More nightly clippy fixes (2021-09-29) #691

Merged
merged 1 commit into from
Oct 1, 2021
Merged

Conversation

sharnoff
Copy link
Member

Most of the changes are for the new if-then-panic lint added in rust-lang/rust-clippy#7669. The error messages look like:

error: only a `panic!` in `if`-then statement
   --> zenith_utils/src/lsn.rs:195:9
    |
195 | /         if prev.checked_add(val).is_none() {
196 | |             panic!("AtomicLsn overflow");
197 | |         }
    | |_________^ help: try: `assert!(!prev.checked_add(val).is_none(), "AtomicLsn overflow");`
    |
    = note: `-D clippy::if-then-panic` implied by `-D warnings`
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#if_then_panic

At first I thought it generally made things less readable, but it actually surprised me - being able to see the assert! at the start of the line makes it abundantly clear what the purpose is.

Most of the changes are for the new if-then-panic lint added in
rust-lang/rust-clippy#7669.

Error messages look like:

  error: only a `panic!` in `if`-then statement
     --> zenith_utils/src/lsn.rs:195:9
      |
  195 | /         if prev.checked_add(val).is_none() {
  196 | |             panic!("AtomicLsn overflow");
  197 | |         }
      | |_________^ help: try: `assert!(!prev.checked_add(val).is_none(), "AtomicLsn overflow");`
      |
      = note: `-D clippy::if-then-panic` implied by `-D warnings`
      = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#if_then_panic
if expected.is_empty() {
panic!("search with {} returned {}, expected None", key, v);
}
assert!(!expected.is_empty(), "search with {} returned {}, expected None", key, v);
Copy link
Contributor

Choose a reason for hiding this comment

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

The !expected.is_empty() check in conjunction with expected None is quite confusing, but I guess I might not know something 🙂

Copy link
Member Author

Choose a reason for hiding this comment

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

It took me a moment to figure out as well. From a higher level, what the function is doing is really just assert!(result.is_none() == expected.is_empty()) -- it should have returned None iff we weren't expecting anything. This is just the case if we already know the result is Some(_).

https://github.com/zenithdb/zenith/blob/d92412554fe81f22c5d3f52173de3a2e82d9f1d1/pageserver/src/layered_repository/interval_tree.rs#L292-L295

@sharnoff sharnoff merged commit 84f7dcd into main Oct 1, 2021
@sharnoff sharnoff deleted the clippy-if-then-panic branch October 1, 2021 22:45
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.

None yet

2 participants