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

chore: Remove some unwraps and replace others by expect #2294

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

larseggert
Copy link
Collaborator

@larseggert larseggert commented Dec 19, 2024

In functions that return Result or Option or where it is easy to make them do so w/o too many other code changes.

This lets us enable the clippy unwrap_used check, which is a nice reminder not to be casual with unwrap.

In functions that return `Result`.
Copy link

github-actions bot commented Dec 19, 2024

Failed Interop Tests

QUIC Interop Runner, client vs. server, differences relative to 4028ca4.

neqo-latest as client

neqo-latest as server

All results

Succeeded Interop Tests

QUIC Interop Runner, client vs. server

neqo-latest as client

neqo-latest as server

Unsupported Interop Tests

QUIC Interop Runner, client vs. server

neqo-latest as client

neqo-latest as server

Copy link

codecov bot commented Dec 19, 2024

Codecov Report

Attention: Patch coverage is 78.11060% with 95 lines in your changes missing coverage. Please review.

Project coverage is 93.13%. Comparing base (bb45c74) to head (c577129).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
.../features/extended_connect/webtransport_session.rs 28.57% 10 Missing and 5 partials ⚠️
neqo-transport/src/send_stream.rs 73.80% 7 Missing and 4 partials ⚠️
neqo-crypto/src/agent.rs 41.66% 3 Missing and 4 partials ⚠️
neqo-http3/src/connection.rs 36.36% 1 Missing and 6 partials ⚠️
neqo-transport/src/recovery/mod.rs 88.67% 4 Missing and 2 partials ⚠️
neqo-transport/src/tracking.rs 91.17% 2 Missing and 4 partials ⚠️
neqo-transport/src/path.rs 73.68% 2 Missing and 3 partials ⚠️
neqo-crypto/src/agentio.rs 66.66% 4 Missing ⚠️
neqo-transport/src/connection/mod.rs 80.95% 2 Missing and 2 partials ⚠️
neqo-qpack/src/decoder.rs 50.00% 2 Missing and 1 partial ⚠️
... and 19 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2294      +/-   ##
==========================================
- Coverage   93.33%   93.13%   -0.20%     
==========================================
  Files         114      114              
  Lines       36896    36931      +35     
  Branches    36896    36931      +35     
==========================================
- Hits        34438    34397      -41     
- Misses       1675     1693      +18     
- Partials      783      841      +58     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@larseggert larseggert changed the title chore: Remove some unwraps chore: Remove some unwraps and replace others by expect Dec 20, 2024
Copy link
Member

@martinthomson martinthomson left a comment

Choose a reason for hiding this comment

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

Lots of improvements here, but there are a few places where some of that could be improved.

(Non-blocking comment as I'm going on leave.)

Comment on lines 585 to 589
self.secrets.register(self.fd)?;
self.raw = Some(r);
Ok(())
} else if self.raw.unwrap() == r {
} else if self.raw.ok_or(Error::InternalError)? == r {
Ok(())
Copy link
Member

Choose a reason for hiding this comment

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

This probably needs to be restructured:

if let Some(raw) = self.raw {
  if raw == r {
    Ok(())
  } else {
    Err(Error::MixedHandshakeMethod)
  }
} else {
  self.secrets.register(self.fd)?;
  self.raw = Some(r);
  Ok(())
}

}

@@ -22,6 +22,7 @@ fn se_create() {
const PLAINTEXT: &[u8] = b"PLAINTEXT";
const AAD: &[u8] = b"AAD";

#[cfg(test)]
Copy link
Member

Choose a reason for hiding this comment

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

?? This is in tests/. Why would it need this?

Copy link
Collaborator Author

@larseggert larseggert Dec 27, 2024

Choose a reason for hiding this comment

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

I don't know, but it does. Otherwise clippy complains.

session_id: stream_info.session_id().unwrap(),
},
));
if let Some(session_id) = stream_info.session_id() {
Copy link
Member

Choose a reason for hiding this comment

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

Is this truly safe? It seems like this should always be called with stream info that has a session ID, but what do we do if it doesn't?

.unwrap()
.maybe_update_priority(priority)
let Some(stream) = self.control_stream_recv.http_stream() else {
return false;
Copy link
Member

Choose a reason for hiding this comment

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

Like the one above.

.unwrap()
.priority_update_sent();
let Some(stream) = self.control_stream_recv.http_stream() else {
return;
Copy link
Member

Choose a reason for hiding this comment

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

Same.

write_chunk(offset + mid as u64, right, builder),
write_chunk(offset, left, builder),
]
if let Some(cs) = self.get_mut(space) {
Copy link
Member

Choose a reason for hiding this comment

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

I think that this would be better with:

let Some(cs) = self.get_mut(space) else { return; }

Comment on lines +333 to +334
let Some(primary) = self.migration_target.take() else {
break;
Copy link
Member

Choose a reason for hiding this comment

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

I think that you can restructure this little piece of code to avoid the extra break.

let Some(primary) = self.migration_target.as_mut() else { break; }
if Rc::ptr_eq(target, primary) {
  let p = primary.take();
  mem::drop(self.select_primary(&p, now));
  return true;
}
break;

Once we're on 1.80 MSRV, we can use Option::take_if and simplify further.

if let Some(primary) = self.migration_target.take_if(|p| Rc::ptr_eq(target, p)) 
  mem::drop(self.select_primary(&primary, now));
  return true;
}
break;

neqo-transport/src/path.rs Outdated Show resolved Hide resolved
&mut lost,
);
self.stats.borrow_mut().lost += lost.len();
if let Some(sp) = self.spaces.get_mut(pn_space) {
Copy link
Member

Choose a reason for hiding this comment

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

This is another great candidate for let Some(foo) = expr else { return ...; };

@@ -457,7 +457,8 @@ impl TransportParameters {
let rbuf = random::<4>();
let mut other = Vec::with_capacity(versions.all().len() + 1);
let mut dec = Decoder::new(&rbuf);
let grease = dec.decode_uint::<u32>().unwrap() & 0xf0f0_f0f0 | 0x0a0a_0a0a;
let grease =
dec.decode_uint::<u32>().expect("dec has u32 left") & 0xf0f0_f0f0 | 0x0a0a_0a0a;
Copy link
Member

Choose a reason for hiding this comment

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

This is a good place to go with u32::from_ne_bytes(random::<4>), like above.

Co-authored-by: Martin Thomson <[email protected]>
Signed-off-by: Lars Eggert <[email protected]>
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.

2 participants