google / google/error-prone

Varifier + VarWithPrimitive conflict when casted

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

Description

Prior to v2.50.0, `Varifier` would encourage primitives to be assigned using `var` and most of these cases were eliminated by preferring `VarWithPrimitive`. However, when a cast is used then the two rules conflict, e.g. `??? sign = (int) Math.signum(dw * dHr);` violates either rule when given an `int` or `var`.

VarWithPrimitive

```console
> Task :simulator:compileJava FAILED
/Users/ben/projects/caffeine/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/policy/sketch/climbing/hill/CorrelationClimber.java:84: warning: [VarWithPrimitive] Avoid using `var` with primitive types. Explicit primitive type names are short and clear, and `var` provides no benefit in readability while potentially hiding the type.
var sign = (int) Math.signum(dw * dHr);
^
(see https://errorprone.info/bugpattern/VarWithPrimitive)
Did you mean 'int sign = (int) Math.signum(dw * dHr);'?
error: warnings found and -Werror specified
1 error
1 warning

> Task :simulator:processTestResources NO-SOURCE

> Task :caffeine:compileTestJava
/Users/ben/projects/caffeine/caffeine/src/test/java/com/github/benmanes/caffeine/cache/BoundedLocalCacheTest.java:2611: warning: [VarWithPrimitive] Avoid using `var` with primitive types. Explicit primitive type names are short and clear, and `var` provides no benefit in readability while potentially hiding the type.
var cappedSampleSize = (int) (baseSampleSize * SMALL_CACHE_SAMPLE_RATIO_CAP);
^
(see https://errorprone.info/bugpattern/VarWithPrimitive)
Did you mean 'int cappedSampleSize = (int) (baseSampleSize * SMALL_CACHE_SAMPLE_RATIO_CAP);'?
error: warnings found and -Werror specified
1 error
1 warning
```

Varifier

```console
> Task :simulator:compileJava
/Users/ben/projects/caffeine/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/policy/sketch/climbing/hill/CorrelationClimber.java:84: warning: [Varifier] Consider using `var` here to avoid boilerplate.
int sign = (int) Math.signum(dw * dHr);
^
(see https://errorprone.info/bugpattern/Varifier)
Did you mean 'var sign = (int) Math.signum(dw * dHr);'?
error: warnings found and -Werror specified
1 error
1 warning

> Task :simulator:compileJava FAILED
> Task :simulator:processTestResources NO-SOURCE

> Task :caffeine:compileTestJava
/Users/ben/projects/caffeine/caffeine/src/test/java/com/github/benmanes/caffeine/cache/BoundedLocalCacheTest.java:2611: warning: [Varifier] Consider using `var` here to avoid boilerplate.
int cappedSampleSize = (int) (baseSampleSize * SMALL_CACHE_SAMPLE_RATIO_CAP);
^
(see https://errorprone.info/bugpattern/Varifier)
Did you mean 'var cappedSampleSize = (int) (baseSampleSize * SMALL_CACHE_SAMPLE_RATIO_CAP);'?
error: warnings found and -Werror specified
1 error
1 warning
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the conflict using the examples in simulator/.../CorrelationClimber.java and caffeine/.../BoundedLocalCacheTest.java, then inspect the Varifier and VarWithPrimitive implementations and their tests. Determine how cast-to-primitive assignments should be diagnosed, and add regression coverage showing that the checks no longer issue mutually exclusive recommendations.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.