Skip to content

Commit 495e085

Browse files
authored
address clippy 0.1.56 lints in services (#437)
1 parent cf10b01 commit 495e085

File tree

358 files changed

+75316
-5026
lines changed

Some content is hidden

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

358 files changed

+75316
-5026
lines changed

services/autorust/codegen/examples/gen_mgmt.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ const BOX_PROPERTIES: &[(&str, &str, &str)] = &[
264264
("../../../azure-rest-api-specs/specification/network/resource-manager/Microsoft.Network/stable/2021-02-01/publicIpAddress.json", "PublicIPAddress", "properties"),
265265
("../../../azure-rest-api-specs/specification/network/resource-manager/Microsoft.Network/stable/2021-03-01/customIpPrefix.json", "CustomIpPrefix", "properties"),
266266
("../../../azure-rest-api-specs/specification/network/resource-manager/Microsoft.Network/stable/2021-03-01/publicIpAddress.json", "PublicIPAddress", "properties"),
267+
("../../../azure-rest-api-specs/specification/network/resource-manager/Microsoft.Network/stable/2021-05-01/customIpPrefix.json", "CustomIpPrefix", "properties"),
268+
("../../../azure-rest-api-specs/specification/network/resource-manager/Microsoft.Network/stable/2021-05-01/publicIpAddress.json", "PublicIPAddress", "properties"),
267269
("../../../azure-rest-api-specs/specification/network/resource-manager/Microsoft.Network/stable/2016-06-01/network.json", "PublicIPAddressPropertiesFormat", "ipConfiguration"),
268270
("../../../azure-rest-api-specs/specification/network/resource-manager/Microsoft.Network/stable/2016-06-01/network.json", "PublicIPAddress", "properties"),
269271
("../../../azure-rest-api-specs/specification/network/resource-manager/Microsoft.Network/stable/2016-06-01/network.json", "IPConfigurationPropertiesFormat", "publicIPAddress"),
@@ -307,16 +309,14 @@ fn main() -> Result<()> {
307309
.iter()
308310
.enumerate()
309311
{
310-
if ONLY_SERVICES.len() > 0 {
312+
if !ONLY_SERVICES.is_empty() {
311313
if ONLY_SERVICES.contains(&spec.spec()) {
312314
println!("{} {}", i + 1, spec.spec());
313315
gen_crate(spec)?;
314316
}
315-
} else {
316-
if !SKIP_SERVICES.contains(&spec.spec()) {
317-
println!("{} {}", i + 1, spec.spec());
318-
gen_crate(spec)?;
319-
}
317+
} else if !SKIP_SERVICES.contains(&spec.spec()) {
318+
println!("{} {}", i + 1, spec.spec());
319+
gen_crate(spec)?;
320320
}
321321
}
322322
Ok(())
@@ -372,14 +372,14 @@ fn gen_crate(spec: &SpecReadme) -> Result<()> {
372372
let input_files: Result<Vec<_>> = config
373373
.input_files
374374
.iter()
375-
.map(|input_file| Ok(path::join(spec.readme(), input_file).map_err(|source| Error::PathError { source })?))
375+
.map(|input_file| path::join(spec.readme(), input_file).map_err(|source| Error::PathError { source }))
376376
.collect();
377377
let input_files = input_files?;
378378
// for input_file in &input_files {
379379
// println!(" {:?}", input_file);
380380
// }
381381
autorust_codegen::run(Config {
382-
output_folder: mod_output_folder.into(),
382+
output_folder: mod_output_folder,
383383
input_files,
384384
box_properties: box_properties.clone(),
385385
optional_properties: optional_properties.clone(),
@@ -388,7 +388,7 @@ fn gen_crate(spec: &SpecReadme) -> Result<()> {
388388
})
389389
.map_err(|source| Error::CodegenError { source })?;
390390
}
391-
if feature_mod_names.len() == 0 {
391+
if feature_mod_names.is_empty() {
392392
return Ok(());
393393
}
394394
cargo_toml::create(

services/autorust/codegen/examples/gen_svc.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,14 @@ fn main() -> Result<()> {
143143
.iter()
144144
.enumerate()
145145
{
146-
if ONLY_SERVICES.len() > 0 {
146+
if !ONLY_SERVICES.is_empty() {
147147
if ONLY_SERVICES.contains(&spec.spec()) {
148148
println!("{} {}", i + 1, spec.spec());
149149
gen_crate(spec)?;
150150
}
151-
} else {
152-
if !SKIP_SERVICES.contains(&spec.spec()) {
153-
println!("{} {}", i + 1, spec.spec());
154-
gen_crate(spec)?;
155-
}
151+
} else if !SKIP_SERVICES.contains(&spec.spec()) {
152+
println!("{} {}", i + 1, spec.spec());
153+
gen_crate(spec)?;
156154
}
157155
}
158156
Ok(())
@@ -225,14 +223,14 @@ fn gen_crate(spec: &SpecReadme) -> Result<()> {
225223
let input_files: Result<Vec<_>> = config
226224
.input_files
227225
.iter()
228-
.map(|input_file| Ok(path::join(spec.readme(), input_file).map_err(|source| Error::PathError { source })?))
226+
.map(|input_file| path::join(spec.readme(), input_file).map_err(|source| Error::PathError { source }))
229227
.collect();
230228
let input_files = input_files?;
231229
// for input_file in &input_files {
232230
// println!(" {:?}", input_file);
233231
// }
234232
autorust_codegen::run(Config {
235-
output_folder: mod_output_folder.into(),
233+
output_folder: mod_output_folder,
236234
input_files,
237235
box_properties: box_properties.clone(),
238236
fix_case_properties: fix_case_properties.clone(),
@@ -242,7 +240,7 @@ fn gen_crate(spec: &SpecReadme) -> Result<()> {
242240
})
243241
.map_err(|source| Error::CodegenError { source })?;
244242
}
245-
if feature_mod_names.len() == 0 {
243+
if feature_mod_names.is_empty() {
246244
return Ok(());
247245
}
248246
cargo_toml::create(

services/autorust/codegen/src/cargo_toml.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub enum Error {
1111
IoError { source: std::io::Error },
1212
}
1313

14-
pub fn create(crate_name: &str, feature_mod_names: &Vec<(String, String)>, path: &Path) -> Result<()> {
14+
pub fn create(crate_name: &str, feature_mod_names: &[(String, String)], path: &Path) -> Result<()> {
1515
let file = File::create(path).map_err(|source| Error::IoError { source })?;
1616
let mut file = LineWriter::new(file);
1717
let version = &env!("CARGO_PKG_VERSION");
@@ -55,7 +55,7 @@ no-default-version = []
5555
Ok(())
5656
}
5757

58-
fn get_default_feature(feature_mod_names: &Vec<(String, String)>) -> String {
58+
fn get_default_feature(feature_mod_names: &[(String, String)]) -> String {
5959
let default = feature_mod_names
6060
.iter()
6161
.map(|(feature, _)| feature)

services/autorust/codegen/src/codegen.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub fn is_string(schema: &SchemaCommon) -> bool {
127127
}
128128

129129
pub fn get_schema_array_items(schema: &SchemaCommon) -> Result<&ReferenceOr<Schema>, Error> {
130-
Ok(schema.items.as_ref().as_ref().ok_or_else(|| Error::ArrayExpectedToHaveItems)?)
130+
schema.items.as_ref().as_ref().ok_or(Error::ArrayExpectedToHaveItems)
131131
}
132132

133133
/// A header placed at the top the file to say that it is generated by AutoRust.
@@ -138,11 +138,11 @@ pub fn create_generated_by_header() -> TokenStream {
138138
}
139139

140140
pub fn is_local_enum(property: &ResolvedSchema) -> bool {
141-
property.schema.common.enum_.len() > 0
141+
!property.schema.common.enum_.is_empty()
142142
}
143143

144144
pub fn is_local_struct(property: &ResolvedSchema) -> bool {
145-
property.schema.properties.len() > 0
145+
!property.schema.properties.is_empty()
146146
}
147147

148148
pub fn is_basic_type(property: &ResolvedSchema) -> bool {
@@ -161,7 +161,7 @@ pub fn require(is_required: bool, tp: TokenStream) -> TokenStream {
161161
}
162162
}
163163

164-
pub fn enum_values_as_strings(values: &Vec<Value>) -> Vec<&str> {
164+
pub fn enum_values_as_strings(values: &[Value]) -> Vec<&str> {
165165
values
166166
.iter()
167167
.filter_map(|v| match v {
@@ -176,10 +176,10 @@ pub fn get_type_name_for_schema(schema: &SchemaCommon, as_ref: AsReference) -> R
176176
let format = schema.format.as_deref();
177177
let ts = match schema_type {
178178
DataType::Array => {
179-
let items = get_schema_array_items(&schema)?;
180-
let vec_items_typ = get_type_name_for_schema_ref(&items, as_ref)?;
179+
let items = get_schema_array_items(schema)?;
180+
let vec_items_typ = get_type_name_for_schema_ref(items, as_ref)?;
181181
match as_ref {
182-
AsReference::True => quote! { &Vec<#vec_items_typ> },
182+
AsReference::True => quote! { &[#vec_items_typ] },
183183
AsReference::False => quote! { Vec<#vec_items_typ> },
184184
}
185185
}
@@ -226,7 +226,7 @@ pub fn get_type_name_for_schema(schema: &SchemaCommon, as_ref: AsReference) -> R
226226
pub fn get_type_name_for_schema_ref(schema: &ReferenceOr<Schema>, as_ref: AsReference) -> Result<TokenStream, Error> {
227227
match schema {
228228
ReferenceOr::Reference { reference, .. } => {
229-
let name = &reference.name.as_ref().ok_or_else(|| Error::NoNameForRef)?;
229+
let name = &reference.name.as_ref().ok_or(Error::NoNameForRef)?;
230230
let idt = ident(&name.to_camel_case()).map_err(Error::TypeNameForSchemaRef)?;
231231
match as_ref {
232232
AsReference::True => Ok(quote! { &#idt }),

services/autorust/codegen/src/codegen_models.rs

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,18 @@ pub fn create_models(cg: &CodeGen) -> Result<TokenStream, Error> {
7878
// "WARN schema {} already created from {:?}, duplicate from {:?}",
7979
// schema_name, _first_doc_file, doc_file
8080
// );
81+
} else if is_array(&schema.schema.common) {
82+
file.extend(create_vec_alias(schema_name, schema)?);
83+
} else if is_local_enum(schema) {
84+
let no_namespace = TokenStream::new();
85+
let (_tp_name, tp) = create_enum(&no_namespace, schema_name, schema, false)?;
86+
file.extend(tp);
87+
} else if is_basic_type(schema) {
88+
let (id, value) = create_basic_type_alias(schema_name, schema)?;
89+
file.extend(quote! { pub type #id = #value;});
8190
} else {
82-
if is_array(&schema.schema.common) {
83-
file.extend(create_vec_alias(schema_name, schema)?);
84-
} else if is_local_enum(schema) {
85-
let no_namespace = TokenStream::new();
86-
let (_tp_name, tp) = create_enum(&no_namespace, schema_name, schema, false)?;
87-
file.extend(tp);
88-
} else if is_basic_type(schema) {
89-
let (id, value) = create_basic_type_alias(schema_name, schema)?;
90-
file.extend(quote! { pub type #id = #value;});
91-
} else {
92-
for stream in create_struct(cg, doc_file, schema_name, schema)? {
93-
file.extend(stream);
94-
}
91+
for stream in create_struct(cg, doc_file, schema_name, schema)? {
92+
file.extend(stream);
9593
}
9694
}
9795
}
@@ -113,13 +111,11 @@ fn add_schema_refs(
113111
) -> Result<(), Error> {
114112
let schema = cg.spec.resolve_schema_ref(doc_file, schema_ref)?;
115113
if let Some(ref_key) = schema.ref_key.clone() {
116-
if !schemas.contains_key(&ref_key) {
117-
if !cg.spec.is_input_file(&ref_key.file_path) {
118-
let refs = get_schema_schema_references(&schema.schema);
119-
schemas.insert(ref_key.clone(), schema);
120-
for reference in refs {
121-
add_schema_refs(cg, schemas, &ref_key.file_path, reference)?;
122-
}
114+
if !schemas.contains_key(&ref_key) && !cg.spec.is_input_file(&ref_key.file_path) {
115+
let refs = get_schema_schema_references(&schema.schema);
116+
schemas.insert(ref_key.clone(), schema);
117+
for reference in refs {
118+
add_schema_refs(cg, schemas, &ref_key.file_path, reference)?;
123119
}
124120
}
125121
}
@@ -144,14 +140,12 @@ fn create_enum(
144140
property: property_name.to_owned(),
145141
})?;
146142
let lower = name.to_lowercase();
147-
let rename = if &nm.to_string() == name {
143+
let rename = if nm.to_string() == name {
148144
quote! {}
145+
} else if name != lower && lowercase_workaround {
146+
quote! { #[serde(rename = #name, alias = #lower)] }
149147
} else {
150-
if name != lower && lowercase_workaround {
151-
quote! { #[serde(rename = #name, alias = #lower)] }
152-
} else {
153-
quote! { #[serde(rename = #name)] }
154-
}
148+
quote! { #[serde(rename = #name)] }
155149
};
156150
let value = quote! {
157151
#rename
@@ -176,7 +170,7 @@ fn create_enum(
176170
fn create_vec_alias(alias_name: &str, schema: &ResolvedSchema) -> Result<TokenStream, Error> {
177171
let items = get_schema_array_items(&schema.schema.common)?;
178172
let typ = ident(&alias_name.to_camel_case()).map_err(Error::VecAliasName)?;
179-
let items_typ = get_type_name_for_schema_ref(&items, AsReference::False)?;
173+
let items_typ = get_type_name_for_schema_ref(items, AsReference::False)?;
180174
Ok(quote! { pub type #typ = Vec<#items_typ>; })
181175
}
182176

@@ -239,7 +233,7 @@ fn create_struct(cg: &CodeGen, doc_file: &Path, struct_name: &str, schema: &Reso
239233
if is_local_enum(property) && lowercase_workaround {
240234
serde_attrs.push(quote! { deserialize_with = "case_insensitive"});
241235
}
242-
let serde = if serde_attrs.len() > 0 {
236+
let serde = if !serde_attrs.is_empty() {
243237
quote! { #[serde(#(#serde_attrs),*)] }
244238
} else {
245239
quote! {}
@@ -261,9 +255,9 @@ fn create_struct(cg: &CodeGen, doc_file: &Path, struct_name: &str, schema: &Reso
261255
#props
262256
}
263257
};
264-
streams.push(TokenStream::from(st));
258+
streams.push(st);
265259

266-
if local_types.len() > 0 {
260+
if !local_types.is_empty() {
267261
let mut types = TokenStream::new();
268262
local_types.into_iter().for_each(|tp| types.extend(tp));
269263
streams.push(quote! {

services/autorust/codegen/src/codegen_operations.rs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,7 @@ fn create_function(cg: &CodeGen, doc_file: &Path, operation: &WebOperation) -> R
7979

8080
let params = parse_params(&operation.path);
8181
// println!("path params {:#?}", params);
82-
let params: Result<Vec<_>, Error> = params
83-
.iter()
84-
.map(|s| Ok(ident(&s.to_snake_case()).map_err(Error::ParamName)?))
85-
.collect();
82+
let params: Result<Vec<_>, Error> = params.iter().map(|s| ident(&s.to_snake_case()).map_err(Error::ParamName)).collect();
8683
let params = params?;
8784
let url_str_args = quote! { #(#params),* };
8885

@@ -95,7 +92,7 @@ fn create_function(cg: &CodeGen, doc_file: &Path, operation: &WebOperation) -> R
9592
if cg.spec.api_version().is_some() {
9693
skip.insert("api-version");
9794
}
98-
let parameters = parameters.into_iter().filter(|p| !skip.contains(p.name.as_str())).collect();
95+
let parameters: Vec<_> = parameters.into_iter().filter(|p| !skip.contains(p.name.as_str())).collect();
9996

10097
let fparams = create_function_params(cg, doc_file, &parameters)?;
10198

@@ -142,7 +139,7 @@ fn create_function(cg: &CodeGen, doc_file: &Path, operation: &WebOperation) -> R
142139
let mut has_body_parameter = false;
143140
for param in &parameters {
144141
let param_name = &param.name;
145-
let param_name_var = get_param_name(&param)?;
142+
let param_name_var = get_param_name(param)?;
146143
let required = param.required.unwrap_or(false);
147144
let is_bool = matches!(&param.common.type_, Some(DataType::Boolean));
148145
match param.in_ {
@@ -207,20 +204,18 @@ fn create_function(cg: &CodeGen, doc_file: &Path, operation: &WebOperation) -> R
207204
req_builder = req_builder.header(#param_name, #param_name_var);
208205
});
209206
}
207+
} else if is_bool {
208+
ts_request_builder.extend(quote! {
209+
if let Some(#param_name_var) = #param_name_var {
210+
req_builder = req_builder.header(#param_name, #param_name_var .to_string());
211+
}
212+
});
210213
} else {
211-
if is_bool {
212-
ts_request_builder.extend(quote! {
213-
if let Some(#param_name_var) = #param_name_var {
214-
req_builder = req_builder.header(#param_name, #param_name_var .to_string());
215-
}
216-
});
217-
} else {
218-
ts_request_builder.extend(quote! {
219-
if let Some(#param_name_var) = #param_name_var {
220-
req_builder = req_builder.header(#param_name, #param_name_var);
221-
}
222-
});
223-
}
214+
ts_request_builder.extend(quote! {
215+
if let Some(#param_name_var) = #param_name_var {
216+
req_builder = req_builder.header(#param_name, #param_name_var);
217+
}
218+
});
224219
}
225220
}
226221
ParameterType::Body => {
@@ -487,7 +482,7 @@ fn create_function(cg: &CodeGen, doc_file: &Path, operation: &WebOperation) -> R
487482
}
488483
}
489484
};
490-
Ok(TokenStream::from(func))
485+
Ok(func)
491486
}
492487

493488
fn create_rsp_value(tp: Option<&TokenStream>, fname: &TokenStream) -> TokenStream {
@@ -506,7 +501,7 @@ fn format_path(path: &str) -> String {
506501
PARAM_RE.replace_all(path, "{}").to_string()
507502
}
508503

509-
fn create_function_params(_cg: &CodeGen, _doc_file: &Path, parameters: &Vec<Parameter>) -> Result<TokenStream, Error> {
504+
fn create_function_params(_cg: &CodeGen, _doc_file: &Path, parameters: &[Parameter]) -> Result<TokenStream, Error> {
510505
let mut params: Vec<TokenStream> = Vec::new();
511506
for param in parameters {
512507
let name = get_param_name(param)?;

0 commit comments

Comments
 (0)