Pārlūkot izejas kodu

Fix rust 1.66 clippy warnings and run crate doc tests (#1102)

Signed-off-by: Sean Young <sean@mess.org>
Sean Young 2 gadi atpakaļ
vecāks
revīzija
603f657a5c

+ 4 - 1
.github/workflows/test.yml

@@ -33,7 +33,10 @@ jobs:
         run: cargo fmt --all -- --check
       - name: Run cargo clippy
         if: always()
-        run: cargo clippy --workspace --tests --bins -- -D warnings -D clippy::inconsistent-struct-constructor
+        run: cargo clippy --workspace --tests --bins -- -D warnings
+      - name: Run cargo doc
+        if: always()
+        run: cargo doc --workspace --bins
 
   docs:
     name: Docs

+ 1 - 1
src/codegen/cfg.rs

@@ -133,7 +133,7 @@ pub enum Instr {
         gas: Expression,
         callty: CallTy,
     },
-    /// Value transfer; either <address>.send() or <address>.transfer()
+    /// Value transfer; either address.send() or address.transfer()
     ValueTransfer {
         success: Option<usize>,
         address: Expression,

+ 1 - 1
src/codegen/expression.rs

@@ -1926,7 +1926,7 @@ fn interfaceid(ns: &Namespace, contract_no: &usize, loc: &pt::Loc) -> Expression
             }
         }
     }
-    Expression::BytesLiteral(*loc, Type::Bytes(selector_len as u8), id.to_vec())
+    Expression::BytesLiteral(*loc, Type::Bytes(selector_len), id.to_vec())
 }
 
 pub fn assign_single(

+ 2 - 3
src/file_resolver.rs

@@ -134,13 +134,12 @@ impl FileResolver {
         // first check maps
         let mut iter = path.iter();
         if let Some(first_part) = iter.next() {
-            let relpath: PathBuf = iter.collect();
+            let relpath: &PathBuf = &iter.collect();
 
             for (import_no, import) in self.import_paths.iter().enumerate() {
                 if let (Some(mapping), import_path) = import {
                     if first_part == mapping {
-                        // match!
-                        if let Ok(full_path) = import_path.join(&relpath).canonicalize() {
+                        if let Ok(full_path) = import_path.join(relpath).canonicalize() {
                             self.load_file(&full_path)?;
                             let base = full_path
                                 .parent()

+ 20 - 11
src/sema/expression.rs

@@ -24,7 +24,6 @@ use num_rational::BigRational;
 use num_traits::{FromPrimitive, Num, One, Pow, ToPrimitive, Zero};
 use solang_parser::pt::{self, CodeLocation, Loc};
 use std::{
-    cmp,
     cmp::Ordering,
     collections::{BTreeMap, HashMap},
     ops::{Mul, Shl, Sub},
@@ -515,7 +514,7 @@ impl Expression {
                     }),
                 }
             }
-            (Type::Bytes(n), Type::FunctionSelector) if *n == ns.target.selector_length() as u8 => {
+            (Type::Bytes(n), Type::FunctionSelector) if *n == ns.target.selector_length() => {
                 Ok(Expression::Cast {
                     loc: *loc,
                     to: to.clone(),
@@ -1247,7 +1246,7 @@ impl Expression {
                 })
             }
             (Type::FunctionSelector, Type::Bytes(n)) => {
-                let selector_length = ns.target.selector_length() as u8;
+                let selector_length = ns.target.selector_length();
                 if *n == selector_length {
                     Ok(Expression::Cast {
                         loc: *loc,
@@ -1287,7 +1286,7 @@ impl Expression {
                 }
                 self.cast_types(
                     loc,
-                    &Type::Bytes(ns.target.selector_length() as u8),
+                    &Type::Bytes(ns.target.selector_length()),
                     to,
                     implicit,
                     ns,
@@ -1559,10 +1558,20 @@ pub fn coerce_number(
     let (right_len, right_signed) = get_int_length(r, r_loc, false, ns, diagnostics)?;
 
     Ok(match (left_signed, right_signed) {
-        (true, true) => Type::Int(cmp::max(left_len, right_len)),
-        (false, false) => Type::Uint(cmp::max(left_len, right_len)),
-        (true, false) => Type::Int(cmp::max(left_len, cmp::min(right_len + 8, 256))),
-        (false, true) => Type::Int(cmp::max(cmp::min(left_len + 8, 256), right_len)),
+        (true, true) => Type::Int(left_len.max(right_len)),
+        (false, false) => Type::Uint(left_len.max(right_len)),
+        (true, false) => {
+            // uint8 fits into int16
+            let len = left_len.max(right_len + 8);
+
+            Type::Int(len.min(256))
+        }
+        (false, true) => {
+            // uint8 fits into int16
+            let len = (left_len + 8).max(right_len);
+
+            Type::Int(len.min(256))
+        }
     })
 }
 
@@ -7218,7 +7227,7 @@ fn array_literal(
     diagnostics: &mut Diagnostics,
     resolve_to: ResolveTo,
 ) -> Result<Expression, ()> {
-    let mut dims = Box::new(Vec::new());
+    let mut dims = Vec::new();
     let mut flattened = Vec::new();
 
     let resolve_to = match resolve_to {
@@ -7345,9 +7354,9 @@ fn array_literal(
     );
 
     if context.constant {
-        Ok(Expression::ConstArrayLiteral(*loc, aty, *dims, exprs))
+        Ok(Expression::ConstArrayLiteral(*loc, aty, dims, exprs))
     } else {
-        Ok(Expression::ArrayLiteral(*loc, aty, *dims, exprs))
+        Ok(Expression::ArrayLiteral(*loc, aty, dims, exprs))
     }
 }
 

+ 1 - 1
src/sema/types.rs

@@ -1262,7 +1262,7 @@ impl Type {
             Type::Int(n) => *n as u8,
             Type::Uint(n) => *n as u8,
             Type::Rational => unreachable!(),
-            Type::Bytes(n) => *n as u8,
+            Type::Bytes(n) => *n,
             Type::Enum(n) => ns.enums[*n].ty.bytes(ns),
             Type::Value => ns.value_length as u8,
             Type::StorageRef(..) => ns.storage_type().bytes(ns),