Explorar o código

Improve code as suggested by clippy -W clippy::pedantic (#1242)

clippy::pedantic can come up with some good suggestions. Implement some of them;
there are more to be done.

Signed-off-by: Sean Young <sean@mess.org>
Sean Young %!s(int64=2) %!d(string=hai) anos
pai
achega
75c05c7ba1

+ 10 - 1
solang-parser/src/lib.rs

@@ -19,7 +19,16 @@ pub mod pt;
 #[cfg(test)]
 mod tests;
 
-#[allow(clippy::all)]
+#[allow(
+    clippy::needless_lifetimes,
+    clippy::clone_on_copy,
+    clippy::type_complexity,
+    clippy::too_many_arguments,
+    clippy::ptr_arg,
+    clippy::redundant_clone,
+    clippy::just_underscores_and_digits,
+    clippy::or_fun_call
+)]
 mod solidity {
     include!(concat!(env!("OUT_DIR"), "/solidity.rs"));
 }

+ 10 - 10
src/abi/substrate.rs

@@ -240,13 +240,13 @@ fn type_to_storage_layout(
     registry: &PortableRegistryBuilder,
 ) -> Layout<PortableForm> {
     let ty = registry.get(key).unwrap();
-    match ty.type_def() {
+    match &ty.type_def {
         TypeDef::Composite(inner) => Layout::Struct(StructLayout::new(
-            ty.path().ident().unwrap_or_default(),
-            inner.fields().iter().map(|field| {
+            ty.path.ident().unwrap_or_default(),
+            inner.fields.iter().map(|field| {
                 FieldLayout::new(
-                    field.name().map(ToString::to_string).unwrap_or_default(),
-                    type_to_storage_layout(field.ty().id(), root, registry),
+                    field.name.clone().unwrap_or_default(),
+                    type_to_storage_layout(field.ty.id, root, registry),
                 )
             }),
         )),
@@ -290,7 +290,7 @@ pub fn gen_project(contract_no: usize, ns: &ast::Namespace) -> InkProject {
             .map(|p| {
                 let ty = resolve_ast(&p.ty, ns, &mut registry);
 
-                let path = registry.get(ty).unwrap().path().clone();
+                let path = registry.get(ty).unwrap().path.clone();
                 let spec = TypeSpec::new(ty.into(), path);
 
                 MessageParamSpec::new(p.name_as_str().to_string())
@@ -340,7 +340,7 @@ pub fn gen_project(contract_no: usize, ns: &ast::Namespace) -> InkProject {
             0 => None,
             1 => {
                 let ty = resolve_ast(&f.returns[0].ty, ns, &mut registry);
-                let path = registry.get(ty).unwrap().path().clone();
+                let path = registry.get(ty).unwrap().path.clone();
                 Some(TypeSpec::new(ty.into(), path))
             }
             _ => {
@@ -364,7 +364,7 @@ pub fn gen_project(contract_no: usize, ns: &ast::Namespace) -> InkProject {
                     TypeDef::Tuple(t),
                     Default::default(),
                 ));
-                let path = registry.get(ty).unwrap().path().clone();
+                let path = registry.get(ty).unwrap().path.clone();
                 Some(TypeSpec::new(ty.into(), path))
             }
         };
@@ -374,7 +374,7 @@ pub fn gen_project(contract_no: usize, ns: &ast::Namespace) -> InkProject {
             .iter()
             .map(|p| {
                 let ty = resolve_ast(&p.ty, ns, &mut registry);
-                let path = registry.get(ty).unwrap().path().clone();
+                let path = registry.get(ty).unwrap().path.clone();
                 let spec = TypeSpec::new(ty.into(), path);
 
                 MessageParamSpec::new(p.name_as_str().to_string())
@@ -426,7 +426,7 @@ pub fn gen_project(contract_no: usize, ns: &ast::Namespace) -> InkProject {
             .iter()
             .map(|p| {
                 let ty = resolve_ast(&p.ty, ns, &mut registry);
-                let path = registry.get(ty).unwrap().path().clone();
+                let path = registry.get(ty).unwrap().path.clone();
                 let spec = TypeSpec::new(ty.into(), path);
                 EventParamSpec::new(p.name_as_str().into())
                     .of_type(spec)

+ 1 - 1
src/bin/doc/mod.rs

@@ -346,7 +346,7 @@ pub fn generate_docs(outdir: &OsString, files: &[ast::Namespace], verbose: bool)
             let mut base_variables = Vec::new();
             let mut base_functions = Vec::new();
 
-            for base_no in bases.into_iter() {
+            for base_no in bases {
                 if contract_no == base_no {
                     continue;
                 }

+ 1 - 1
src/bin/languageserver/mod.rs

@@ -38,7 +38,7 @@ pub async fn start_server(target: Target, matches: &ArgMatches) -> ! {
 
     if let Some(paths) = matches.get_many::<PathBuf>("IMPORTPATH") {
         for path in paths {
-            importpaths.push(path.to_path_buf());
+            importpaths.push(path.clone());
         }
     }
 

+ 5 - 5
src/bin/solang.rs

@@ -443,7 +443,7 @@ fn compile(matches: &ArgMatches) {
     // Build a map of requested contract names, and a flag specifying whether it was found or not
     let contract_names: HashSet<&str> = if let Some(values) = matches.get_many::<String>("CONTRACT")
     {
-        values.map(|v| v.as_str()).collect()
+        values.map(String::as_str).collect()
     } else {
         HashSet::new()
     };
@@ -472,7 +472,7 @@ fn compile(matches: &ArgMatches) {
         }
     }
 
-    if let Some("ast-dot") = matches.get_one::<String>("EMIT").map(|v| v.as_str()) {
+    if let Some("ast-dot") = matches.get_one::<String>("EMIT").map(String::as_str) {
         exit(0);
     }
 
@@ -560,7 +560,7 @@ fn process_file(
     // codegen all the contracts; some additional errors/warnings will be detected here
     codegen(&mut ns, opt);
 
-    if let Some("ast-dot") = matches.get_one::<String>("EMIT").map(|v| v.as_str()) {
+    if let Some("ast-dot") = matches.get_one::<String>("EMIT").map(String::as_str) {
         let filepath = PathBuf::from(filename);
         let stem = filepath.file_stem().unwrap().to_string_lossy();
         let dot_filename = output_file(matches, &stem, "dot", false);
@@ -619,7 +619,7 @@ fn contract_results(
 
     seen_contracts.insert(resolved_contract.name.to_string(), loc);
 
-    if let Some("cfg") = matches.get_one::<String>("EMIT").map(|v| v.as_str()) {
+    if let Some("cfg") = matches.get_one::<String>("EMIT").map(String::as_str) {
         println!("{}", resolved_contract.print_cfg(ns));
         return;
     }
@@ -693,7 +693,7 @@ fn contract_results(
 fn save_intermediates(binary: &solang::emit::binary::Binary, matches: &ArgMatches) -> bool {
     let verbose = *matches.get_one("VERBOSE").unwrap();
 
-    match matches.get_one::<String>("EMIT").map(|v| v.as_str()) {
+    match matches.get_one::<String>("EMIT").map(String::as_str) {
         Some("llvm-ir") => {
             let llvm_filename = output_file(matches, &binary.name, "ll", false);
 

+ 1 - 1
src/sema/contracts.rs

@@ -381,7 +381,7 @@ fn check_inheritance(contract_no: usize, ns: &mut ast::Namespace) {
                         ));
                     } else {
                         let override_specified: HashSet<usize> =
-                            override_specified.iter().cloned().collect();
+                            override_specified.iter().copied().collect();
                         let override_needed: HashSet<usize> =
                             entry.iter().map(|(contract_no, _)| *contract_no).collect();
 

+ 3 - 3
src/sema/mod.rs

@@ -212,9 +212,9 @@ fn resolve_import(
     ns: &mut ast::Namespace,
 ) {
     let filename = match import {
-        pt::Import::Plain(f, _) => f,
-        pt::Import::GlobalSymbol(f, _, _) => f,
-        pt::Import::Rename(f, _, _) => f,
+        pt::Import::Plain(f, _)
+        | pt::Import::GlobalSymbol(f, _, _)
+        | pt::Import::Rename(f, _, _) => f,
     };
 
     let os_filename = OsStr::new(&filename.string);

+ 3 - 3
src/sema/yul/expression.rs

@@ -40,7 +40,7 @@ pub(crate) fn resolve_yul_expression(
     ns: &mut Namespace,
 ) -> Result<YulExpression, ()> {
     match expr {
-        pt::YulExpression::BoolLiteral(loc, value, ty) => resolve_bool_literal(loc, value, ty, ns),
+        pt::YulExpression::BoolLiteral(loc, value, ty) => resolve_bool_literal(loc, *value, ty, ns),
 
         pt::YulExpression::NumberLiteral(loc, base, exp, ty) => {
             resolve_number_literal(loc, base, exp, ty, ns)
@@ -97,7 +97,7 @@ fn get_type_from_big_int(big_int: &BigInt) -> Type {
 
 fn resolve_bool_literal(
     loc: &pt::Loc,
-    value: &bool,
+    value: bool,
     ty: &Option<pt::Identifier>,
     ns: &mut Namespace,
 ) -> Result<YulExpression, ()> {
@@ -115,7 +115,7 @@ fn resolve_bool_literal(
         Type::Bool
     };
 
-    Ok(YulExpression::BoolLiteral(*loc, *value, new_type))
+    Ok(YulExpression::BoolLiteral(*loc, value, new_type))
 }
 
 fn resolve_number_literal(