Skip to content

Commit 4f248cc

Browse files
committed
Fix clippy warnings and errors
Also includes code generation changes provided by @russcam in PR #210.
1 parent bf09d72 commit 4f248cc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+1304
-1330
lines changed

api_generator/docs/namespaces/security.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@ Security APIs
88
- Manage role mappings
99
- Manage API keys
1010
- Manage Bearer tokens
11-
- Authenticate users against an OpenID Connect or SAML authentication realm when using a
12-
custom web application other than Kibana
11+
- Authenticate users against an OpenID Connect or SAML authentication realm when using a custom web application other than Kibana

api_generator/src/generator/code_gen/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fn ident<I: AsRef<str>>(name: I) -> syn::Ident {
6767
fn doc<I: Into<String>>(comment: I) -> syn::Attribute {
6868
syn::Attribute {
6969
style: syn::AttrStyle::Outer,
70-
value: syn::MetaItem::NameValue(ident("doc".to_string()), lit(comment)),
70+
value: syn::MetaItem::NameValue(ident("doc"), lit(comment)),
7171
is_sugared_doc: true,
7272
}
7373
}
@@ -136,15 +136,15 @@ pub trait GetPath {
136136
impl GetPath for syn::Ty {
137137
fn get_path(&self) -> &syn::Path {
138138
match *self {
139-
syn::Ty::Path(_, ref p) => &p,
139+
syn::Ty::Path(_, ref p) => p,
140140
ref p => panic!("Expected syn::Ty::Path, but found {:?}", p),
141141
}
142142
}
143143
}
144144

145145
impl GetPath for syn::Path {
146146
fn get_path(&self) -> &syn::Path {
147-
&self
147+
self
148148
}
149149
}
150150

@@ -172,7 +172,7 @@ fn typekind_to_ty(name: &str, kind: &TypeKind, required: bool, fn_arg: bool) ->
172172
TypeKind::List => {
173173
v.push_str("&'b [");
174174
v.push_str(str_type);
175-
v.push_str("]");
175+
v.push(']');
176176
}
177177
TypeKind::Enum => match name {
178178
// opened https://github.com/elastic/elasticsearch/issues/53212
@@ -181,7 +181,7 @@ fn typekind_to_ty(name: &str, kind: &TypeKind, required: bool, fn_arg: bool) ->
181181
// Expand wildcards should
182182
v.push_str("&'b [");
183183
v.push_str(name.to_pascal_case().as_str());
184-
v.push_str("]");
184+
v.push(']');
185185
}
186186
_ => v.push_str(name.to_pascal_case().as_str()),
187187
},
@@ -219,7 +219,7 @@ fn typekind_to_ty(name: &str, kind: &TypeKind, required: bool, fn_arg: bool) ->
219219
};
220220

221221
if !required {
222-
v.push_str(">");
222+
v.push('>');
223223
}
224224

225225
syn::parse_type(v.as_str()).unwrap()

api_generator/src/generator/code_gen/namespace_clients.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub fn generate(api: &Api, docs_dir: &PathBuf) -> anyhow::Result<Vec<(String, St
4141
tokens.append(use_declarations());
4242

4343
let namespace_pascal_case = namespace_name.to_pascal_case();
44-
let namespace_replaced_pascal_case = namespace_name.replace("_", " ").to_pascal_case();
44+
let namespace_replaced_pascal_case = namespace_name.replace('_', " ").to_pascal_case();
4545
let namespace_client_name = ident(&namespace_pascal_case);
4646
let name_for_docs = match namespace_replaced_pascal_case.as_ref() {
4747
"Ccr" => "Cross Cluster Replication",
@@ -61,7 +61,7 @@ pub fn generate(api: &Api, docs_dir: &PathBuf) -> anyhow::Result<Vec<(String, St
6161
"Creates a new instance of [{}]",
6262
&namespace_pascal_case
6363
));
64-
let namespace_name = ident(namespace_name.to_string());
64+
let namespace_name = ident(namespace_name);
6565

6666
let (builders, methods): (Vec<Tokens>, Vec<Tokens>) = namespace
6767
.endpoints()
@@ -74,7 +74,7 @@ pub fn generate(api: &Api, docs_dir: &PathBuf) -> anyhow::Result<Vec<(String, St
7474
name,
7575
&builder_name,
7676
&api.common_params,
77-
&endpoint,
77+
endpoint,
7878
false,
7979
)
8080
.build()

api_generator/src/generator/code_gen/params.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn generate(api: &Api) -> anyhow::Result<String> {
2727
use serde::{Serialize, Deserialize};
2828
);
2929
for e in &api.enums {
30-
generate_param(&mut tokens, &e);
30+
generate_param(&mut tokens, e);
3131
}
3232

3333
let generated = tokens.to_string();
@@ -60,10 +60,7 @@ fn generate_param(tokens: &mut Tokens, e: &ApiEnum) {
6060
})
6161
.unzip();
6262

