Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Rule: explicit-types #467

Merged
merged 3 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
## [3.4.2] - 2023-08-04
### Updated
- Support `ignoreConstructors` option for `no-empty-blocks` [#418](https://github.com/protofire/solhint/pull/418)
- Bump json5 from 2.1.3 to 2.2.3 [#376](https://github.com/protofire/solhint/pull/376)
- Bump json-schema and jsprim [#370](https://github.com/protofire/solhint/pull/370)
- Bump semver from 6.3.0 to 7.5.2 [#438](https://github.com/protofire/solhint/pull/438)
- Corrected "Category" of `quotes` rule, added default rules list on readme [#443](https://github.com/protofire/solhint/pull/443)
- 'Deprecated' column on `rules.md`` [#444](https://github.com/protofire/solhint/pull/444)
- Information about maxCharacters allowed on `reason-string` rule [#446](https://github.com/protofire/solhint/pull/446)
- E2E tests for `max-warnings` [#455](https://github.com/protofire/solhint/pull/455)
- Replaced blacklist and whitelist words [#459](https://github.com/protofire/solhint/pull/459)
### Added
- New Rule: No unused imports [#417](https://github.com/protofire/solhint/pull/417)
- New Rule: To treat immutable as constants [#458](https://github.com/protofire/solhint/pull/458)
- New Rule: Explicit-types. To forbid/enforce full type or alias for variables declaration [#467](https://github.com/protofire/solhint/pull/467)
- JSON formatter support [#440](https://github.com/protofire/solhint/pull/440)
- Rules List with `list-rules` command [#449](https://github.com/protofire/solhint/pull/449)
- E2E tests for formatters and new `Compact formatter` [#457](https://github.com/protofire/solhint/pull/457)

### Fixed
- `maxWarnings` parameter waiting review [#439](https://github.com/protofire/solhint/pull/439)
- `–fix` option not working in avoid-throw rule [#442](https://github.com/protofire/solhint/pull/442)
- Formatter option fixed for `stdin` command [#450](https://github.com/protofire/solhint/pull/450)


<br><br>
## [3.4.1] - 2023-03-06
### Updated
- Updated solidity parser to 0.16.0 [#420](https://github.com/protofire/solhint/pull/420)
Expand Down
63 changes: 32 additions & 31 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ title: "Rule Index of Solhint"

## Best Practise Rules

| Rule Id | Error | Recommended |
| ------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------- | ----------- |
| [code-complexity](./rules/best-practises/code-complexity.md) | Function has cyclomatic complexity "current" but allowed no more than maxcompl. | |
| [function-max-lines](./rules/best-practises/function-max-lines.md) | Function body contains "count" lines but allowed no more than maxlines. | |
| [max-line-length](./rules/best-practises/max-line-length.md) | Line length must be no more than maxlen. | |
| [max-states-count](./rules/best-practises/max-states-count.md) | Contract has "some count" states declarations but allowed no more than maxstates. | ✔️ |
| [no-console](./rules/best-practises/no-console.md) | No console.log/logInt/logBytesX/logString/etc & No hardhat and forge-std console.sol import statements. | ✔️ |
| [no-empty-blocks](./rules/best-practises/no-empty-blocks.md) | Code contains empty block. | ✔️ |
| [no-global-import](./rules/best-practises/no-global-import.md) | Import statement includes an entire file instead of selected symbols. | ✔️ |
| [no-unused-import](./rules/best-practises/no-unused-import.md) | Imported name is not used. | ✔️ |
| [no-unused-vars](./rules/best-practises/no-unused-vars.md) | Variable "name" is unused. | ✔️ |
| [payable-fallback](./rules/best-practises/payable-fallback.md) | When fallback is not payable you will not be able to receive ethers. | ✔️ |
| [reason-string](./rules/best-practises/reason-string.md) | Require or revert statement must have a reason string and check that each reason string is at most N characters long. | ✔️ |
| [constructor-syntax](./rules/best-practises/constructor-syntax.md) | Constructors should use the new constructor keyword. | |
| Rule Id | Error | Recommended | Deprecated |
| ------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------- | ------------ | ---------- |
| [code-complexity](./rules/best-practises/code-complexity.md) | Function has cyclomatic complexity "current" but allowed no more than maxcompl. | | |
| [explicit-types](./rules/best-practises/explicit-types.md) | Forbid or enforce explicit types (like uint256) that have an alias (like uint). | $~~~~~~~~$✔️ | |
| [function-max-lines](./rules/best-practises/function-max-lines.md) | Function body contains "count" lines but allowed no more than maxlines. | | |
| [max-line-length](./rules/best-practises/max-line-length.md) | Line length must be no more than maxlen. | | |
| [max-states-count](./rules/best-practises/max-states-count.md) | Contract has "some count" states declarations but allowed no more than maxstates. | $~~~~~~~~$✔️ | |
| [no-console](./rules/best-practises/no-console.md) | No console.log/logInt/logBytesX/logString/etc & No hardhat and forge-std console.sol import statements. | $~~~~~~~~$✔️ | |
| [no-empty-blocks](./rules/best-practises/no-empty-blocks.md) | Code block has zero statements inside. Exceptions apply. | $~~~~~~~~$✔️ | |
| [no-global-import](./rules/best-practises/no-global-import.md) | Import statement includes an entire file instead of selected symbols. | $~~~~~~~~$✔️ | |
| [no-unused-import](./rules/best-practises/no-unused-import.md) | Imported name is not used. | $~~~~~~~~$✔️ | |
| [no-unused-vars](./rules/best-practises/no-unused-vars.md) | Variable "name" is unused. | $~~~~~~~~$✔️ | |
| [payable-fallback](./rules/best-practises/payable-fallback.md) | When fallback is not payable you will not be able to receive ethers. | $~~~~~~~~$✔️ | |
| [reason-string](./rules/best-practises/reason-string.md) | Require or revert statement must have a reason string and check that each reason string is at most N characters long. | $~~~~~~~~$✔️ | |
| [constructor-syntax](./rules/best-practises/constructor-syntax.md) | Constructors should use the new constructor keyword. | | |


## Miscellaneous
Expand All @@ -32,23 +33,23 @@ title: "Rule Index of Solhint"

## Style Guide Rules

| Rule Id | Error | Recommended |
| ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------- | ----------- |
| [const-name-snakecase](./rules/naming/const-name-snakecase.md) | Constant name must be in capitalized SNAKE_CASE. (Does not check IMMUTABLES, use immutable-vars-naming) | ✔️ |
| [contract-name-camelcase](./rules/naming/contract-name-camelcase.md) | Contract name must be in CamelCase. | ✔️ |
| [event-name-camelcase](./rules/naming/event-name-camelcase.md) | Event name must be in CamelCase. | ✔️ |
| [func-name-mixedcase](./rules/naming/func-name-mixedcase.md) | Function name must be in mixedCase. | ✔️ |
| [func-param-name-mixedcase](./rules/naming/func-param-name-mixedcase.md) | Function param name must be in mixedCase. | |
| [immutable-vars-naming](./rules/naming/immutable-vars-naming.md) | Check Immutable variables. Capitalized SNAKE_CASE or mixedCase depending on configuration. | ✔️ |
| [modifier-name-mixedcase](./rules/naming/modifier-name-mixedcase.md) | Modifier name must be in mixedCase. | |
| [named-parameters-mapping](./rules/naming/named-parameters-mapping.md) | Solidity v0.8.18 introduced named parameters on the mappings definition. | |
| [private-vars-leading-underscore](./rules/naming/private-vars-leading-underscore.md) | Private and internal names must start with a single underscore. | |
| [use-forbidden-name](./rules/naming/use-forbidden-name.md) | Avoid to use letters 'I', 'l', 'O' as identifiers. | ✔️ |
| [var-name-mixedcase](./rules/naming/var-name-mixedcase.md) | Variable name must be in mixedCase. (Does not check IMMUTABLES, use immutable-vars-naming) | ✔️ |
| [func-order](./rules/order/func-order.md) | Function order is incorrect. | |
| [imports-on-top](./rules/order/imports-on-top.md) | Import statements must be on top. | ✔️ |
| [ordering](./rules/order/ordering.md) | Check order of elements in file and inside each contract, according to the style guide. | |
| [visibility-modifier-order](./rules/order/visibility-modifier-order.md) | Visibility modifier must be first in list of modifiers. | ✔️ |
| Rule Id | Error | Recommended | Deprecated |
| ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------- | ------------ | ----------- |
| [const-name-snakecase](./rules/naming/const-name-snakecase.md) | Constant name must be in capitalized SNAKE_CASE. (Does not check IMMUTABLES, use immutable-vars-naming) | $~~~~~~~~$✔️ | |
| [contract-name-camelcase](./rules/naming/contract-name-camelcase.md) | Contract name must be in CamelCase. | $~~~~~~~~$✔️ | |
| [event-name-camelcase](./rules/naming/event-name-camelcase.md) | Event name must be in CamelCase. | $~~~~~~~~$✔️ | |
| [func-name-mixedcase](./rules/naming/func-name-mixedcase.md) | Function name must be in mixedCase. | $~~~~~~~~$✔️ | |
| [func-param-name-mixedcase](./rules/naming/func-param-name-mixedcase.md) | Function param name must be in mixedCase. | | |
| [immutable-vars-naming](./rules/naming/immutable-vars-naming.md) | Check Immutable variables. Capitalized SNAKE_CASE or mixedCase depending on configuration. | $~~~~~~~~$✔️ | |
| [modifier-name-mixedcase](./rules/naming/modifier-name-mixedcase.md) | Modifier name must be in mixedCase. | | |
| [named-parameters-mapping](./rules/naming/named-parameters-mapping.md) | Solidity v0.8.18 introduced named parameters on the mappings definition. | | |
| [private-vars-leading-underscore](./rules/naming/private-vars-leading-underscore.md) | Private and internal names must start with a single underscore. | | |
| [use-forbidden-name](./rules/naming/use-forbidden-name.md) | Avoid to use letters 'I', 'l', 'O' as identifiers. | $~~~~~~~~$✔️ | |
| [var-name-mixedcase](./rules/naming/var-name-mixedcase.md) | Variable name must be in mixedCase. (Does not check IMMUTABLES, use immutable-vars-naming) | $~~~~~~~~$✔️ | |
| [func-order](./rules/order/func-order.md) | Function order is incorrect. | | $~~~~~~~$✔️ |
| [imports-on-top](./rules/order/imports-on-top.md) | Import statements must be on top. | $~~~~~~~~$✔️ | |
| [ordering](./rules/order/ordering.md) | Check order of elements in file and inside each contract, according to the style guide. | | |
| [visibility-modifier-order](./rules/order/visibility-modifier-order.md) | Visibility modifier must be first in list of modifiers. | $~~~~~~~~$✔️ | |


## Security Rules
Expand Down
71 changes: 71 additions & 0 deletions docs/rules/best-practises/explicit-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
warning: "This is a dynamically generated file. Do not edit manually."
layout: "default"
title: "explicit-types | Solhint"
---

# explicit-types
![Recommended Badge](https://img.shields.io/badge/-Recommended-brightgreen)
![Category Badge](https://img.shields.io/badge/-Best%20Practise%20Rules-informational)
![Default Severity Badge warn](https://img.shields.io/badge/Default%20Severity-warn-yellow)
> The {"extends": "solhint:recommended"} property in a configuration file enables this rule.

## Description
Forbid or enforce explicit types (like uint256) that have an alias (like uint).

## Options
This rule accepts an array of options:

| Index | Description | Default Value |
| ----- | ----------------------------------------------------- | ------------- |
| 0 | Rule severity. Must be one of "error", "warn", "off". | warn |
| 1 | Options need to be one of "explicit", "implicit" | explicit |


### Example Config
```json
{
"rules": {
"explicit-types": ["warn","explicit"]
}
}
```


## Examples
### 👍 Examples of **correct** code for this rule

#### If explicit is selected

```solidity
uint256 public variableName
```

#### If implicit is selected

```solidity
uint public variableName
```

### 👎 Examples of **incorrect** code for this rule

#### If explicit is selected

```solidity
uint public variableName
```

#### If implicit is selected

```solidity
uint256 public variableName
```

## Version
This rule is introduced in the latest version.

## Resources
- [Rule source](https://github.com/protofire/solhint/tree/master/lib/rules/best-practises/explicit-types.js)
- [Document source](https://github.com/protofire/solhint/tree/master/docs/rules/best-practises/explicit-types.md)
- [Test cases](https://github.com/protofire/solhint/tree/master/test/rules/best-practises/explicit-types.js)
36 changes: 34 additions & 2 deletions docs/rules/best-practises/no-empty-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ title: "no-empty-blocks | Solhint"


## Description
Code contains empty block.
Code block has zero statements inside. Exceptions apply.

## Options
This rule accepts a string option of rule severity. Must be one of "error", "warn", "off". Default to warn.
Expand All @@ -28,7 +28,39 @@ This rule accepts a string option of rule severity. Must be one of "error", "war


## Examples
This rule does not have examples.
### 👍 Examples of **correct** code for this rule

#### Empty fallback function

```solidity
fallback () external { }
```

#### Empty constructor with member initialization list

```solidity
constructor(uint param) Foo(param) Bar(param*2) { }
```

### 👎 Examples of **incorrect** code for this rule

#### Empty block on if statement

```solidity
if (condition) { }
```

#### Empty contract

```solidity
contract Foo { }
```

#### Empty block in constructor without parent initialization

```solidity
constructor () { }
```

## Version
This rule was introduced in [Solhint 1.1.5](https://github.com/protofire/solhint/tree/v1.1.5)
Expand Down
6 changes: 3 additions & 3 deletions lib/common/identifier-naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ function match(text, regex) {

module.exports = {
isMixedCase(text) {
return match(text, /[_]*[a-z]+[a-zA-Z0-9$]*[_]?/)
return match(text, /[_]*[a-z$]+[a-zA-Z0-9$]*[_]?/)
},

isNotMixedCase(text) {
return !this.isMixedCase(text)
},

isCamelCase(text) {
return match(text, /[A-Z]+[a-zA-Z0-9$]*/)
return match(text, /[A-Z$]+[a-zA-Z0-9$]*/)
},

isNotCamelCase(text) {
return !this.isCamelCase(text)
},

isUpperSnakeCase(text) {
return match(text, /_{0,2}[A-Z0-9]+[_A-Z0-9]*/)
return match(text, /_{0,2}[A-Z0-9$]+[_A-Z0-9$]*/)
},

isNotUpperSnakeCase(text) {
Expand Down
Loading