diff --git a/ets2panda/linter/homecheck/src/checker/migration/NumericSemanticCheck.ts b/ets2panda/linter/homecheck/src/checker/migration/NumericSemanticCheck.ts index 3e4f7ae295d26ce44bab1e2ce851f15313bb01fe..00c9adb29a12463d7c246097b760dd586c51acb8 100644 --- a/ets2panda/linter/homecheck/src/checker/migration/NumericSemanticCheck.ts +++ b/ets2panda/linter/homecheck/src/checker/migration/NumericSemanticCheck.ts @@ -999,7 +999,7 @@ export class NumericSemanticCheck implements BaseChecker { if (param1 instanceof ClassType && param2 instanceof ClassType) { const classSign1 = param1.getClassSignature(); const classSign2 = param2.getClassSignature(); - if(SdkUtils.isClassFromSdk(classSign1) && SdkUtils.isClassFromSdk(classSign2)) { + if (SdkUtils.isClassFromSdk(classSign1) && SdkUtils.isClassFromSdk(classSign2)) { return classSign1.getClassName() === classSign2.getClassName() } return classSignatureCompare(classSign1, classSign2) @@ -1542,6 +1542,7 @@ export class NumericSemanticCheck implements BaseChecker { this.addIssueReport(RuleCategory.NumericLiteral, NumberCategory.number, IssueReason.UsedWithOtherType, true, stmt, op1); fixedEnumOp2Number = true; } + this.checkDivisionWithLocal(op1); } if (op2 instanceof NumberConstant && !this.isNumberConstantActuallyFloat(op2)) { this.addIssueReport(RuleCategory.NumericLiteral, NumberCategory.number, IssueReason.UsedWithOtherType, true, stmt, op2); @@ -1623,6 +1624,57 @@ export class NumericSemanticCheck implements BaseChecker { return IssueReason.Other; } + private isArkAssignStmt(stmt: Stmt): boolean { + return stmt instanceof ArkAssignStmt; + } + + private isArkNormalBinopExpr(value: Value): boolean { + return value instanceof ArkNormalBinopExpr; + } + + private checkDivisionWithLocal(local: Local): void { + if (!local.getName().startsWith(TEMP_LOCAL_PREFIX)) { + return; + } + const decl = local.getDeclaringStmt(); + if (decl === null) { + return; + } + + if (!this.isArkAssignStmt(decl)) { + return; + } + const assignStmt = decl as ArkAssignStmt; + + const rightSide = assignStmt.getRightOp(); + if (!this.isArkNormalBinopExpr(rightSide)) { + return; + } + + const binaryOperation = rightSide as ArkNormalBinopExpr; + const operator = binaryOperation.getOperator(); + switch (operator) { + case NormalBinaryOperator.Division: + case NormalBinaryOperator.Addition: + case NormalBinaryOperator.Multiplication: + case NormalBinaryOperator.Exponentiation: + case NormalBinaryOperator.Subtraction: { + const lhs = binaryOperation.getOp1(); + if (lhs instanceof Local) { + this.checkDivisionWithLocal(lhs); + } + if (lhs instanceof NumberConstant) { + this.addIssueReport(RuleCategory.NumericLiteral, NumberCategory.number, IssueReason.UsedWithOtherType, true, decl, lhs); + return; + } + break; + } + default: { + break; + } + } + } + private isAbstractRefOnlyUsedAsIntLong(stmt: Stmt, ref: AbstractRef, hasChecked: Map, numberCategory: NumberCategory): IssueReason { if (ref instanceof ArkArrayRef) { // 使用数组中某元素进行赋值的场景很复杂,需要判断index的具体值,需要判断数组中的队应元素的全部使用场景,当前不做检查,直接返回false