Skip to content

Commit 5d8b6ad

Browse files
committed
Clippy fixes
1 parent a002fef commit 5d8b6ad

File tree

8 files changed

+70
-56
lines changed

8 files changed

+70
-56
lines changed

ntex-grpc/src/client/request.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct RequestContextInner {
2323
}
2424

2525
impl RequestContext {
26-
/// Create new RequestContext instance
26+
/// Create new `RequestContext` instance
2727
fn new() -> Self {
2828
Self(Rc::new(RequestContextInner {
2929
err: None,
@@ -77,7 +77,7 @@ impl RequestContext {
7777
Err(e) => ctx.err = Some(log_error(e)),
7878
},
7979
Err(e) => ctx.err = Some(log_error(e)),
80-
};
80+
}
8181
}
8282
self
8383
}

ntex-grpc/src/client/transport.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ impl<T: MethodDef> Transport<T> for h2::client::Client {
4242
impl<T: MethodDef> Transport<T> for h2::client::SimpleClient {
4343
type Error = Error<ClientError>;
4444

45+
#[allow(clippy::too_many_lines)]
4546
async fn request(
4647
&self,
4748
val: &T::Input,
@@ -61,7 +62,7 @@ impl<T: MethodDef> Transport<T> for h2::client::SimpleClient {
6162
hdrs.insert(consts::GRPC_ENCODING, consts::IDENTITY);
6263
hdrs.insert(consts::GRPC_ACCEPT_ENCODING, consts::IDENTITY);
6364
for (key, val) in ctx.headers() {
64-
hdrs.insert(key.clone(), val.clone())
65+
hdrs.insert(key.clone(), val.clone());
6566
}
6667

6768
// send request
@@ -85,9 +86,7 @@ impl<T: MethodDef> Transport<T> for h2::client::SimpleClient {
8586

8687
async {
8788
loop {
88-
let msg = if let Some(msg) = rcv_stream.recv().await {
89-
msg
90-
} else {
89+
let Some(msg) = rcv_stream.recv().await else {
9190
return Err(Error::from(ClientError::UnexpectedEof(status, hdrs)));
9291
};
9392

@@ -122,10 +121,9 @@ impl<T: MethodDef> Transport<T> for h2::client::SimpleClient {
122121
pseudo.status,
123122
headers,
124123
)));
125-
} else {
126-
hdrs = headers;
127-
status = pseudo.status;
128124
}
125+
hdrs = headers;
126+
status = pseudo.status;
129127
continue;
130128
}
131129
h2::MessageKind::Data(data, _cap) => {
@@ -140,7 +138,7 @@ impl<T: MethodDef> Transport<T> for h2::client::SimpleClient {
140138
h2::StreamEof::Trailers(hdrs) => {
141139
// check grpc status
142140
match check_grpc_status(&hdrs) {
143-
Some(Ok(GrpcStatus::Ok)) => Ok(()),
141+
Some(Ok(GrpcStatus::Ok)) | None => Ok(()),
144142
Some(Ok(GrpcStatus::DeadlineExceeded)) => {
145143
return Err(Error::from(ClientError::DeadlineExceeded(
146144
hdrs,
@@ -154,14 +152,13 @@ impl<T: MethodDef> Transport<T> for h2::client::SimpleClient {
154152
Some(Err(())) => Err(Error::from(ClientError::Decode(
155153
DecodeError::new("Cannot parse grpc status"),
156154
))),
157-
None => Ok(()),
158155
}?;
159156
trailers = hdrs;
160157
}
161158
h2::StreamEof::Error(err) => {
162159
return Err(err.map(ClientError::Stream));
163160
}
164-
};
161+
}
165162
}
166163
h2::MessageKind::Disconnect(err) => {
167164
return Err(err.map(ClientError::Operation));
@@ -179,9 +176,7 @@ impl<T: MethodDef> Transport<T> for h2::client::SimpleClient {
179176
}
180177
let _compressed = data.get_u8();
181178
let len = data.get_u32();
182-
let mut block = if let Some(b) = data.split_to_checked(len as usize) {
183-
b
184-
} else {
179+
let Some(mut block) = data.split_to_checked(len as usize) else {
185180
return Err(Error::from(ClientError::UnexpectedEof(None, hdrs)));
186181
};
187182

ntex-grpc/src/encoding.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/// protobuf encoding utils
2-
/// cloned from https://github.com/hyperium/tonic/
2+
/// cloned from `<https://github.com/hyperium/tonic/>`
33
use std::{borrow::Cow, cmp::min, convert::TryFrom, fmt, rc::Rc};
44

55
use ntex_bytes::{Buf, BufMut, Bytes, BytesMut};
@@ -54,10 +54,9 @@ pub fn encode_varint(mut value: u64, buf: &mut BytesMut) {
5454
if value < 0x80 {
5555
buf.put_u8(value as u8);
5656
break;
57-
} else {
58-
buf.put_u8(((value & 0x7F) | 0x80) as u8);
59-
value >>= 7;
6057
}
58+
buf.put_u8(((value & 0x7F) | 0x80) as u8);
59+
value >>= 7;
6160
}
6261
}
6362

@@ -108,67 +107,67 @@ fn decode_varint_slice(bytes: &[u8]) -> Result<(u64, usize), DecodeError> {
108107
let mut part0: u32 = u32::from(b);
109108
if b < 0x80 {
110109
return Ok((u64::from(part0), 1));
111-
};
110+
}
112111
part0 -= 0x80;
113112
b = unsafe { *bytes.get_unchecked(1) };
114113
part0 += u32::from(b) << 7;
115114
if b < 0x80 {
116115
return Ok((u64::from(part0), 2));
117-
};
116+
}
118117
part0 -= 0x80 << 7;
119118
b = unsafe { *bytes.get_unchecked(2) };
120119
part0 += u32::from(b) << 14;
121120
if b < 0x80 {
122121
return Ok((u64::from(part0), 3));
123-
};
122+
}
124123
part0 -= 0x80 << 14;
125124
b = unsafe { *bytes.get_unchecked(3) };
126125
part0 += u32::from(b) << 21;
127126
if b < 0x80 {
128127
return Ok((u64::from(part0), 4));
129-
};
128+
}
130129
part0 -= 0x80 << 21;
131130
let value = u64::from(part0);
132131

133132
b = unsafe { *bytes.get_unchecked(4) };
134133
let mut part1: u32 = u32::from(b);
135134
if b < 0x80 {
136135
return Ok((value + (u64::from(part1) << 28), 5));
137-
};
136+
}
138137
part1 -= 0x80;
139138
b = unsafe { *bytes.get_unchecked(5) };
140139
part1 += u32::from(b) << 7;
141140
if b < 0x80 {
142141
return Ok((value + (u64::from(part1) << 28), 6));
143-
};
142+
}
144143
part1 -= 0x80 << 7;
145144
b = unsafe { *bytes.get_unchecked(6) };
146145
part1 += u32::from(b) << 14;
147146
if b < 0x80 {
148147
return Ok((value + (u64::from(part1) << 28), 7));
149-
};
148+
}
150149
part1 -= 0x80 << 14;
151150
b = unsafe { *bytes.get_unchecked(7) };
152151
part1 += u32::from(b) << 21;
153152
if b < 0x80 {
154153
return Ok((value + (u64::from(part1) << 28), 8));
155-
};
154+
}
156155
part1 -= 0x80 << 21;
157156
let value = value + ((u64::from(part1)) << 28);
158157

159158
b = unsafe { *bytes.get_unchecked(8) };
160159
let mut part2: u32 = u32::from(b);
161160
if b < 0x80 {
162161
return Ok((value + (u64::from(part2) << 56), 9));
163-
};
162+
}
164163
part2 -= 0x80;
165164
b = unsafe { *bytes.get_unchecked(9) };
166165
part2 += u32::from(b) << 7;
167166
// Check for u64::MAX overflow. See [`ConsumeVarint`][1] for details.
168167
// [1]: https://github.com/protocolbuffers/protobuf-go/blob/v1.27.1/encoding/protowire/wire.go#L358
169168
if b < 0x02 {
170169
return Ok((value + (u64::from(part2) << 56), 10));
171-
};
170+
}
172171

173172
// We have overrun the maximum size of a varint (10 bytes) or the final byte caused an overflow.
174173
// Assume the data is corrupt.
@@ -194,11 +193,11 @@ where
194193
if byte <= 0x7F {
195194
// Check for u64::MAX overflow. See [`ConsumeVarint`][1] for details.
196195
// [1]: https://github.com/protocolbuffers/protobuf-go/blob/v1.27.1/encoding/protowire/wire.go#L358
197-
if count == 9 && byte >= 0x02 {
198-
return Err(DecodeError::new("invalid varint"));
196+
return if count == 9 && byte >= 0x02 {
197+
Err(DecodeError::new("invalid varint"))
199198
} else {
200-
return Ok(value);
201-
}
199+
Ok(value)
200+
};
202201
}
203202
}
204203