63-
let doc = match &e.description {
64-
Some(description) => Some(code_gen::doc(description)),
65-
None => None,
66-
};
63+
let doc = e.description.as_ref().map(code_gen::doc);
6764

6865
let cfg_attr = e.stability.outer_cfg_attr();
6966
let cfg_doc = stability_doc(e.stability);

api_generator/src/generator/code_gen/request/request_builder.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use crate::generator::{
2424
use inflector::Inflector;
2525
use quote::{ToTokens, Tokens};
2626
use reqwest::Url;
27+
use std::path::Path;
2728
use std::{collections::BTreeMap, fs, path::PathBuf, str};
2829
use syn::{Field, FieldValue, ImplItem, TraitBoundModifier, TyParamBound};
2930

@@ -245,7 +246,7 @@ impl<'a> RequestBuilder<'a> {
245246
default_fields: &[&syn::Ident],
246247
accepts_nd_body: bool,
247248
) -> syn::ImplItem {
248-
let fields: Vec<FieldValue> = default_fields
249+
let fields = default_fields
249250
.iter()
250251
.filter(|&&part| part != &ident("body"))
251252
.map(|&part| syn::FieldValue {
@@ -257,8 +258,7 @@ impl<'a> RequestBuilder<'a> {
257258
)
258259
.into(),
259260
is_shorthand: false,
260-
})
261-
.collect();
261+
});
262262

