phase3: nullfix arg-bang handler (argument_type/invalid_assignment). 872 -> 621
This commit is contained in:
@@ -101,6 +101,29 @@ def insert_bang(line, col0, name, kind):
|
||||
return None
|
||||
return None
|
||||
|
||||
def insert_arg_bang(line, col0):
|
||||
"""在参数值末尾插 '!'(仅限简单值:标识符/成员/调用/下标)。"""
|
||||
depth=0; end=None
|
||||
for i in range(col0, len(line)):
|
||||
ch=line[i]
|
||||
if ch in '([{': depth+=1
|
||||
elif ch in ')]}':
|
||||
if depth==0: end=i; break
|
||||
depth-=1
|
||||
elif ch==',' and depth==0: end=i; break
|
||||
if end is None: end=len(line)
|
||||
# 值的实际末尾(跳过空白)
|
||||
j=end-1
|
||||
while j>=col0 and line[j] in ' \t': j-=1
|
||||
if j<col0: return None
|
||||
if line[j]=='!': return None
|
||||
val=line[col0:j+1].strip()
|
||||
if not val: return None
|
||||
if val[0] in '0123456789"\'': return None # 字面量跳过
|
||||
if val in ('true','false','null'): return None
|
||||
if not re.fullmatch(r'[\w.\[\]()]+', val): return None # 含运算符等跳过
|
||||
return line[:j+1]+'!'+line[j+1:]
|
||||
|
||||
def make_field_nullable(lines, name):
|
||||
"""找字段声明 'Type name;' 并在类型后加 '?'。返回 (行索引, 新行) 或 (None,None)。"""
|
||||
pat = re.compile(r'^(\s+(?:final\s+|const\s+)?(?:static\s+)?)(.+)\s+'+re.escape(name)+r';\s*$')
|
||||
@@ -170,6 +193,8 @@ def apply_round(errs):
|
||||
new=insert_nullable_before_name(old, pname, col-1)
|
||||
elif rule in ('not_initialized_non_nullable_instance_field','not_initialized_non_nullable_variable'):
|
||||
new=insert_late(old)
|
||||
elif rule in ('argument_type_not_assignable','invalid_assignment'):
|
||||
new=insert_arg_bang(old, col-1)
|
||||
if new and new!=old:
|
||||
L[line-1]=new; changed+=1
|
||||
flush()
|
||||
|
||||
Reference in New Issue
Block a user