Skip to content

Commit 8535f39

Browse files
committed
fix antipatterns
1 parent 870681e commit 8535f39

27 files changed

Lines changed: 655 additions & 646 deletions

cli/render.mbt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ fn render_skipped_file_warnings(skipped_files : Array[SkippedFile]) -> String {
3636

3737
///|
3838
fn render_skipped_file_warning(skipped : SkippedFile) -> String {
39-
match skipped.block_start_line {
40-
Some(line) =>
41-
"warning: skipping \{skipped.file} block starting at line \{line}: \{skipped.reason}"
42-
None => "warning: skipping \{skipped.file}: \{skipped.reason}"
39+
if skipped.block_start_line is Some(line) {
40+
"warning: skipping \{skipped.file} block starting at line \{line}: \{skipped.reason}"
41+
} else {
42+
"warning: skipping \{skipped.file}: \{skipped.reason}"
4343
}
4444
}
4545

@@ -226,7 +226,7 @@ fn format_location(file : String, loc : @basic.Location) -> String {
226226
fn trim_trailing_newlines(input : String) -> String {
227227
let mut end = input.length()
228228
while end > 0 && input[end - 1:end].to_owned() == "\n" {
229-
end = end - 1
229+
end -= 1
230230
}
231231
input[:end].to_owned()
232232
}

cli/render_wbtest.mbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ async test "matched source line prefix is not dimmed" {
5252
content="12 | let value = \u{1b}[93mtarget()\u{1b}[39m",
5353
)
5454
let arr = [5, 6, 7]
55-
for i = 0; i < arr.length(); i = i + 1 {
56-
if arr[i] == 0 {
55+
for i, value in arr {
56+
if value == 0 {
5757
@stdio.stdout.write("\{i}\n")
5858
}
5959
}

cli/scan_wbtest.mbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ async test "scan excludes configured rule ids before planning" {
333333
async test "scan rejects unknown exclude rule ids" {
334334
try collect_directory_hits(unknown_exclude_rule_scan_options()) catch {
335335
CliError::Usage(message~, exit_code=found) => {
336-
inspect(found, content="2")
336+
assert_eq(found, 2)
337337
inspect(message, content="unknown rule id in --exclude-rule: missing")
338338
}
339339
err => fail("unexpected error \{err}")

main.mbt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ extern "c" fn runtime_exit(code : Int) -> Unit = "exit"
99
///|
1010
async fn write_cli_output(output : String) -> Unit {
1111
if output != "" {
12-
match output.strip_suffix("\n") {
13-
Some(_) => @stdio.stdout.write(output)
14-
None => @stdio.stdout.write(output + "\n")
12+
if output.strip_suffix("\n") is Some(_) {
13+
@stdio.stdout.write(output)
14+
} else {
15+
@stdio.stdout.write(output + "\n")
1516
}
1617
}
1718
}

matching/ellipsis.mbt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ fn ellipsis_item_raw(item : @untyped_ast.Node) -> String? {
5757
matching_ellipsis_direct_raw(pattern)
5858
}
5959
ArrayPattern_Pattern | SpreadableElem_Regular =>
60-
match child(item, "value") {
61-
Some(value) => matching_ellipsis_direct_raw(value)
62-
None => None
60+
if child(item, "value") is Some(value) {
61+
matching_ellipsis_direct_raw(value)
62+
} else {
63+
None
6364
}
6465
Parameter_Positional => {
6566
guard child(item, "binder") is Some(binder) else { return None }
@@ -77,9 +78,10 @@ fn matching_ellipsis_direct_raw(node : @untyped_ast.Node) -> String? {
7778
let raw = match node.kind {
7879
Expr_Ident => node.normalized_expr_identifier_name()
7980
Pattern_Var =>
80-
match child(node, "value") {
81-
Some(binder) => leaf_string_child(binder, "name")
82-
None => None
81+
if child(node, "value") is Some(binder) {
82+
leaf_string_child(binder, "name")
83+
} else {
84+
None
8385
}
8486
Binder => leaf_string_child(node, "name")
8587
Type_Name => type_placeholder_name(node)

matching/matching.mbt

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,18 @@ fn match_expr_placeholder(
181181
contains_name(compiled.expr_metavars, name) {
182182
Some(bind_value(bindings, name, candidate))
183183
} else if contains_name(compiled.identifier_metavars, name) {
184-
match candidate.normalized_expr_identifier_name() {
185-
Some(candidate_name) =>
186-
Some(
187-
bind_value(bindings, name, string_binding(candidate_name, candidate)),
188-
)
189-
None => Some(false)
184+
if candidate.normalized_expr_identifier_name() is Some(candidate_name) {
185+
Some(
186+
bind_value(bindings, name, string_binding(candidate_name, candidate)),
187+
)
188+
} else {
189+
Some(false)
190190
}
191191
} else if contains_name(compiled.constant_metavars, name) {
192-
match expr_constant(candidate) {
193-
Some(constant) => Some(bind_value(bindings, name, constant))
194-
None => Some(false)
192+
if expr_constant(candidate) is Some(constant) {
193+
Some(bind_value(bindings, name, constant))
194+
} else {
195+
Some(false)
195196
}
196197
} else {
197198
None
@@ -216,17 +217,18 @@ fn match_pattern_placeholder(
216217
} else if is_ignore_placeholder(name) {
217218
Some(true)
218219
} else if contains_name(compiled.identifier_metavars, name) {
219-
match candidate.normalized_pattern_identifier_name() {
220-
Some(candidate_name) =>
221-
Some(
222-
bind_value(bindings, name, string_binding(candidate_name, candidate)),
223-
)
224-
None => Some(false)
220+
if candidate.normalized_pattern_identifier_name() is Some(candidate_name) {
221+
Some(
222+
bind_value(bindings, name, string_binding(candidate_name, candidate)),
223+
)
224+
} else {
225+
Some(false)
225226
}
226227
} else if contains_name(compiled.constant_metavars, name) {
227-
match pattern_constant(candidate) {
228-
Some(constant) => Some(bind_value(bindings, name, constant))
229-
None => Some(false)
228+
if pattern_constant(candidate) is Some(constant) {
229+
Some(bind_value(bindings, name, constant))
230+
} else {
231+
Some(false)
230232
}
231233
} else {
232234
None
@@ -783,35 +785,35 @@ fn constructor_name(node : @untyped_ast.Node) -> String? {
783785
///|
784786
fn normalized_constructor(constr : @untyped_ast.Node) -> String {
785787
let name = constructor_name(constr).unwrap_or("")
786-
match child(constr, "extra_info") {
787-
Some(extra) =>
788-
match extra.kind {
789-
ConstructorExtraInfo_NoExtraInfo => name
790-
ConstructorExtraInfo_Package =>
791-
if leaf_string_child(extra, "value") is Some(pkg) {
792-
"@\{pkg}.\{name}"
788+
if child(constr, "extra_info") is Some(extra) {
789+
match extra.kind {
790+
ConstructorExtraInfo_NoExtraInfo => name
791+
ConstructorExtraInfo_Package =>
792+
if leaf_string_child(extra, "value") is Some(pkg) {
793+
"@\{pkg}.\{name}"
794+
} else {
795+
name
796+
}
797+
ConstructorExtraInfo_TypeName =>
798+
if child(extra, "value") is Some(type_name) {
799+
"\{normalized_type_name(type_name)}::\{name}"
800+
} else {
801+
name
802+
}
803+
ConstructorExtraInfo_TypeNameWithConstrPackage =>
804+
if child(extra, "type_name") is Some(type_name) {
805+
if leaf_string_child(extra, "pkg") is Some(pkg) {
806+
"\{normalized_type_name(type_name)}::@\{pkg}.\{name}"
793807
} else {
794-
name
795-
}
796-
ConstructorExtraInfo_TypeName =>
797-
if child(extra, "value") is Some(type_name) {
798808
"\{normalized_type_name(type_name)}::\{name}"
799-
} else {
800-
name
801809
}
802-
ConstructorExtraInfo_TypeNameWithConstrPackage =>
803-
if child(extra, "type_name") is Some(type_name) {
804-
if leaf_string_child(extra, "pkg") is Some(pkg) {
805-
"\{normalized_type_name(type_name)}::@\{pkg}.\{name}"
806-
} else {
807-
"\{normalized_type_name(type_name)}::\{name}"
808-
}
809-
} else {
810-
name
811-
}
812-
_ => name
813-
}
814-
None => name
810+
} else {
811+
name
812+
}
813+
_ => name
814+
}
815+
} else {
816+
name
815817
}
816818
}
817819

matching/matching_test.mbt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -371,13 +371,13 @@ test "argument metavar captures complete argument nodes" {
371371
source in [
372372
"sink(value)", "sink(label=value)", "sink(label~)", "sink(label?=value)", "sink(label?)",
373373
] {
374-
match match_expr_pattern(compiled, parse_expr(source)) {
375-
Some(result) =>
376-
match result.bindings.get("arg") {
377-
Some(Single(node)) => assert_true(node.kind == Argument)
378-
_ => fail("expected argument binding")
379-
}
380-
None => fail("expected argument metavar to match \{source}")
374+
if match_expr_pattern(compiled, parse_expr(source)) is Some(result) {
375+
match result.bindings.get("arg") {
376+
Some(Single(node)) => assert_true(node.kind == Argument)
377+
_ => fail("expected argument binding")
378+
}
379+
} else {
380+
fail("expected argument metavar to match \{source}")
381381
}
382382
}
383383
assert_false(match_expr_pattern(compiled, parse_expr("sink()")) is Some(_))

matching/untyped_matching_test.mbt

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,10 @@ test "default matcher binds repeated expression metavars" {
193193
///|
194194
test "default matcher treats only $_ as ignore placeholder" {
195195
let compiled = untyped_compiled(parse_untyped_test_metavar_node("foo($_)"))
196-
match untyped_match(compiled, "foo(make())") {
197-
Some(result) => inspect(result.bindings.length(), content="0")
198-
None => fail("expected $_ ignore placeholder to match")
196+
if untyped_match(compiled, "foo(make())") is Some(result) {
197+
assert_eq(result.bindings.length(), 0)
198+
} else {
199+
fail("expected $_ ignore placeholder to match")
199200
}
200201
assert_true(untyped_match(compiled, "foo(other())") is Some(_))
201202
assert_true(
@@ -306,14 +307,14 @@ test "default matcher captures complex type nodes" {
306307
"let value : Array[Int] = input", "let value : Int? = input", "let value : (Int, String) = input",
307308
"let value : (Int) -> String = input",
308309
] {
309-
match untyped_match(compiled, source) {
310-
Some(result) =>
311-
match result.bindings.get("T") {
312-
Some(Single(node)) =>
313-
assert_true(node.kind.to_string().has_prefix("Type::"))
314-
_ => fail("expected type binding for \{source}")
315-
}
316-
None => fail("expected type metavar to match \{source}")
310+
if untyped_match(compiled, source) is Some(result) {
311+
match result.bindings.get("T") {
312+
Some(Single(node)) =>
313+
assert_true(node.kind.to_string().has_prefix("Type::"))
314+
_ => fail("expected type binding for \{source}")
315+
}
316+
} else {
317+
fail("expected type metavar to match \{source}")
317318
}
318319
}
319320
}

query/query_test.mbt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ test "query captures identifiers and constants by metavar name" {
148148
fail("expected second literal value")
149149
}
150150
inspect(first_value, content="danger")
151-
inspect(second_value, content="42")
151+
assert_eq(second_value, "42")
152152
}
153153

154154
///|
@@ -225,7 +225,7 @@ test "query raises parse error when relevant source is invalid" {
225225
#|}
226226
#|
227227
try query.captures(source_name="bad.mbt", source) catch {
228-
err => inspect("\{Repr(err)}".contains("parse"), content="true")
228+
err => assert_true("\{Repr(err)}".contains("parse"))
229229
} noraise {
230230
_ => fail("expected parse error")
231231
}
@@ -234,7 +234,7 @@ test "query raises parse error when relevant source is invalid" {
234234
///|
235235
test "query raises when pattern is invalid" {
236236
try ExprQuery::ExprQuery("target(") catch {
237-
err => inspect("\{Repr(err)}".contains("InvalidRule"), content="true")
237+
err => assert_true("\{Repr(err)}".contains("InvalidRule"))
238238
} noraise {
239239
_ => fail("expected invalid pattern")
240240
}
@@ -267,7 +267,7 @@ test "query captures_from_ast matches a direct expression root" {
267267
guard query_constant_value(literal) is Some(value) else {
268268
fail("expected literal value")
269269
}
270-
inspect(value, content="42")
270+
assert_eq(value, "42")
271271
}
272272

273273
///|

rule/apply/apply.mbt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,12 @@ fn scan_plan_literal_matches(
110110
literal_cache : Map[String, Bool],
111111
literal : String,
112112
) -> Bool {
113-
match literal_cache.get(literal) {
114-
Some(matched) => matched
115-
None => {
116-
let matched = source.contains(literal)
117-
literal_cache[literal] = matched
118-
matched
119-
}
113+
if literal_cache.get(literal) is Some(matched) {
114+
matched
115+
} else {
116+
let matched = source.contains(literal)
117+
literal_cache[literal] = matched
118+
matched
120119
}
121120
}
122121

0 commit comments

Comments
 (0)