263263
let (fn_arg, field_arg, ret_ty) = if accepts_nd_body {
264264
(
@@ -401,9 +401,9 @@ impl<'a> RequestBuilder<'a> {
401401

402402
/// Creates the AST for a builder fn for a builder impl
403403
fn create_impl_fn(f: (&String, &Type)) -> syn::ImplItem {
404-
let name = valid_name(&f.0).to_lowercase();
404+
let name = valid_name(f.0).to_lowercase();
405405
let (ty, value_ident, fn_generics) = {
406-
let ty = typekind_to_ty(&f.0, &f.1.ty, true, true);
406+
let ty = typekind_to_ty(f.0, &f.1.ty, true, true);
407407
match ty {
408408
syn::Ty::Path(ref _q, ref p) => {
409409
if p.get_ident().as_ref() == "Into" {
@@ -552,7 +552,7 @@ impl<'a> RequestBuilder<'a> {
552552
// add a body impl if supported
553553
if supports_body {
554554
let body_fn = Self::create_body_fn(
555-
&builder_name,
555+
builder_name,
556556
&builder_ident,
557557
&default_fields,
558558
accepts_nd_body,
@@ -566,9 +566,9 @@ impl<'a> RequestBuilder<'a> {
566566
builder_fns.dedup_by(|a, b| a.ident.eq(&b.ident));
567567

568568
let new_fn =
569-
Self::create_new_fn(&builder_name, &builder_ident, enum_builder, &default_fields);
569+
Self::create_new_fn(builder_name, &builder_ident, enum_builder, &default_fields);
570570

571-
let method_expr = Self::create_method_expression(&builder_name, &endpoint);
571+
let method_expr = Self::create_method_expression(builder_name, endpoint);
572572

573573
let query_string_params = {
574574
let mut p = endpoint.params.clone();
@@ -669,7 +669,7 @@ impl<'a> RequestBuilder<'a> {
669669
/// Creates the AST for a fn that returns a new instance of a builder struct
670670
/// from the root or namespace client
671671
fn create_builder_struct_ctor_fns(
672-
docs_dir: &PathBuf,
672+
docs_dir: &Path,
673673
namespace_name: &str,
674674
name: &str,
675675
builder_name: &str,
@@ -697,7 +697,7 @@ impl<'a> RequestBuilder<'a> {
697697
let api_name_for_docs = split_on_pascal_case(builder_name);
698698

699699
let markdown_doc = {
700-
let mut path = docs_dir.clone();
700+
let mut path = docs_dir.to_owned();
701701
path.push("functions");
702702
path.push(format!("{}.{}.md", namespace_name, name));
703703
if path.exists() {
@@ -759,10 +759,10 @@ impl<'a> RequestBuilder<'a> {
759759
/// Creates the AST for a field for a struct
760760
fn create_struct_field(f: (&String, &Type)) -> syn::Field {
761761
syn::Field {
762-
ident: Some(ident(valid_name(&f.0).to_lowercase())),
762+
ident: Some(ident(valid_name(f.0).to_lowercase())),
763763
vis: syn::Visibility::Inherited,
764764
attrs: vec![],
765-
ty: typekind_to_ty(&f.0, &f.1.ty, false, false),
765+
ty: typekind_to_ty(f.0, &f.1.ty, false, false),
766766
}
767767
}
768768

api_generator/src/generator/code_gen/root.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ pub fn generate(api: &Api, docs_dir: &PathBuf) -> anyhow::Result<String> {
5252
tokens.append(quote!(
5353
#(#builders)*
5454

55+
#[allow(clippy::needless_lifetimes)]
5556
impl Elasticsearch {
5657
#(#methods)*
5758
}

api_generator/src/generator/code_gen/url/enum_builder.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl<'a> EnumBuilder<'a> {
9191
0 => Self::parts_none(),
9292
_ => {
9393
self.has_lifetime = true;
94-
Self::parts(&path)
94+
Self::parts(path)
9595
}
9696
};
9797

@@ -113,18 +113,18 @@ impl<'a> EnumBuilder<'a> {
113113
.join("");
114114

115115
let doc = match params.len() {
116-
1 => doc(params[0].replace("_", " ").to_pascal_case()),
116+
1 => doc(params[0].replace('_', " ").to_pascal_case()),
117117
n => {
118118
let mut d: String = params
119119
.iter()
120120
.enumerate()
121121
.filter(|&(i, _)| i != n - 1)
122-
.map(|(_, e)| e.replace("_", " ").to_pascal_case())
122+
.map(|(_, e)| e.replace('_', " ").to_pascal_case())
123123
.collect::<Vec<_>>()
124124
.join(", ");
125125

126126
d.push_str(
127-
format!(" and {}", params[n - 1].replace("_", " ").to_pascal_case()).as_str(),
127+
format!(" and {}", params[n - 1].replace('_', " ").to_pascal_case()).as_str(),
128128
);
129129
doc(d)
130130
}
@@ -181,7 +181,7 @@ impl<'a> EnumBuilder<'a> {
181181
.iter()
182182
.map(|&p| {
183183
syn::Pat::Ident(
184-
syn::BindingMode::ByRef(syn::Mutability::Immutable),
184+
syn::BindingMode::ByValue(syn::Mutability::Immutable),
185185
ident(valid_name(p)),
186186
None,
187187
)
@@ -282,6 +282,7 @@ impl<'a> EnumBuilder<'a> {
282282
syn::NestedMetaItem::MetaItem(syn::MetaItem::Word(ident("Debug"))),
283283
syn::NestedMetaItem::MetaItem(syn::MetaItem::Word(ident("Clone"))),
284284
syn::NestedMetaItem::MetaItem(syn::MetaItem::Word(ident("PartialEq"))),
285+
syn::NestedMetaItem::MetaItem(syn::MetaItem::Word(ident("Eq"))),
285286
],
286287
),
287288
},
@@ -299,7 +300,7 @@ impl<'a> From<&'a (String, ApiEndpoint)> for EnumBuilder<'a> {
299300
let endpoint = &value.1;
300301
let mut builder = EnumBuilder::new(value.0.to_pascal_case().as_ref());
301302
for path in &endpoint.url.paths {
302-
builder = builder.with_path(&path);
303+
builder = builder.with_path(path);
303304
}
304305

305306
builder
@@ -391,7 +392,7 @@ mod tests {
391392
assert_eq!(ty_b("SearchParts"), enum_ty);
392393

393394
let expected_decl = quote!(
394-
#[derive(Debug, Clone, PartialEq)]
395+
#[derive(Debug, Clone, PartialEq, Eq)]
395396
#[doc = "API parts for the Search API"]
396397
pub enum SearchParts<'b> {
397398
#[doc = "No parts"]
@@ -411,7 +412,7 @@ mod tests {
411412
pub fn url(self) -> Cow<'static, str> {
412413
match self {
413414
SearchParts::None => "/_search".into(),
414-
SearchParts::Index(ref index) => {
415+
SearchParts::Index(index) => {
415416
let index_str = index.join(",");
416417
let encoded_index: Cow<str> = percent_encode(index_str.as_bytes(), PARTS_ENCODED).into();
417418
let mut p = String::with_capacity(9usize + encoded_index.len());
@@ -420,7 +421,7 @@ mod tests {
420421
p.push_str("/_search");
421422
p.into()
422423
}
423-
SearchParts::IndexType(ref index, ref ty) => {
424+
SearchParts::IndexType(index, ty) => {
424425
let index_str = index.join(",");
425426
let ty_str = ty.join(",");
426427
let encoded_index: Cow<str> = percent_encode(index_str.as_bytes(), PARTS_ENCODED).into();

api_generator/src/generator/code_gen/url/url_builder.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ impl<'a> UrlBuilder<'a> {
170170
/// Build the AST for an allocated url from the path literals and params.
171171
fn build_owned(self) -> syn::Block {
172172
// collection of let {name}_str = [self.]{name}.[join(",")|to_string()];
173-
let let_params_exprs = Self::let_parameters_exprs(&self.path, &self.parts);
173+
let let_params_exprs = Self::let_parameters_exprs(&self.path, self.parts);
174174

175-
let mut let_encoded_params_exprs = Self::let_encoded_exprs(&self.path, &self.parts);
175+
let mut let_encoded_params_exprs = Self::let_encoded_exprs(&self.path, self.parts);
176176

177177
let url_ident = ident("p");
178178
let len_expr = {
@@ -275,7 +275,7 @@ impl<'a> UrlBuilder<'a> {
275275
.filter_map(|p| match *p {
276276
PathPart::Param(p) => {
277277
let name = valid_name(p);
278-
let name_ident = ident(&name);
278+
let name_ident = ident(name);
279279
let ty = &parts[p].ty;
280280

281281
// don't generate an assignment expression for strings
@@ -405,8 +405,15 @@ impl<'a> UrlBuilder<'a> {
405405
url.iter()
406406
.map(|p| match *p {
407407
PathPart::Literal(p) => {
408-
let lit = syn::Lit::Str(p.to_string(), syn::StrStyle::Cooked);
409-
syn::Stmt::Semi(Box::new(parse_expr(quote!(#url_ident.push_str(#lit)))))
408+
let push = if p.len() == 1 {
409+
let lit = syn::Lit::Char(p.chars().next().unwrap());
410+
quote!(#url_ident.push(#lit))
411+
} else {
412+
let lit = syn::Lit::Str(p.to_string(), syn::StrStyle::Cooked);
413+
quote!(#url_ident.push_str(#lit))
414+
};
415+
416+
syn::Stmt::Semi(Box::new(parse_expr(push)))
410417
}
411418
PathPart::Param(p) => {
412419
let name = format!("encoded_{}", valid_name(p));
@@ -420,10 +427,7 @@ impl<'a> UrlBuilder<'a> {
420427
}
421428

422429
pub fn build(self) -> syn::Expr {
423-
let has_params = self.path.iter().any(|p| match *p {
424-
PathPart::Param(_) => true,
425-
_ => false,
426-
});
430+
let has_params = self.path.iter().any(|p| matches!(*p, PathPart::Param(_)));
427431

428432
if has_params {
429433
self.build_owned().into_expr()

0 commit comments

Comments
 (0)