@@ -312,6 +311,7 @@ impl DecodeError {
312311
///
313312
/// Meant to be used only by `Message` implementations.
314313
#[doc(hidden)]
314+
#[must_use]
315315
pub fn push(mut self, message: &'static str, field: &'static str) -> Self {
316316
let inner = if let Some(inner) = Rc::get_mut(&mut self.inner) {
317317
inner

ntex-grpc/src/google_types/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
#![allow(
2+
clippy::wildcard_imports,
3+
clippy::default_trait_access,
4+
clippy::cast_lossless,
5+
clippy::cast_sign_loss,
6+
clippy::cast_possible_wrap,
7+
clippy::missing_panics_doc,
8+
clippy::doc_markdown
9+
)]
110
mod duration;
211
mod duration_impl;
312
mod timestamp;

ntex-grpc/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
#![deny(clippy::pedantic)]
2+
#![allow(
3+
clippy::must_use_candidate,
4+
clippy::cast_possible_truncation,
5+
clippy::missing_errors_doc,
6+
clippy::missing_fields_in_debug
7+
)]
18
mod consts;
29
mod service;
310
mod status;

ntex-grpc/src/server/service.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ where
171171
type Response = ();
172172
type Error = h2::StreamError;
173173

174-
#[allow(clippy::await_holding_refcell_ref)]
174+
#[allow(clippy::await_holding_refcell_ref, clippy::too_many_lines)]
175175
async fn call(
176176
&self,
177177
msg: h2::Message,
@@ -231,7 +231,7 @@ where
231231
match data {
232232
h2::StreamEof::Data(chunk) => inflight.data.push(chunk),
233233
h2::StreamEof::Trailers(hdrs) => {
234-
for (name, val) in hdrs.iter() {
234+
for (name, val) in &hdrs {
235235
inflight.headers.insert(name.clone(), val.clone());
236236
}
237237
}
@@ -308,14 +308,14 @@ where
308308
trailers.insert(consts::GRPC_MESSAGE, err.message);
309309
stream.send_trailers(trailers);
310310
}
311-
Err(_) => {
311+
Err(()) => {
312312
log::debug!(
313313
"{}: Deadline exceeded failure during service call",
314314
self.cfg.tag()
315315
);
316316
send_error(&stream, GrpcStatus::DeadlineExceeded, ERR_DEADLINE);
317317
}
318-
};
318+
}
319319

320320
return Ok(());
321321
}
@@ -370,7 +370,7 @@ fn try_parse_grpc_timeout(val: &HeaderValue) -> Result<Millis, ()> {
370370
// Microseconds
371371
"u" => Millis(u32::try_from(timeout_value / 1000).unwrap_or(u32::MAX)),
372372
// Nanoseconds
373-
"n" => Millis(u32::try_from(timeout_value / 1000000).unwrap_or(u32::MAX)),
373+
"n" => Millis(u32::try_from(timeout_value / 1_000_000).unwrap_or(u32::MAX)),
374374
_ => return Err(()),
375375
};
376376

