quasilyte/go-ruleguard

false negative submatch

peakle opened this issue · 1 comments

test data:

var globalVar *os.File

func negative2() {
	globalVar, _ = ioutil.TempFile("", "") // global var
	kk := globalVar.Name()

	print(kk)
}

rule:

func unclosedResource(m dsl.Matcher) {
	varEscapeFunction := func(x dsl.Var) bool {
		return x.Contains(`$_($*_, $res, $*_)`) || x.Contains(`$_{$*_, $res, $*_}`) ||
			x.Contains(`$_{$*_, $_: $res, $*_}`) || x.Contains(`$_ <- $res`) ||
			x.Contains(`return $*_, $res, $*_`) || x.Contains(`$_[$_] = $res`) ||
			x.Contains(`$_[$res] = $_`)
	}

	m.Match(`$res, $err := $open($*_); $*body`,
		`$res, $err = $open($*_); $*body`,
		`var $res, $err = $open($*_); $*body`,
	).
		Where(m["res"].Type.Implements(`io.Closer`) &&
			m["err"].Type.Implements(`error`) &&
			(!m["body"].Contains(`$res.Close()`) && !varEscapeFunction(m["body"]))).
		Report(`$res.Close() should be deferred right after the $open error check`).
		At(m["res"])
}

debug-group output:

 rejected by m["err"].Type.Implements(`error`)

so as i see if we nulify second var in = assignment , it doesn't have error type in current implemntation