Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ruleguard: autofix some interpolated expressions #353

Merged
merged 1 commit into from Jan 9, 2022

Conversation

quasilyte
Copy link
Owner

Given this pattern:

io.WriteString($w, $s)

And this input string:

io.WriteString(&buf, s)

With this replacement template:

$w.WriteString($s)

We'll get this result:

&buf.WriteString(s)

That's incorrect.

To avoid that, ruleguard attempts to fix some interpolated
expressions when it can understand the context.

In the cases above, it sees that variable is followed by .
while $w contains unary & expression. It knows that
$w.x is identical to (&$w).w, so we can simply insert
buf instead of &buf and get buf.WriteString(s).

This greatly simplifies the creation of reliable rules
that just work.

Given this pattern:

	io.WriteString($w, $s)

And this input string:

	io.WriteString(&buf, s)

With this replacement template:

	$w.WriteString($s)

We'll get this result:

	&buf.WriteString(s)

That's incorrect.

To avoid that, ruleguard attempts to fix some interpolated
expressions when it can understand the context.

In the cases above, it sees that variable is followed by `.`
while `$w` contains unary `&` expression. It knows that
`$w.x` is identical to `(&$w).w`, so we can simply insert
`buf` instead of `&buf` and get `buf.WriteString(s)`.

This greatly simplifies the creation of reliable rules
that just work.
@quasilyte quasilyte merged commit 38dd4e2 into master Jan 9, 2022
@quasilyte quasilyte deleted the quasilyte/autofix_interpolated_expr branch January 9, 2022 12:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant