fix: code review — defensivな下*/去掉+B枝一致化

## 评审发现修正

### 1. __import__('re') → re (cond.py, 3处)
__import__绕过module级re引用,mock下前后不一致。

### 2. NOT路径下*/去掉 (cond.py:124-126)
NOT WS-PLAN-CODE(WS-IDX) > 50 → 返回保留了下标
其他路径(算术/标准)都去了,只有NOT路径没去。

### 3. _match_constraint防禦*/去掉 (coverage.py)
两边字段名同时去掉下标再比较,防止约束侧/解析侧
下标处理不一致导致匹配失败。

### 4. _match_leaf 防禦*/去掉 (coverage.py)
CondLeaf路径同样的防禦。

### 5. 裸字段分支去死码 (cond.py)
 在外層guard保證下永遠為真,
是死码。合併為一行。

Co-Authored-By: Claude
This commit is contained in:
NB-076
2026-06-25 08:28:43 +08:00
parent 3eb356d711
commit 56d1cf5e78
2 changed files with 16 additions and 11 deletions
+10 -9
View File
@@ -100,10 +100,9 @@ def parse_single_condition(text, fields=None):
# Bare field reference (no operator, no NOT): WS-EOF → WS-EOF = 'Y'
if re.match(r'^[A-Z][A-Z0-9_-]*(?:\([^)]*\))?\s*$', text, re.IGNORECASE):
bare = __import__('re').match(r'^[A-Z][A-Z0-9_-]*', text, re.IGNORECASE)
if bare:
return (bare.group(0), '=', 'Y')
return (text, '=', 'Y')
bare = re.match(r'^[A-Z][A-Z0-9_-]*', text, re.IGNORECASE)
field = bare.group(0) if bare else text
return (field, '=', 'Y')
# Bare NOT field reference (no operator): NOT WS-EOF → WS-EOF <> 'Y'
if text.upper().startswith('NOT ') and not re.search(r'(>=|<=|<>|>|<|=)', text):
@@ -122,6 +121,9 @@ def parse_single_condition(text, fields=None):
if m_inner:
inv_op_map = {'=': '<>', '<>': '=', '>': '<=', '<': '>=', '>=': '<', '<=': '>'}
f = re.sub(r'\s*([(),])\s*', r'\1', m_inner.group(1))
bare = re.match(r"^(\w[\w-]*)", f)
if bare:
f = bare.group(1)
op = m_inner.group(2)
val = m_inner.group(3).strip().strip("'").strip('"')
inv = inv_op_map.get(op, op)
@@ -163,7 +165,7 @@ def parse_single_condition(text, fields=None):
if field.upper().endswith(' NOT'):
field = field[:-4].strip()
# Strip subscript: WS-KEY-DUP-CNT(WS-J) -> WS-KEY-DUP-CNT
bare_m = __import__('re').match(r'^(\w[\w-]*)', field)
bare_m = re.match(r'^(\w[\w-]*)', field)
if bare_m:
field = bare_m.group(1)
return (field, m.group(2), m.group(3).strip().strip("'").strip('"'))
@@ -183,10 +185,9 @@ def parse_single_condition(text, fields=None):
# Bare field: WS-EOF (no operator) -> WS-EOF = 'Y'
if re.match(r'^[A-Z][A-Z0-9_-]*(?:\([^)]*\))?\s*$', text, re.IGNORECASE):
bare = __import__('re').match(r'^[A-Z][A-Z0-9_-]*', text, re.IGNORECASE)
if bare:
return (bare.group(0), '=', 'Y')
return (text, '=', 'Y')
bare = re.match(r'^[A-Z][A-Z0-9_-]*', text, re.IGNORECASE)
field = bare.group(0) if bare else text
return (field, '=', 'Y')
return None
+6 -2
View File
@@ -182,14 +182,18 @@ def mark_coverage(decision_points, leaf_stats, branch_paths, fields):
def _match_constraint(c, parsed):
if len(c) != 4:
return False
return (c[0] == parsed[0] and c[1] == parsed[1]
c0 = re.sub(r'\s*\(.*?\)\s*$', '', str(c[0])).strip()
p0 = re.sub(r'\s*\(.*?\)\s*$', '', str(parsed[0])).strip()
return (c0 == p0 and c[1] == parsed[1]
and str(c[2]) == str(parsed[2]))
def _match_leaf(c, leaf):
if len(c) != 4:
return False
return (c[0] == leaf.field and c[1] == leaf.op
c0 = re.sub(r'\s*\(.*?\)\s*$', '', str(c[0])).strip()
l0 = re.sub(r'\s*\(.*?\)\s*$', '', str(leaf.field)).strip()
return (c0 == l0 and c[1] == leaf.op
and str(c[2]) == str(leaf.value))