Skip to content

Commit

Permalink
clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
Ggiggle committed Jan 25, 2024
1 parent 9f552f2 commit c5015cc
Show file tree
Hide file tree
Showing 17 changed files with 64 additions and 131 deletions.
2 changes: 1 addition & 1 deletion pilota-build/src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ where
ws.write_crates()
}

pub fn write_items<'a>(&self, stream: &mut String, items: impl Iterator<Item = CodegenItem>)
pub fn write_items(&self, stream: &mut String, items: impl Iterator<Item = CodegenItem>)
where
B: Send,
{
Expand Down
6 changes: 1 addition & 5 deletions pilota-build/src/codegen/pkg_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ fn from_pkgs(base_path: &[FastStr], pkgs: &[&[FastStr]]) -> Arc<[PkgNode]> {
.into_group_map_by(|p| p.first().unwrap());

Arc::from_iter(groups.into_iter().map(|(k, v)| {
let path = base_path
.iter()
.chain(Some(k).into_iter())
.cloned()
.collect::<Vec<_>>();
let path = base_path.iter().chain(Some(k)).cloned().collect::<Vec<_>>();

let pkgs = v
.into_iter()
Expand Down
9 changes: 4 additions & 5 deletions pilota-build/src/codegen/thrift/decode_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,11 @@ impl DecodeHelper {
pub fn codegen_field_begin_len(&self, keep: bool) -> FastStr {
if self.is_async {
Default::default()
} else if keep {
"__pilota_offset += protocol.field_begin_len(field_ident.field_type, field_ident.id);"
.into()
} else {
if keep {
"__pilota_offset += protocol.field_begin_len(field_ident.field_type, field_ident.id);".into()
} else {
"protocol.field_begin_len(field_ident.field_type, field_ident.id);".into()
}
"protocol.field_begin_len(field_ident.field_type, field_ident.id);".into()
}
}
}
13 changes: 5 additions & 8 deletions pilota-build/src/codegen/thrift/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ impl ThriftBackend {
})
}

fn codegen_entry_enum(&self, _def_id: DefId, _stream: &mut String, _e: &rir::Enum) {
fn codegen_entry_enum(&self, _def_id: DefId, _stream: &mut str, _e: &rir::Enum) {
// TODO
}

Expand Down Expand Up @@ -713,13 +713,10 @@ impl CodegenBackend for ThriftBackend {
if e.variants.first().filter(|v| variant_is_void(v)).is_some() {
format!("Ok({name}::Ok(()))").into()
} else {
format!(
r#"Err(::pilota::thrift::DecodeError::new(
::pilota::thrift::DecodeErrorKind::InvalidData,
"received empty union from remote Message")
)"#
)
.into()
r#"Err(::pilota::thrift::DecodeError::new(
::pilota::thrift::DecodeErrorKind::InvalidData,
"received empty union from remote Message")
)"#.into()
};

format! {
Expand Down
4 changes: 2 additions & 2 deletions pilota-build/src/codegen/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub fn merge_tomls(a: &mut toml::Value, b: toml::Value) {
(toml::Value::String(a), toml::Value::String(b)) => *a = b,
(toml::Value::Array(a), toml::Value::Array(b)) => {
a.extend(b);
a.sort_by(|a, b| a.to_string().cmp(&b.to_string()));
a.sort_by_key(|a| a.to_string());
a.dedup_by(|a, b| a.to_string() == b.to_string());
}
(toml::Value::Table(a), toml::Value::Table(b)) => b.into_iter().for_each(|(k, v)| {
Expand All @@ -16,6 +16,6 @@ pub fn merge_tomls(a: &mut toml::Value, b: toml::Value) {
a.insert(k, v);
}
}),
pair @ _ => panic!("can not merge {pair:?}"),
pair => panic!("can not merge {pair:?}"),
}
}
2 changes: 1 addition & 1 deletion pilota-build/src/codegen/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ where
.as_table()
.unwrap()
.keys()
.map(|s| FastStr::new(s))
.map(FastStr::new)
.collect_vec();

std::fs::write(
Expand Down
28 changes: 11 additions & 17 deletions pilota-build/src/middle/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ impl ContextBuilder {
rir::Item::Message(m) => m.fields.iter().for_each(|f| {
PathCollector { cx, set }.visit(&f.ty);
if let Some(Literal::Path(p)) = &f.default {
PathCollector { cx, set }.visit_path(&p);
PathCollector { cx, set }.visit_path(p);
}
}),
rir::Item::Enum(e) => e
Expand Down Expand Up @@ -395,7 +395,7 @@ impl ContextBuilder {
return;
}
let files = cx.db.files();
let file = files.get(&file_id).unwrap();
let file = files.get(file_id).unwrap();
file.uses.iter().for_each(|f| keep_files(cx, f, file_ids));
cx.keep_unknown_fields.extend(
file.items
Expand All @@ -405,17 +405,12 @@ impl ContextBuilder {
kind: rir::NodeKind::Item(_),
tags,
..
}) => {
if let Some(crate::tags::KeepUnknownFields(false)) =
cx.db.tags_map().get(&tags).and_then(|tags| {
tags.get::<crate::tags::KeepUnknownFields>()
})
{
false
} else {
true
}
}
}) => !matches!(
cx.db.tags_map().get(&tags).and_then(|tags| {
tags.get::<crate::tags::KeepUnknownFields>()
}),
Some(crate::tags::KeepUnknownFields(false))
),
_ => true,
})
.cloned(),
Expand Down Expand Up @@ -483,7 +478,7 @@ impl Context {
where
F: FnOnce(&mut Adjust) -> T,
{
let adjust = &mut *self.adjusts.entry(def_id).or_insert_with(Default::default);
let adjust = &mut *self.adjusts.entry(def_id).or_default();
f(adjust)
}

Expand Down Expand Up @@ -949,16 +944,15 @@ impl Context {
.unwrap();
self.config(crate_id)
.get("crate_name")
.map(|s| s.as_str().map(|s| FastStr::new(s)))
.flatten()
.and_then(|s| s.as_str().map(FastStr::new))
.unwrap_or_else(|| {
service
.path
.file_stem()
.unwrap()
.to_str()
.unwrap()
.replace(".", "_")
.replace('.', "_")
.into()
})
}
Expand Down
2 changes: 1 addition & 1 deletion pilota-build/src/middle/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl PathResolver for DefaultPathResolver {

let _length = path.len();

for (_idx, k) in path.into_iter().enumerate() {
for k in path.into_iter() {
segs.push(match k {
Kind::Super => "super".into(),
Kind::Ident(ident) => ident.to_string(),
Expand Down
20 changes: 3 additions & 17 deletions pilota-build/src/parser/protobuf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ impl Lower {
type_: Option<protobuf::EnumOrUnknown<protobuf::descriptor::field_descriptor_proto::Type>>,
type_name: Option<&str>,
nested_messages: &AHashMap<FastStr, &DescriptorProto>,
message_name: Option<&str>,
) -> ir::Ty {
if let Some(name) = type_name {
if let Some(msg) = nested_messages.get(name) {
Expand All @@ -83,13 +82,11 @@ impl Lower {
key.type_,
key.type_name.as_deref(),
nested_messages,
message_name,
)),
Arc::from(self.lower_ty(
value.type_,
value.type_name.as_deref(),
nested_messages,
message_name,
)),
),
tags: Default::default(),
Expand Down Expand Up @@ -239,7 +236,6 @@ impl Lower {
f.type_,
f.type_name.as_deref(),
&nested_messages,
Some(message.name()),
)],
tags: Default::default(),
})
Expand Down Expand Up @@ -294,12 +290,8 @@ impl Lower {
fields: fields
.iter()
.map(|(idx, f)| {
let mut ty = self.lower_ty(
f.type_,
f.type_name.as_deref(),
&nested_messages,
Some(message.name()),
);
let mut ty =
self.lower_ty(f.type_, f.type_name.as_deref(), &nested_messages);

let is_map = matches!(ty.kind, TyKind::Map(_, _));
let repeated = !is_map && matches!(f.label(), Label::LABEL_REPEATED);
Expand Down Expand Up @@ -401,17 +393,11 @@ impl Lower {
None,
m.input_type.as_deref(),
&Default::default(),
Some(service.name()),
),
tags: Arc::new(Tags::default()),
}],
oneway: false,
ret: self.lower_ty(
None,
m.output_type.as_deref(),
&Default::default(),
Some(service.name()),
),
ret: self.lower_ty(None, m.output_type.as_deref(), &Default::default()),
exceptions: None,
}
})
Expand Down
6 changes: 2 additions & 4 deletions pilota-build/src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,8 @@ pub fn walk_codegen_uint<P: Plugin + ?Sized>(p: &mut P, cx: &Context, items: &[D
items.iter().for_each(|def_id| {
CUR_ITEM.set(def_id, || {
let node = cx.node(*def_id).unwrap();

match &node.kind {
NodeKind::Item(item) => p.on_item(cx, *def_id, item.clone()),
_ => {}
if let NodeKind::Item(item) = &node.kind {
p.on_item(cx, *def_id, item.clone())
}
});
});
Expand Down
21 changes: 8 additions & 13 deletions pilota-build/src/plugin/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ impl Plugin for _WorkspacePlugin {
v.iter().for_each(|(def_id, _)| {
CUR_ITEM.set(def_id, || {
let node = cx.node(*def_id).unwrap();

match &node.kind {
NodeKind::Item(item) => self.on_item(cx, *def_id, item.clone()),
_ => {}
if let NodeKind::Item(item) = &node.kind {
self.on_item(cx, *def_id, item.clone());
}
});
})
Expand All @@ -31,15 +29,12 @@ impl Plugin for _WorkspacePlugin {
def_id: crate::DefId,
item: std::sync::Arc<crate::rir::Item>,
) {
match &*item {
Item::Service(s) => {
if let Some(loc) = cx.location_map.get(&def_id) {
if let Some(mut gen) = cx.plugin_gen.get_mut(loc) {
gen.push_str(&format!("pub struct {};", s.name.sym));
}
};
}
_ => {}
if let Item::Service(s) = &*item {
if let Some(loc) = cx.location_map.get(&def_id) {
if let Some(mut gen) = cx.plugin_gen.get_mut(loc) {
gen.push_str(&format!("pub struct {};", s.name.sym));
}
};
}
}
}
13 changes: 6 additions & 7 deletions pilota-build/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,10 @@ impl ir::visit::Visitor for CollectDef<'_> {
ir::ItemKind::Use(_) => None,
} {
let prev_parent = self.parent.replace(ModuleId::Node(did));
match &item.kind {
ir::ItemKind::Enum(e) => e.variants.iter().for_each(|e| {
if let ir::ItemKind::Enum(e) = &item.kind {
e.variants.iter().for_each(|e| {
self.def_sym(Namespace::Value, (*e.name).clone());
}),
_ => {}
})
}
ir::visit::walk_item(self, item);
self.parent = prev_parent;
Expand Down Expand Up @@ -454,9 +453,9 @@ impl Resolver {
assert!(status.r#mod.len() <= 1);

match ns {
Namespace::Value => status.value.get(0),
Namespace::Ty => status.ty.get(0),
Namespace::Mod => status.r#mod.get(0),
Namespace::Value => status.value.first(),
Namespace::Ty => status.ty.first(),
Namespace::Mod => status.r#mod.first(),
}
.copied()
}
Expand Down
1 change: 1 addition & 0 deletions pilota/src/prost/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1559,6 +1559,7 @@ macro_rules! map {
///
/// This is necessary because enumeration values can have a default value other
/// than 0 in proto2.
#[allow(clippy::too_many_arguments)]
pub fn encode_with_default<K, V, B, KE, KL, VE, VL>(
key_encode: KE,
key_encoded_len: KL,
Expand Down
2 changes: 1 addition & 1 deletion pilota/src/thrift/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ impl TInputProtocol for TBinaryProtocol<&mut Bytes> {
fn read_faststr(&mut self) -> Result<FastStr, DecodeError> {
let len = self.trans.read_i32()? as usize;
let bytes = self.trans.split_to(len);
unsafe { return Ok(FastStr::from_bytes_unchecked(bytes)) };
unsafe { Ok(FastStr::from_bytes_unchecked(bytes)) }
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion pilota/src/thrift/binary_le.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ impl TInputProtocol for TBinaryProtocol<&mut Bytes> {
fn read_faststr(&mut self) -> Result<FastStr, DecodeError> {
let len = self.trans.read_i32_le()? as usize;
let bytes = self.trans.split_to(len);
unsafe { return Ok(FastStr::from_bytes_unchecked(bytes)) };
unsafe { Ok(FastStr::from_bytes_unchecked(bytes)) }
}

#[inline]
Expand Down
Loading

0 comments on commit c5015cc

Please sign in to comment.