|
| 1 | +--- |
| 2 | +warning: "This is a dynamically generated file. Do not edit manually." |
| 3 | +layout: "default" |
| 4 | +title: "use-natspec | Solhint" |
| 5 | +--- |
| 6 | + |
| 7 | +# use-natspec |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +## Description |
| 12 | +Enforces the presence and correctness of NatSpec tags. |
| 13 | + |
| 14 | +## Options |
| 15 | +This rule accepts an array of options: |
| 16 | + |
| 17 | +| Index | Description | Default Value | |
| 18 | +| ----- | ------------------------------------------------------------------------ | -------------------- | |
| 19 | +| 0 | Rule severity. Must be one of "error", "warn", "off". | warn | |
| 20 | +| 1 | A JSON object with natspec properties. See EXAMPLE CONFIG section below. | Check EXAMPLE CONFIG | |
| 21 | + |
| 22 | + |
| 23 | +### Example Config |
| 24 | +```json |
| 25 | +{ |
| 26 | + "rules": { |
| 27 | + "use-natspec": [ |
| 28 | + "warn", |
| 29 | + { |
| 30 | + "title": { |
| 31 | + "enabled": true, |
| 32 | + "ignore": {} |
| 33 | + }, |
| 34 | + "author": { |
| 35 | + "enabled": true, |
| 36 | + "ignore": {} |
| 37 | + }, |
| 38 | + "notice": { |
| 39 | + "enabled": true, |
| 40 | + "ignore": {} |
| 41 | + }, |
| 42 | + "param": { |
| 43 | + "enabled": true, |
| 44 | + "ignore": {} |
| 45 | + }, |
| 46 | + "return": { |
| 47 | + "enabled": true, |
| 48 | + "ignore": {} |
| 49 | + } |
| 50 | + } |
| 51 | + ] |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +### Notes |
| 57 | +- If a function or return value has unnamed parameters (e.g. `function foo(uint256)`), the rule only checks the number of `@param` or `@return` tags, not their names. |
| 58 | +- If a function or variable has `@inheritdoc`, the rule skips the validation. |
| 59 | +- The rule supports both `///` and `/** */` style NatSpec comments. |
| 60 | +- If a custom config is provided, it is merged with the default config. Only the overridden fields need to be specified. |
| 61 | + |
| 62 | +## Examples |
| 63 | +### 👍 Examples of **correct** code for this rule |
| 64 | + |
| 65 | +#### Contract with valid NatSpec |
| 66 | + |
| 67 | +```solidity |
| 68 | +
|
| 69 | + /// @title Token contract |
| 70 | + /// @author Me |
| 71 | + /// @notice This contract handles tokens |
| 72 | + contract Token { |
| 73 | + /// @notice Transfers tokens |
| 74 | + /// @param to The recipient address |
| 75 | + /// @param amount The amount to transfer |
| 76 | + /// @return success Whether the transfer succeeded |
| 77 | + function transfer(address to, uint256 amount) public returns (bool success) { |
| 78 | + return true; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | +``` |
| 83 | + |
| 84 | +#### You can disable specific tags globally or by type/name using the `ignore` option in the config. For example: |
| 85 | + |
| 86 | +```solidity |
| 87 | +{ |
| 88 | + "use-natspec": [ |
| 89 | + "warn", |
| 90 | + { |
| 91 | + "title": { |
| 92 | + "enabled": true, |
| 93 | + "ignore": { |
| 94 | + "contract": ["MyContract"], |
| 95 | + "*": ["LegacyContract"] |
| 96 | + } |
| 97 | + }, |
| 98 | + "param": { "enabled": false } |
| 99 | + } |
| 100 | + ] |
| 101 | + } |
| 102 | +``` |
| 103 | + |
| 104 | +#### The default configuration enables all checks with no ignore rules: |
| 105 | + |
| 106 | +```solidity |
| 107 | +{ |
| 108 | + "title": { "enabled": true, "ignore": {} }, |
| 109 | + "author": { "enabled": true, "ignore": {} }, |
| 110 | + "notice": { "enabled": true, "ignore": {} }, |
| 111 | + "param": { "enabled": true, "ignore": {} }, |
| 112 | + "return": { "enabled": true, "ignore": {} } |
| 113 | + } |
| 114 | +``` |
| 115 | + |
| 116 | +### 👎 Examples of **incorrect** code for this rule |
| 117 | + |
| 118 | +#### Missing @notice and incorrect @param and @return tags |
| 119 | + |
| 120 | +```solidity |
| 121 | +
|
| 122 | + /// @title Token contract |
| 123 | + contract Token { |
| 124 | + /// @param wrongParam Not matching actual parameter |
| 125 | + function transfer(address to) public returns (bool) { |
| 126 | + return true; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | +``` |
| 131 | + |
| 132 | +## Version |
| 133 | +This rule was introduced in the latest version. |
| 134 | + |
| 135 | +## Resources |
| 136 | +- [Rule source](https://github.com/protofire/solhint/blob/master/lib/rules/best-practices/use-natspec.js) |
| 137 | +- [Document source](https://github.com/protofire/solhint/blob/master/docs/rules/best-practices/use-natspec.md) |
| 138 | +- [Test cases](https://github.com/protofire/solhint/blob/master/test/rules/best-practices/use-natspec.js) |
0 commit comments