Skip to content

Commit b978482

Browse files
authored
Merge pull request GaijinEntertainment#2627 from GaijinEntertainment/bbatkin/lint-rules-and-fixer
lint: PERF018/STYLE005/STYLE020/STYLE021 + auto-fixer + daslib+utils sweep
2 parents 59fa27c + fbd7f5e commit b978482

168 files changed

Lines changed: 4567 additions & 5735 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CLAUDE.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ Full migration table (when reading older docs that say `var inscope` or `<-` for
187187
- `try/recover` — NOT `try/catch` (`recover` is the keyword)
188188
- `panic("message")`, `assert(condition)`, `verify(condition)` (stays in release)
189189
- **Postfix conditional:** `return expr if (cond)`, `break if (cond)`, `continue if (cond)` — early-exit guard on one line
190+
- **Braceless early-exit:** prefer `if (cond) return X` (or postfix `return X if (cond)`) over `if (cond) { return X }` — STYLE005 flags the braced single-terminator form as noise
190191

191192
### Generic function dispatch
192193

@@ -258,6 +259,10 @@ Full migration table (when reading older docs that say `var inscope` or `<-` for
258259
| `if (true) { ... }` | `{ ... }` | bare blocks create lexical scope in gen2 |
259260
| `var inscope r <- expr; return <- r` | `return <- expr` | direct return avoids intermediate |
260261
| `unsafe { (reinterpret<ExprBlock?> blk).list }` / `unsafe(reinterpret<T?> x)` | make param `var` + plain `x.list` | `var` param gives non-const field access without reinterpret |
262+
| `if (cond) { return X }` (or `{ break }` / `{ continue }`) | `if (cond) return X` or postfix `return X if (cond)` | STYLE005: braces around a single-statement early-exit are noise |
263+
| `for (i in range(length(arr))) { ... arr[i] ... }` where `i` is used only as `arr[i]` | `for (c in arr) { ... c ... }` | PERF018: direct iteration drops the index variable |
264+
| `from_JV(v, type<int>, 13)` | `v ?? 13` | STYLE020: json_boost provides `operator ??` for every scalar `from_JV` overload |
265+
| `var args : table<string; JsonValue?>; args \|> insert("k1", JV(v1)); args \|> insert("k2", JV(v2))` | `var args = JV((k1=v1, k2=v2))` | STYLE021: named-tuple JV form (json_boost.das:638) is one line instead of N |
261266

262267
For path/filename ops use `fio` helpers (`base_name`/`dir_name`/`path_join`/etc.) — see `skills/filesystem.md`. Never hand-roll `rfind("/")` / slice — misses Windows separators.
263268

daslib/algorithm.das

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ def sort_unique(var a : array<auto(TT)>) {
3434
//! Returns an array of elements from a, sorted and with duplicates removed.
3535
//! The elements are sorted in ascending order.
3636
//! The resulting array has only unique elements.
37-
if (empty(a)) {
38-
return
39-
}
37+
if (empty(a)) return
4038
sort(a)
4139
var b <- unique(a)
4240
delete a
@@ -432,19 +430,15 @@ def fill(var a; value) {
432430
def is_sorted(a : array<auto(TT)>) : bool {
433431
//! Returns true if the array is sorted in non-descending order.
434432
for (i in range(1, length(a))) {
435-
if (a[i] < a[i - 1]) {
436-
return false
437-
}
433+
if (a[i] < a[i - 1]) return false
438434
}
439435
return true
440436
}
441437

442438
def is_sorted(a : array<auto(TT)>; less : block<(a, b : TT const -&) : bool>) : bool {
443439
//! Returns true if the array is sorted according to the provided less function.
444440
for (i in range(1, length(a))) {
445-
if (invoke(less, a[i], a[i - 1])) {
446-
return false
447-
}
441+
if (invoke(less, a[i], a[i - 1])) return false
448442
}
449443
return true
450444
}
@@ -463,9 +457,7 @@ def rotate(var a : array<auto>; mid : int) {
463457
//! Rotates the array so that the element at index mid becomes the first element.
464458
//! Elements before mid are moved to the end.
465459
let l = length(a)
466-
if (l == 0 || mid == 0 || mid == l) {
467-
return
468-
}
460+
if (l == 0 || mid == 0 || mid == l) return
469461
assert(mid > 0 && mid < l, "rotate mid out of range")
470462
// reverse [0, mid), [mid, l), then reverse all
471463
let lm1 = l - 1
@@ -494,9 +486,7 @@ def rotate(var a; mid : int) {
494486

495487
def min_element(a : array<auto(TT)>) : int {
496488
//! Returns the index of the minimum element in the array, or -1 if the array is empty.
497-
if (length(a) == 0) {
498-
return -1
499-
}
489+
if (length(a) == 0) return -1
500490
var best = 0
501491
for (i in range(1, length(a))) {
502492
if (a[i] < a[best]) {
@@ -508,9 +498,7 @@ def min_element(a : array<auto(TT)>) : int {
508498

509499
def min_element(a : array<auto(TT)>; less : block<(a, b : TT const -&) : bool>) : int {
510500
//! Returns the index of the minimum element according to the provided less function, or -1 if the array is empty.
511-
if (length(a) == 0) {
512-
return -1
513-
}
501+
if (length(a) == 0) return -1
514502
var best = 0
515503
for (i in range(1, length(a))) {
516504
if (invoke(less, a[i], a[best])) {
@@ -522,9 +510,7 @@ def min_element(a : array<auto(TT)>; less : block<(a, b : TT const -&) : bool>)
522510

523511
def max_element(a : array<auto(TT)>) : int {
524512
//! Returns the index of the maximum element in the array, or -1 if the array is empty.
525-
if (length(a) == 0) {
526-
return -1
527-
}
513+
if (length(a) == 0) return -1
528514
var best = 0
529515
for (i in range(1, length(a))) {
530516
if (a[best] < a[i]) {
@@ -536,9 +522,7 @@ def max_element(a : array<auto(TT)>) : int {
536522

537523
def max_element(a : array<auto(TT)>; less : block<(a, b : TT const -&) : bool>) : int {
538524
//! Returns the index of the maximum element according to the provided less function, or -1 if the array is empty.
539-
if (length(a) == 0) {
540-
return -1
541-
}
525+
if (length(a) == 0) return -1
542526
var best = 0
543527
for (i in range(1, length(a))) {
544528
if (invoke(less, a[best], a[i])) {
@@ -647,13 +631,9 @@ def difference(a, b : table<auto(TT)>) : table<TT> {
647631

648632
def identical(a, b : table<auto(TT)>) : bool {
649633
//! Returns true if the two sets are identical.
650-
if (length(a) != length(b)) {
651-
return false
652-
}
634+
if (length(a) != length(b)) return false
653635
for (k in keys(a)) {
654-
if (!b |> key_exists(k)) {
655-
return false
656-
}
636+
if (!b |> key_exists(k)) return false
657637
}
658638
return true
659639
}
@@ -676,13 +656,9 @@ def symmetric_difference(a, b : table<auto(TT)>) : table<TT> {
676656

677657
def is_subset(a, b : table<auto(TT)>) : bool {
678658
//! Returns true if all elements of a are contained in b.
679-
if (length(a) > length(b)) {
680-
return false
681-
}
659+
if (length(a) > length(b)) return false
682660
for (k in keys(a)) {
683-
if (!b |> key_exists(k)) {
684-
return false
685-
}
661+
if (!b |> key_exists(k)) return false
686662
}
687663
return true
688664
}

daslib/ansi_colors.das

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,7 @@ def reset_str() : string {
9494

9595
def private has_value(args : array<string>; val : string) : bool {
9696
for (a in args) {
97-
if (a == val) {
98-
return true
99-
}
97+
if (a == val) return true
10098
}
10199
return false
102100
}

daslib/aot_cpp.das

Lines changed: 28 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ def aotModuleName(pm : Module?) {
4141

4242
def isSequencialMask(fields : dasvector`uint8) {
4343
for (i in range(1, length(fields))) {
44-
if (int(fields[i - 1]) + 1 != int(fields[i])) {
45-
return false;
46-
}
44+
if (int(fields[i - 1]) + 1 != int(fields[i])) return false;
4745
}
4846
return true;
4947
}
@@ -149,8 +147,8 @@ def das_to_cppCTypeString(t : Type) {
149147

150148

151149
def isConstRedundantForCpp(typeDecl : TypeDeclPtr) {
152-
if (!empty(typeDecl.dim)) { return false; }
153-
if (typeDecl.isVectorType) { return true; }
150+
if (!empty(typeDecl.dim)) return false;
151+
if (typeDecl.isVectorType) return true;
154152
match (typeDecl.baseType) {
155153
if (Type.tBool) { return true; }
156154
if (Type.tInt8) { return true; }
@@ -415,7 +413,7 @@ def describeCppTypeEx(typeDecl : TypeDeclPtr;
415413
write(writer, ",{typeDecl.dim[dim_count - 1 - i]}>")
416414
}
417415

418-
if (cfg.skip_const == false && typeDecl.flags.constant) {
416+
if (!cfg.skip_const && typeDecl.flags.constant) {
419417
write(writer, " const ");
420418
}
421419
// Value-annotation handles (e.g. Handle<T>) are POD and always passed by value,
@@ -424,7 +422,7 @@ def describeCppTypeEx(typeDecl : TypeDeclPtr;
424422
// Keep the * suffix when substitute_ref is requested (e.g. iteration variables),
425423
// since das_iterator::first(Context*, QQ*&) demands a real pointer regardless.
426424
let valueHandle = baseType == Type.tHandle && !typeDecl.isRefType
427-
if (typeDecl.flags.ref && cfg.skip_ref == false) {
425+
if (typeDecl.flags.ref && !cfg.skip_ref) {
428426
if (cfg.substitute_ref) {
429427
write(writer, " *");
430428
} elif (!valueHandle) {
@@ -691,11 +689,11 @@ class public AotDebugInfoHelper {
691689
write(writer, " ann.resolveAnnotation();\n")
692690
write(writer, " \}\n")
693691
helper |> debug_helper_iter_structs($(name, si) {
694-
if (si.fields == null) { return ; }
692+
if (si.fields == null) return ;
695693
for (fi in range(si.count)) {
696694
let fld & = unsafe(si.fields[fi])
697-
if (fld.annotation_arguments == null) { continue; }
698-
if (length(*fld.annotation_arguments) == 0) { continue; }
695+
if (fld.annotation_arguments == null) continue;
696+
if (length(*fld.annotation_arguments) == 0) continue;
699697
write(writer, " {structInfoName(si)}_field_{fi}.annotation_arguments = &{structInfoName(si)}_field_{fi}_ann;\n")
700698
}
701699
})
@@ -1332,8 +1330,7 @@ class public CppAot : AstVisitor {
13321330
// function
13331331
def override canVisitFunction(fun : Function?) {
13341332
if (fun.flags.noAot) return false;
1335-
if (fun.moreFlags.isTemplate) return false;
1336-
return true;
1333+
return !fun.moreFlags.isTemplate;
13371334
}
13381335
def override preVisitFunction(fn : FunctionPtr) {
13391336
if (!empty(aot_filter_function) && (fn.name == aot_filter_function || aotFuncName(fn) == aot_filter_function)) {
@@ -1686,9 +1683,7 @@ class public CppAot : AstVisitor {
16861683
}
16871684
}
16881685
def isOpPolicy1(that : ExprOp1?) {
1689-
if (is_alpha(first_character(that.op))) {
1690-
return true;
1691-
}
1686+
if (is_alpha(first_character(that.op))) return true;
16921687
return that.subexpr._type.isPolicyType;
16931688
}
16941689
def override preVisitExprOp1(var that : ExprOp1?) {
@@ -3320,26 +3315,16 @@ class public CppAot : AstVisitor {
33203315
}
33213316
// call
33223317
def policyArgNeedCast(polType : TypeDeclPtr; argType : TypeDeclPtr) {
3323-
if (argType.isVectorType) {
3324-
return false;
3325-
}
3318+
if (argType.isVectorType) return false;
33263319
if (!polType.isHandle) {
3327-
if (polType.isVecPolicyType && argType.isVecPolicyType) {
3328-
return false;
3329-
}
3330-
}
3331-
if (!polType.isPolicyType) {
3332-
return false;
3320+
if (polType.isVecPolicyType && argType.isVecPolicyType) return false;
33333321
}
3322+
if (!polType.isPolicyType) return false;
33343323
return true;
33353324
}
33363325
def policyResultNeedCast(polType : TypeDeclPtr; resType : TypeDeclPtr) {
3337-
if (resType.isVoid) {
3338-
return false;
3339-
}
3340-
if (!resType.isPolicyType) {
3341-
return false;
3342-
}
3326+
if (resType.isVoid) return false;
3327+
if (!resType.isPolicyType) return false;
33433328
return policyArgNeedCast(polType, resType);
33443329
}
33453330
def isPolicyBasedCall(call : ExprCall?) {
@@ -3356,9 +3341,7 @@ class public CppAot : AstVisitor {
33563341
def isPolicyBasedCallFunc(call : ExprCallFunc?) {
33573342
if (call.func.flags.builtIn) {
33583343
let bif = call.func as BuiltInFunction;
3359-
if (bif.flags.policyBased) {
3360-
return true;
3361-
}
3344+
if (bif.flags.policyBased) return true;
33623345
}
33633346
return false;
33643347
}
@@ -3371,10 +3354,7 @@ class public CppAot : AstVisitor {
33713354
if (func.flags.callBased) {
33723355
panic("we should not be here. call-based calls handled elsewhere");
33733356
}
3374-
if (length(bif.cppName) == 0) {
3375-
return true;
3376-
}
3377-
return false;
3357+
return length(bif.cppName) == 0;
33783358
}
33793359
if (func.flags.noAot) return true;
33803360
if (func.flags.aotHybrid) return true;
@@ -3388,9 +3368,7 @@ class public CppAot : AstVisitor {
33883368
if (expr is ExprMakeBlock) {
33893369
let mkblk = expr as ExprMakeBlock;
33903370
let blk = mkblk._block as ExprBlock;
3391-
if (blk.blockFlags.aotSkipMakeBlock) {
3392-
return false;
3393-
}
3371+
if (blk.blockFlags.aotSkipMakeBlock) return false;
33943372
}
33953373
return needsArgPassType(expr._type);
33963374
}
@@ -3512,9 +3490,7 @@ class public CppAot : AstVisitor {
35123490
if (call.func.moreFlags.propertyFunction) return ; // property function goes ((arg0).name()). we do nothing here
35133491
var argIndex = 0;
35143492
for (it in range(length(call.arguments))) {
3515-
if (call.arguments[it] == arg) {
3516-
break;
3517-
}
3493+
if (call.arguments[it] == arg) break;
35183494
argIndex++;
35193495
}
35203496
assert(argIndex != length(call.arguments));
@@ -3555,9 +3531,7 @@ class public CppAot : AstVisitor {
35553531
if (call.func.moreFlags.propertyFunction) return ; // property function goes ((arg0).name()). we do nothing here
35563532
var argIndex = 0;
35573533
for (it in range(length(call.arguments))) {
3558-
if (call.arguments[it] == arg) {
3559-
break;
3560-
}
3534+
if (call.arguments[it] == arg) break;
35613535
argIndex++;
35623536
}
35633537

@@ -3670,19 +3644,15 @@ class public CppAot : AstVisitor {
36703644
write(*ss, "{tabs()}");
36713645
}
36723646
def isCountOrUCount(expr : ExpressionPtr) {
3673-
if (!(isExprCallFunc(expr))) {
3674-
return false;
3675-
}
3647+
if (!(isExprCallFunc(expr))) return false;
36763648
assume call = unsafe(reinterpret<ExprCallFunc?>(expr));
36773649
return call.func != null && call.func.flags.builtIn && call.func._module.name == "$" && (call.name == "count" || call.name == "ucount");
36783650
}
36793651
def override preVisitExprForSource(ffor : ExprFor?; that : ExpressionPtr; last : bool) {
36803652
var idx = 0;
36813653
let idxs = length(ffor.sources);
36823654
for (id in range(idxs)) {
3683-
if (ffor.sources[id] == that) {
3684-
break;
3685-
}
3655+
if (ffor.sources[id] == that) break;
36863656
idx++
36873657
}
36883658
assume src = ffor.sources[idx];
@@ -3699,9 +3669,7 @@ class public CppAot : AstVisitor {
36993669
var idx = 0;
37003670
let idxs = length(ffor.sources);
37013671
for (id in range(idxs)) {
3702-
if (ffor.sources[id] == that) {
3703-
break;
3704-
}
3672+
if (ffor.sources[id] == that) break;
37053673
idx++
37063674
}
37073675
assume src = ffor.sources[idx];
@@ -3749,17 +3717,13 @@ def public dumpDependencies(program : ProgramPtr; var aotVisitor : CppAot?) {
37493717
}
37503718
let remUS = program._options |> find_arg("remove_unused_symbols") ?as tBool ?? true;
37513719
program.get_ptr() |> for_each_module_no_order($(pm) {
3752-
if (pm.moduleFlags.fromExtraDependency) {
3753-
return ;
3754-
}
3720+
if (pm.moduleFlags.fromExtraDependency) return ;
37553721
pm |> for_each_structure($(ps) {
37563722
write(*aotVisitor.ss, "namespace {aotModuleName(ps._module)} \{ struct {aotStructName(ps)}; \};\n");
37573723
});
37583724
});
37593725
program.get_ptr() |> for_each_module_no_order($(pm) {
3760-
if (pm == program.getThisModule || pm.moduleFlags.fromExtraDependency) {
3761-
return ;
3762-
}
3726+
if (pm == program.getThisModule || pm.moduleFlags.fromExtraDependency) return ;
37633727
pm |> for_each_enumeration($(penum) {
37643728
if (!remUS || utm.useEnums |> key_exists(penum)) {
37653729
program |> visit_enumeration(penum, aotVisitor.adapter)
@@ -3788,8 +3752,7 @@ class ArgsConverter : AstVisitor {
37883752

37893753
def override canVisitFunction(fun : Function?) {
37903754
if (fun.flags.noAot) return false;
3791-
if (fun.moreFlags.isTemplate) return false;
3792-
return true;
3755+
return !fun.moreFlags.isTemplate;
37933756
}
37943757

37953758
def override preVisitFunction(fn : FunctionPtr) {
@@ -3853,9 +3816,7 @@ def collectUsedFunctions(modules : array<Module?>; totalFunctions : int; this_mo
38533816
fnn.reserve(totalFunctions);
38543817
for (pm in modules) {
38553818
pm |> for_each_module_function($(pfun) {
3856-
if (!all_modules && pfun._module != this_module) {
3857-
return ;
3858-
}
3819+
if (!all_modules && pfun._module != this_module) return ;
38593820
if (pfun.index < 0 || !pfun.flags.used) return ;
38603821
if (!is_all) {
38613822
if (pfun.flags.builtIn || pfun.flags.noAot) return ;
@@ -4111,9 +4072,7 @@ def public run_aot_function(prog : Program?; var pctx : Context?; cop : CodeOfPo
41114072
let program <- unsafe(reinterpret<ProgramPtr>(prog));
41124073
return build_string() $(writer) {
41134074
let (modules_str, noAotModule) = getRequiredModulesFor(program)
4114-
if ((program._options |> find_arg("no_aot") ?as tBool ?? false) || noAotModule) {
4115-
return
4116-
}
4075+
if ((program._options |> find_arg("no_aot") ?as tBool ?? false) || noAotModule) return
41174076
build_string() $(tmp_writer) {
41184077
let marker_vis = new NoAotMarker();
41194078
make_visitor(*marker_vis) $(adapter_marker) {

0 commit comments

Comments
 (0)