ntex-grpc/src/status.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,16 @@ gen_error_code! {
9494
impl From<Reason> for GrpcStatus {
9595
fn from(reason: Reason) -> GrpcStatus {
9696
match reason {
97-
Reason::NO_ERROR => GrpcStatus::Internal,
98-
Reason::PROTOCOL_ERROR => GrpcStatus::Internal,
99-
Reason::INTERNAL_ERROR => GrpcStatus::Internal,
100-
Reason::FLOW_CONTROL_ERROR => GrpcStatus::Internal,
101-
Reason::SETTINGS_TIMEOUT => GrpcStatus::Internal,
102-
Reason::FRAME_SIZE_ERROR => GrpcStatus::Internal,
97+
Reason::NO_ERROR
98+
| Reason::PROTOCOL_ERROR
99+
| Reason::INTERNAL_ERROR
100+
| Reason::FLOW_CONTROL_ERROR
101+
| Reason::SETTINGS_TIMEOUT
102+
| Reason::FRAME_SIZE_ERROR
103+
| Reason::COMPRESSION_ERROR
104+
| Reason::CONNECT_ERROR => GrpcStatus::Internal,
103105
Reason::REFUSED_STREAM => GrpcStatus::Unavailable,
104106
Reason::CANCEL => GrpcStatus::Cancelled,
105-
Reason::COMPRESSION_ERROR => GrpcStatus::Internal,
106-
Reason::CONNECT_ERROR => GrpcStatus::Internal,
107107
Reason::ENHANCE_YOUR_CALM => GrpcStatus::ResourceExhausted,
108108
Reason::INADEQUATE_SECURITY => GrpcStatus::PermissionDenied,
109109
_ => GrpcStatus::Unknown,

ntex-grpc/src/types.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
#![allow(
2+
clippy::cast_lossless,
3+
clippy::cast_sign_loss,
4+
clippy::cast_possible_wrap
5+
)]
16
use std::{collections::HashMap, convert::TryFrom, fmt, hash::BuildHasher, hash::Hash, mem};
27

38
use ntex_bytes::{Buf, BufMut, ByteString, Bytes, BytesMut};
@@ -147,7 +152,7 @@ impl<T: Message + PartialEq> NativeType for T {
147152
#[inline]
148153
/// Encode message to the buffer
149154
fn encode_value(&self, dst: &mut BytesMut) {
150-
self.write(dst)
155+
self.write(dst);
151156
}
152157

153158
/// Deserialize from the input
@@ -301,9 +306,7 @@ impl<T: NativeType> NativeType for Option<T> {
301306
#[inline]
302307
/// Protobuf field length
303308
fn encoded_len(&self, tag: u32) -> usize {
304-
self.as_ref()
305-
.map(|value| value.encoded_len(tag))
306-
.unwrap_or(0)
309+
self.as_ref().map_or(0, |value| value.encoded_len(tag))
307310
}
308311
}
309312

@@ -380,14 +383,14 @@ impl<T: NativeType> NativeType for Vec<T> {
380383
if T::TYPE == WireType::Varint {
381384
encoding::encode_key(tag, WireType::LengthDelimited, dst);
382385
encoding::encode_varint(
383-
self.iter().map(|v| v.value_len()).sum::<usize>() as u64,
386+
self.iter().map(NativeType::value_len).sum::<usize>() as u64,
384387
dst,
385388
);
386-
for item in self.iter() {
389+
for item in self {
387390
item.encode_value(dst);
388391
}
389392
} else {
390-
for item in self.iter() {
393+
for item in self {
391394
item.serialize(tag, DefaultValue::Unknown, dst);
392395
}
393396
}
@@ -401,8 +404,8 @@ impl<T: NativeType> NativeType for Vec<T> {
401404
/// Protobuf field length
402405
fn encoded_len(&self, tag: u32) -> usize {
403406
if T::TYPE == WireType::Varint {
404-
let len = self.iter().map(|value| value.value_len()).sum::<usize>();
405-
self.iter().map(|value| value.value_len()).sum::<usize>()
407+
let len = self.iter().map(NativeType::value_len).sum::<usize>();
408+
self.iter().map(NativeType::value_len).sum::<usize>()
406409
+ encoding::key_len(tag)
407410
+ encoding::encoded_len_varint(len as u64)
408411
} else {
@@ -468,7 +471,7 @@ impl<K: NativeType + Eq + Hash, V: NativeType, S: BuildHasher + Default> NativeT
468471
let key_default = K::default();
469472
let val_default = V::default();
470473

471-
for item in self.iter() {
474+
for item in self {
472475
let skip_key = item.0 == &key_default;
473476
let skip_val = item.1 == &val_default;
474477

0 commit comments

Comments
 (0)