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

Fix all clippy warnings #652

Merged
merged 3 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 1 addition & 4 deletions examples/akamai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ pub async fn main() -> Result<(), Box<dyn Error>> {
{
let (_, session) = tls.get_ref();
let negotiated_protocol = session.alpn_protocol();
assert_eq!(
Some(ALPN_H2.as_bytes()),
negotiated_protocol.as_ref().map(|x| &**x)
);
assert_eq!(Some(ALPN_H2.as_bytes()), negotiated_protocol);
}

println!("Starting client handshake");
Expand Down
10 changes: 5 additions & 5 deletions src/codec/framed_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ fn decode_frame(

if partial_inout.is_some() && head.kind() != Kind::Continuation {
proto_err!(conn: "expected CONTINUATION, got {:?}", head.kind());
return Err(Error::library_go_away(Reason::PROTOCOL_ERROR).into());
return Err(Error::library_go_away(Reason::PROTOCOL_ERROR));
}

let kind = head.kind();
Expand Down Expand Up @@ -231,7 +231,7 @@ fn decode_frame(
if head.stream_id() == 0 {
// Invalid stream identifier
proto_err!(conn: "invalid stream ID 0");
return Err(Error::library_go_away(Reason::PROTOCOL_ERROR).into());
return Err(Error::library_go_away(Reason::PROTOCOL_ERROR));
}

match frame::Priority::load(head, &bytes[frame::HEADER_LEN..]) {
Expand All @@ -257,14 +257,14 @@ fn decode_frame(
Some(partial) => partial,
None => {
proto_err!(conn: "received unexpected CONTINUATION frame");
return Err(Error::library_go_away(Reason::PROTOCOL_ERROR).into());
return Err(Error::library_go_away(Reason::PROTOCOL_ERROR));
}
};

// The stream identifiers must match
if partial.frame.stream_id() != head.stream_id() {
proto_err!(conn: "CONTINUATION frame stream ID does not match previous frame stream ID");
return Err(Error::library_go_away(Reason::PROTOCOL_ERROR).into());
return Err(Error::library_go_away(Reason::PROTOCOL_ERROR));
}

// Extend the buf
Expand All @@ -287,7 +287,7 @@ fn decode_frame(
// the attacker to go away.
if partial.buf.len() + bytes.len() > max_header_list_size {
proto_err!(conn: "CONTINUATION frame header block size over ignorable limit");
return Err(Error::library_go_away(Reason::COMPRESSION_ERROR).into());
return Err(Error::library_go_away(Reason::COMPRESSION_ERROR));
}
}
partial.buf.extend_from_slice(&bytes[frame::HEADER_LEN..]);
Expand Down
8 changes: 1 addition & 7 deletions src/frame/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct Data<T = Bytes> {
pad_len: Option<u8>,
}

#[derive(Copy, Clone, Eq, PartialEq)]
#[derive(Copy, Clone, Default, Eq, PartialEq)]
struct DataFlags(u8);

const END_STREAM: u8 = 0x1;
Expand Down Expand Up @@ -211,12 +211,6 @@ impl DataFlags {
}
}

impl Default for DataFlags {
fn default() -> Self {
DataFlags(0)
}
}

impl From<DataFlags> for u8 {
fn from(src: DataFlags) -> u8 {
src.0
Expand Down
13 changes: 8 additions & 5 deletions src/frame/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,17 +309,20 @@ impl fmt::Debug for Headers {

// ===== util =====

pub fn parse_u64(src: &[u8]) -> Result<u64, ()> {
#[derive(Debug, PartialEq, Eq)]
pub struct ParseU64Error;

pub fn parse_u64(src: &[u8]) -> Result<u64, ParseU64Error> {
gtsiam marked this conversation as resolved.
Show resolved Hide resolved
if src.len() > 19 {
// At danger for overflow...
return Err(());
return Err(ParseU64Error);
}

let mut ret = 0;

for &d in src {
if d < b'0' || d > b'9' {
return Err(());
return Err(ParseU64Error);
}

ret *= 10;
Expand All @@ -333,7 +336,7 @@ pub fn parse_u64(src: &[u8]) -> Result<u64, ()> {

#[derive(Debug)]
pub enum PushPromiseHeaderError {
InvalidContentLength(Result<u64, ()>),
InvalidContentLength(Result<u64, ParseU64Error>),
NotSafeAndCacheable,
}

Expand Down Expand Up @@ -381,7 +384,7 @@ impl PushPromise {
fn safe_and_cacheable(method: &Method) -> bool {
// Cacheable: https://httpwg.org/specs/rfc7231.html#cacheable.methods
// Safe: https://httpwg.org/specs/rfc7231.html#safe.methods
return method == Method::GET || method == Method::HEAD;
method == Method::GET || method == Method::HEAD
}

pub fn fields(&self) -> &HeaderMap {
Expand Down
6 changes: 3 additions & 3 deletions src/frame/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ impl Settings {
}
}
Some(MaxFrameSize(val)) => {
if val < DEFAULT_MAX_FRAME_SIZE || val > MAX_MAX_FRAME_SIZE {
return Err(Error::InvalidSettingValue);
} else {
if DEFAULT_MAX_FRAME_SIZE <= val && val <= MAX_MAX_FRAME_SIZE {
settings.max_frame_size = Some(val);
} else {
return Err(Error::InvalidSettingValue);
}
}
Some(MaxHeaderListSize(val)) => {
Expand Down
29 changes: 13 additions & 16 deletions src/hpack/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -852,26 +852,24 @@ mod test {
fn test_decode_empty() {
let mut de = Decoder::new(0);
let mut buf = BytesMut::new();
let empty = de.decode(&mut Cursor::new(&mut buf), |_| {}).unwrap();
assert_eq!(empty, ());
let _: () = de.decode(&mut Cursor::new(&mut buf), |_| {}).unwrap();
}

#[test]
fn test_decode_indexed_larger_than_table() {
let mut de = Decoder::new(0);

let mut buf = BytesMut::new();
buf.extend(&[0b01000000, 0x80 | 2]);
buf.extend([0b01000000, 0x80 | 2]);
buf.extend(huff_encode(b"foo"));
buf.extend(&[0x80 | 3]);
buf.extend([0x80 | 3]);
buf.extend(huff_encode(b"bar"));

let mut res = vec![];
let _ = de
.decode(&mut Cursor::new(&mut buf), |h| {
res.push(h);
})
.unwrap();
de.decode(&mut Cursor::new(&mut buf), |h| {
res.push(h);
})
.unwrap();

assert_eq!(res.len(), 1);
assert_eq!(de.table.size(), 0);
Expand Down Expand Up @@ -900,10 +898,10 @@ mod test {
let value = huff_encode(b"bar");
let mut buf = BytesMut::new();
// header name is non_huff encoded
buf.extend(&[0b01000000, 0x00 | 3]);
buf.extend([0b01000000, 3]);
buf.extend(b"foo");
// header value is partial
buf.extend(&[0x80 | 3]);
buf.extend([0x80 | 3]);
buf.extend(&value[0..1]);

let mut res = vec![];
Expand All @@ -917,11 +915,10 @@ mod test {

// extend buf with the remaining header value
buf.extend(&value[1..]);
let _ = de
.decode(&mut Cursor::new(&mut buf), |h| {
res.push(h);
})
.unwrap();
de.decode(&mut Cursor::new(&mut buf), |h| {
res.push(h);
})
.unwrap();

assert_eq!(res.len(), 1);
assert_eq!(de.table.size(), 0);
Expand Down
8 changes: 4 additions & 4 deletions src/hpack/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ impl Encoder {
encode_int(idx, 7, 0x80, dst);
}
Index::Name(idx, _) => {
let header = self.table.resolve(&index);
let header = self.table.resolve(index);

encode_not_indexed(idx, header.value_slice(), header.is_sensitive(), dst);
}
Index::Inserted(_) => {
let header = self.table.resolve(&index);
let header = self.table.resolve(index);

assert!(!header.is_sensitive());

Expand All @@ -133,15 +133,15 @@ impl Encoder {
encode_str(header.value_slice(), dst);
}
Index::InsertedValue(idx, _) => {
let header = self.table.resolve(&index);
let header = self.table.resolve(index);

assert!(!header.is_sensitive());

encode_int(idx, 6, 0b0100_0000, dst);
encode_str(header.value_slice(), dst);
}
Index::NotIndexed(_) => {
let header = self.table.resolve(&index);
let header = self.table.resolve(index);

encode_not_indexed2(
header.name().as_slice(),
Expand Down
26 changes: 13 additions & 13 deletions src/hpack/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,18 +190,18 @@ impl Header {
use http::header;

match *self {
Header::Field { ref name, .. } => match *name {
Header::Field { ref name, .. } => matches!(
*name,
header::AGE
| header::AUTHORIZATION
| header::CONTENT_LENGTH
| header::ETAG
| header::IF_MODIFIED_SINCE
| header::IF_NONE_MATCH
| header::LOCATION
| header::COOKIE
| header::SET_COOKIE => true,
_ => false,
},
| header::AUTHORIZATION
| header::CONTENT_LENGTH
| header::ETAG
| header::IF_MODIFIED_SINCE
| header::IF_NONE_MATCH
| header::LOCATION
| header::COOKIE
| header::SET_COOKIE
),
Header::Path(..) => true,
_ => false,
}
Expand Down Expand Up @@ -231,10 +231,10 @@ impl<'a> Name<'a> {
match self {
Name::Field(name) => Ok(Header::Field {
name: name.clone(),
value: HeaderValue::from_bytes(&*value)?,
value: HeaderValue::from_bytes(&value)?,
}),
Name::Authority => Ok(Header::Authority(BytesStr::try_from(value)?)),
Name::Method => Ok(Header::Method(Method::from_bytes(&*value)?)),
Name::Method => Ok(Header::Method(Method::from_bytes(&value)?)),
Name::Scheme => Ok(Header::Scheme(BytesStr::try_from(value)?)),
Name::Path => Ok(Header::Path(BytesStr::try_from(value)?)),
Name::Protocol => Ok(Header::Protocol(Protocol::try_from(value)?)),
Expand Down
9 changes: 4 additions & 5 deletions src/hpack/huffman/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ mod test {
#[test]
fn decode_single_byte() {
assert_eq!("o", decode(&[0b00111111]).unwrap());
assert_eq!("0", decode(&[0x0 + 7]).unwrap());
assert_eq!("0", decode(&[7]).unwrap());
assert_eq!("A", decode(&[(0x21 << 2) + 3]).unwrap());
}

Expand All @@ -138,7 +138,7 @@ mod test {

dst.clear();
encode(b"0", &mut dst);
assert_eq!(&dst[..], &[0x0 + 7]);
assert_eq!(&dst[..], &[7]);

dst.clear();
encode(b"A", &mut dst);
Expand All @@ -147,7 +147,7 @@ mod test {

#[test]
fn encode_decode_str() {
const DATA: &'static [&'static str] = &[
const DATA: &[&str] = &[
"hello world",
":method",
":scheme",
Expand Down Expand Up @@ -184,8 +184,7 @@ mod test {

#[test]
fn encode_decode_u8() {
const DATA: &'static [&'static [u8]] =
&[b"\0", b"\0\0\0", b"\0\x01\x02\x03\x04\x05", b"\xFF\xF8"];
const DATA: &[&[u8]] = &[b"\0", b"\0\0\0", b"\0\x01\x02\x03\x04\x05", b"\xFF\xF8"];

for s in DATA {
let mut dst = BytesMut::with_capacity(s.len());
Expand Down
2 changes: 1 addition & 1 deletion src/hpack/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ impl Table {

// Find the associated position
probe_loop!(probe < self.indices.len(), {
debug_assert!(!self.indices[probe].is_none());
debug_assert!(self.indices[probe].is_some());

let mut pos = self.indices[probe].unwrap();

Expand Down
10 changes: 5 additions & 5 deletions src/hpack/test/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ fn test_story(story: Value) {

Case {
seqno: case.get("seqno").unwrap().as_u64().unwrap(),
wire: wire,
expect: expect,
wire,
expect,
header_table_size: size,
}
})
Expand Down Expand Up @@ -142,10 +142,10 @@ fn key_str(e: &Header) -> &str {
fn value_str(e: &Header) -> &str {
match *e {
Header::Field { ref value, .. } => value.to_str().unwrap(),
Header::Authority(ref v) => &**v,
Header::Authority(ref v) => v,
Header::Method(ref m) => m.as_str(),
Header::Scheme(ref v) => &**v,
Header::Path(ref v) => &**v,
Header::Scheme(ref v) => v,
Header::Path(ref v) => v,
Header::Protocol(ref v) => v.as_str(),
Header::Status(ref v) => v.as_str(),
}
Expand Down
2 changes: 1 addition & 1 deletion src/hpack/test/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl FuzzHpack {
let high = rng.gen_range(128..MAX_CHUNK * 2);
let low = rng.gen_range(0..high);

frame.resizes.extend(&[low, high]);
frame.resizes.extend([low, high]);
}
1..=3 => {
frame.resizes.push(rng.gen_range(128..MAX_CHUNK * 2));
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
#![doc(html_root_url = "https://docs.rs/h2/0.3.15")]
#![deny(missing_debug_implementations, missing_docs)]
#![cfg_attr(test, deny(warnings))]
#![allow(clippy::type_complexity, clippy::manual_range_contains)]

macro_rules! proto_err {
(conn: $($msg:tt)+) => {
Expand Down
2 changes: 1 addition & 1 deletion src/proto/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ where
});

match (ours, theirs) {
(Reason::NO_ERROR, Reason::NO_ERROR) => return Ok(()),
(Reason::NO_ERROR, Reason::NO_ERROR) => Ok(()),
(ours, Reason::NO_ERROR) => Err(Error::GoAway(Bytes::new(), ours, initiator)),
// If both sides reported an error, give their
// error back to th user. We assume our error
Expand Down
4 changes: 2 additions & 2 deletions src/proto/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub enum Error {
Io(io::ErrorKind, Option<String>),
}

#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Initiator {
User,
Library,
Expand Down Expand Up @@ -70,7 +70,7 @@ impl fmt::Display for Error {

impl From<io::ErrorKind> for Error {
fn from(src: io::ErrorKind) -> Self {
Error::Io(src.into(), None)
Error::Io(src, None)
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/proto/ping_pong.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,7 @@ impl PingPong {

impl ReceivedPing {
pub(crate) fn is_shutdown(&self) -> bool {
match *self {
ReceivedPing::Shutdown => true,
_ => false,
}
matches!(*self, Self::Shutdown)
}
}

Expand Down