google / google/error-prone

Refaster matcher for expressions guaranteed to be free of side effects

Open
#1,497 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Java
Stars
7.2k
Forks
820
Avg merge
5h 9m
Merged PRs (30d)
50

Description

### Description of the feature request:

I suggest to add a `com.google.errorprone.matchers.Matcher` implementation that matches expressions that are guaranteed to have no side effects. Besides the obvious assignment expression, this would also not match on expressions that contain method calls. The matcher should work recursively and analyze the full exception.

### Feature requests: what underlying problem are you trying to solve with this feature?

I would like to be able to write a Refaster rule that transforms
```
if (cond) { throw new IllegalArgumentException(msg) };
```
into
```
Preconditions.checkArgument(cond, msg);
```
but only if the evaluation of `msg` is guaranteed to not have any side effects.

I suspect this matcher is also useful in other cases to match rules safer.

### What version of Error Prone are you using?

2.3.4

### Have you found anything relevant by searching the web?

No.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.