Repository: protofire/solhint (js)

Issue:
Low sensitivity of explicit-types
The explicit-type rule is not checked when casting a variable.

## Example

Option: `explicit`

```solidity
function test() external {
  uint256 a = uint(10);
}
```

Expected: error because of `uint(10)`

Observed: no errors



Pull request:
Enhance explicit types sensitivity
Fixes #492 

Also added new tests for `explicit-types` rule. The previous implementation fails these tests.

The rule is not bound to nodes of type `ElementaryTypeName`, as they are sometimes duplicated, which leads to unnecessary triggers.

Patch:
diff --git a/docs/rules/best-practises/explicit-types.md b/docs/rules/best-practises/explicit-types.md
index 8448bc6..cd698b2 100644
--- a/docs/rules/best-practises/explicit-types.md
+++ b/docs/rules/best-practises/explicit-types.md
@@ -48,6 +48,12 @@ uint256 public variableName
 uint public variableName
 ```
 
+#### If explicit is selected
+
+```solidity
+uint256 public variableName = uint256(5)
+```
+
 ### 👎 Examples of **incorrect** code for this rule
 
 #### If explicit is selected
@@ -62,6 +68,12 @@ uint public variableName
 uint256 public variableName
 ```
 
+#### At any setting
+
+```solidity
+uint public variableName = uint256(5)
+```
+
 ## Version
 This rule was introduced in [Solhint 3.5.1](https://github.com/protofire/solhint/tree/v3.5.1)
 
diff --git a/lib/rules/best-practises/explicit-types.js b/lib/rules/best-practises/explicit-types.js
index ebf7b60..010db85 100644
--- a/lib/rules/best-practises/explicit-types.js
+++ b/lib/rules/best-practises/explicit-types.js
@@ -35,6 +35,10 @@ const meta = {
           description: 'If implicit is selected',
           code: 'uint public variableName',
         },
+        {
+          description: 'If explicit is selected',
+          code: 'uint256 public variableName = uint256(5)',
+        },
       ],
       bad: [
         {
@@ -45,6 +49,10 @@ const meta = {
           description: 'If implicit is selected',
           code: 'uint256 public variableName',
         },
+        {
+          description: 'At any setting',
+          code: 'uint public variableName = uint256(5)',
+        },
       ],
     },
   },
@@ -68,6 +76,19 @@ class ExplicitTypesChecker extends BaseChecker {
   }
 
   VariableDeclaration(node) {
+    this.validateInNode(node)
+  }
+
+  VariableDeclarationStatement(node) {
+    if (!node.initialValue) return
+    this.validateInNode(node.initialValue)
+  }
+
+  ExpressionStatement(node) {
+    this.validateInNode(node)
+  }
+
+  validateInNode(node) {
     if (!VALID_CONFIGURATION_OPTIONS.includes(this.configOption)) {
       this.error(node, 'Error: Config error on [explicit-types]. See explicit-types.md.')
       return


Test patch:
diff --git a/test/fixtures/best-practises/explicit-types.js b/test/fixtures/best-practises/explicit-types.js
index b63c483..925b879 100644
--- a/test/fixtures/best-practises/explicit-types.js
+++ b/test/fixtures/best-practises/explicit-types.js
@@ -119,6 +119,12 @@ const VAR_DECLARATIONS = {
     errorsExplicit: 1,
   },
 
+  fixedArrayCastInDeclaration: {
+    code: 'uint[] public arr = [uint(1),2,3];',
+    errorsImplicit: 0,
+    errorsExplicit: 2,
+  },
+
   fixedArrayOfArrays: {
     code: 'uint256[] public arr = [[1,2,3]];',
     errorsImplicit: 1,
@@ -130,6 +136,42 @@ const VAR_DECLARATIONS = {
     errorsImplicit: 1,
     errorsExplicit: 1,
   },
+
+  castInStateVariableDeclaration: {
+    code: 'uint256 public varUint256 = uint256(1); uint public varUint = uint(1);',
+    errorsImplicit: 2,
+    errorsExplicit: 2,
+  },
+
+  castInsideFunctionAtDeclarationUint256: {
+    code: 'function withUint256() external { uint256 varUint256 = uint256(1); }',
+    errorsImplicit: 2,
+    errorsExplicit: 0,
+  },
+
+  castInsideFunctionAtDeclarationUint: {
+    code: 'function withUint() external { uint varUint = uint(1); }',
+    errorsImplicit: 0,
+    errorsExplicit: 2,
+  },
+
+  castInsideFunctionUint: {
+    code: 'function withUint() external { uint varUint; varUint = uint(1);}',
+    errorsImplicit: 0,
+    errorsExplicit: 2,
+  },
+
+  castInsideFunctionUint256: {
+    code: 'function withUint256() external { uint256 varUint; varUint = uint256(1);}',
+    errorsImplicit: 2,
+    errorsExplicit: 0,
+  },
+
+  castInsideModifier: {
+    code: 'modifier withUint256() { _; uint256 varUint; varUint = uint256(1);}',
+    errorsImplicit: 2,
+    errorsExplicit: 0,
+  },
 }
 
 module.exports = VAR_DECLARATIONS