|
| 1 | +## Builtin rules |
| 2 | + |
| 3 | +```mooncram |
| 4 | +$ cd "$TESTDIR"/.. && moonrun "$TESTDIR"/moongrep.wasm -- scan --enable-builtin-rules testdata/builtin-rules-all |
| 5 | +testdata/builtin-rules-all/catch_all.mbt:3:3-5:4 |
| 6 | +rule: moonbitlang/catch_all |
| 7 | +description: |
| 8 | + Single catch arm handles every error, which can hide unexpected failures. |
| 9 | + Prefer matching only the specific error cases that can be recovered from. |
| 10 | +source: |
| 11 | +1 | ///| |
| 12 | +2 | fn catches_everything() -> Unit { |
| 13 | +3 > try risky() catch { |
| 14 | +4 > _ => recover() |
| 15 | +5 > } |
| 16 | +6 | } |
| 17 | +
|
| 18 | +testdata/builtin-rules-all/match_option.mbt:3:3-6:4 |
| 19 | +rule: moonbitlang/match_option |
| 20 | +description: |
| 21 | + Found an Option value handled with match over Some and None. |
| 22 | + Prefer if + is for simple Option checks. |
| 23 | +source: |
| 24 | +1 | ///| |
| 25 | +2 | fn option_match(value : Int?) -> Bool { |
| 26 | +3 > match value { |
| 27 | +4 > Some(inner) => inner > 0 |
| 28 | +5 > None => false |
| 29 | +6 > } |
| 30 | +7 | } |
| 31 | +
|
| 32 | +testdata/builtin-rules-all/inspect_number.mbt:3:3-3:26 |
| 33 | +rule: moonbitlang/inspect_number |
| 34 | +description: |
| 35 | + Found inspect() snapshots whose expected value is a plain number. |
| 36 | + Prefer numeric assertions for numeric checks. |
| 37 | +source: |
| 38 | +1 | ///| |
| 39 | +2 | fn number_snapshot() -> Unit { |
| 40 | +3 > inspect(1, content="1") |
| 41 | +4 | } |
| 42 | +
|
| 43 | +testdata/builtin-rules-all/inspect_boolean.mbt:3:3-3:32 |
| 44 | +rule: moonbitlang/inspect_boolean |
| 45 | +description: |
| 46 | + Found inspect(), debug_inspect(), or json_inspect() snapshots whose expected value is true or false. |
| 47 | + Prefer assert_true(...) or assert_false(...) for boolean checks. |
| 48 | +source: |
| 49 | +1 | ///| |
| 50 | +2 | fn boolean_snapshot(flag : Bool) -> Unit { |
| 51 | +3 > inspect(flag, content="true") |
| 52 | +4 | } |
| 53 | +
|
| 54 | +testdata/builtin-rules-all/cstyle_forward_simple_forloop.mbt:3:3-5:4 |
| 55 | +rule: moonbitlang/cstyle_forward_simple_forloop |
| 56 | +outer_loc: testdata/builtin-rules-all/cstyle_forward_simple_forloop.mbt:3:3-5:4 |
| 57 | +description: |
| 58 | + C-style forward for loops that can be rewritten as simple for-in loops. |
| 59 | +source: |
| 60 | +1 | ///| |
| 61 | +2 | fn forward_simple_loop(limit : Int) -> Unit { |
| 62 | +3 > for i = 0; i < limit; i = i + 1 { |
| 63 | +4 > tick() |
| 64 | +5 > } |
| 65 | +6 | } |
| 66 | +
|
| 67 | +testdata/builtin-rules-all/cstyle_backward_simple_forloop.mbt:3:3-5:4 |
| 68 | +rule: moonbitlang/cstyle_backward_simple_forloop |
| 69 | +outer_loc: testdata/builtin-rules-all/cstyle_backward_simple_forloop.mbt:3:3-5:4 |
| 70 | +description: |
| 71 | + C-style backward for loops that can be rewritten as simple for-in loops. |
| 72 | +source: |
| 73 | +1 | ///| |
| 74 | +2 | fn backward_simple_loop(limit : Int) -> Unit { |
| 75 | +3 > for i = limit; i > 0; i = i - 1 { |
| 76 | +4 > tick_back() |
| 77 | +5 > } |
| 78 | +6 | } |
| 79 | +
|
| 80 | +testdata/builtin-rules-all/cstyle_forward_array_iteration.mbt:4:18-4:21 |
| 81 | +rule: moonbitlang/cstyle_forward_array_iteration |
| 82 | +outer_loc: testdata/builtin-rules-all/cstyle_forward_array_iteration.mbt:3:3-5:4 |
| 83 | +description: |
| 84 | + C-style forward array iteration that can be rewritten as simple for-in loops. |
| 85 | +source: |
| 86 | +2 | fn forward_array_loop(items : Array[Int]) -> Unit { |
| 87 | +3 | for i = 0; i < items.length(); i = i + 1 { |
| 88 | +4 > consume(items[i]) |
| 89 | +5 | } |
| 90 | +6 | } |
| 91 | +
|
| 92 | +testdata/builtin-rules-all/cstyle_backward_array_iteration.mbt:4:26-4:29 |
| 93 | +rule: moonbitlang/cstyle_backward_array_iteration |
| 94 | +outer_loc: testdata/builtin-rules-all/cstyle_backward_array_iteration.mbt:3:3-5:4 |
| 95 | +description: |
| 96 | + C-style backward array iteration that can be rewritten as simple for-in loops. |
| 97 | +source: |
| 98 | +2 | fn backward_array_loop(items : Array[Int]) -> Unit { |
| 99 | +3 | for i = items.length() - 1; i >= 0; i = i - 1 { |
| 100 | +4 > consume_reverse(items[i]) |
| 101 | +5 | } |
| 102 | +6 | } |
| 103 | +``` |
0 commit comments