From 21046ab148c33daa0eb15198c295af11e38ff402 Mon Sep 17 00:00:00 2001 From: xudan16 Date: Mon, 9 Jun 2025 14:44:00 +0800 Subject: [PATCH] fix error in bind this rule Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICDNT3 Signed-off-by: xudan16 --- .../src/checker/migration/ThisBindCheck.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/ets2panda/linter/homecheck/src/checker/migration/ThisBindCheck.ts b/ets2panda/linter/homecheck/src/checker/migration/ThisBindCheck.ts index af8657687c..b4921ec648 100644 --- a/ets2panda/linter/homecheck/src/checker/migration/ThisBindCheck.ts +++ b/ets2panda/linter/homecheck/src/checker/migration/ThisBindCheck.ts @@ -29,6 +29,7 @@ import { ArkNewExpr, ArkClass, ClassSignature, + ArkReturnStmt, } from 'arkanalyzer'; import Logger, { LOG_MODULE_TYPE } from 'arkanalyzer/lib/utils/logger'; import { BaseChecker, BaseMetaData } from '../BaseChecker'; @@ -115,7 +116,18 @@ export class ThisBindCheck implements BaseChecker { private useThisInBody(method: ArkMethod): boolean { const thisInstance = (method.getThisInstance() as Local)!; - return thisInstance.getUsedStmts().length > 0; + const usedStmts = thisInstance.getUsedStmts(); + if (method.getName() !== 'constructor') { + return usedStmts.length > 0; + } + // constructor方法一定会有return this语句,此句若为ArkAnalyzer为constructor方法自动生成,则不在检查范围内 + for (const stmt of usedStmts) { + if (stmt instanceof ArkReturnStmt && stmt.getOriginPositionInfo().getLineNo() <= 0) { + continue; + } + return true; + } + return false; } private isSafeUse(v: Value): boolean { -- Gitee