palantir / conjure-java

Conjure generator for Java clients and servers
Apache License 2.0
27 stars 43 forks source link

Add @CheckReturnValue to build() methods #2282

Closed ash211 closed 5 months ago

ash211 commented 5 months ago

Fixes https://github.com/palantir/conjure-java/issues/2281

Before this PR

It's possible for users to call MyConjureObject.builder().x(1).y(2).build(); and not assign that to a variable. This is almost certainly a mistake, and calling code should either remove the instantiation of that object, or assign to a variable / return the value.

After this PR

==COMMIT_MSG== Add @CheckReturnValue to build() methods ==COMMIT_MSG==

@CheckReturnValue

Possible downsides?

Any violations of the error prone rule will manifest as a build failure when downstream repos pick up the new version of conjure-java.

changelog-app[bot] commented 5 months ago

Generate changelog in changelog-dir>`changelog/@unreleased`</changelog-dir

What do the change types mean? - `feature`: A new feature of the service. - `improvement`: An incremental improvement in the functionality or operation of the service. - `fix`: Remedies the incorrect behaviour of a component of the service in a backwards-compatible way. - `break`: Has the potential to break consumers of this service's API, inclusive of both Palantir services and external consumers of the service's API (e.g. customer-written software or integrations). - `deprecation`: Advertises the intention to remove service functionality without any change to the operation of the service itself. - `manualTask`: Requires the possibility of manual intervention (running a script, eyeballing configuration, performing database surgery, ...) at the time of upgrade for it to succeed. - `migration`: A fully automatic upgrade migration task with no engineer input required. _Note: only one type should be chosen._
How are new versions calculated? - ❗The `break` and `manual task` changelog types will result in a major release! - 🐛 The `fix` changelog type will result in a minor release in most cases, and a patch release version for patch branches. This behaviour is configurable in autorelease. - ✨ All others will result in a minor version release.

Type

- [ ] Feature - [x] Improvement - [ ] Fix - [ ] Break - [ ] Deprecation - [ ] Manual task - [ ] Migration

Description

Add @CheckReturnValue to build() methods **Check the box to generate changelog(s)** - [x] Generate changelog entry
ash211 commented 5 months ago

We had a private discussion about how many methods to put @CheckReturnValue on. You can imagine really going ham on this and sprinkling them over tons of methods. "constructors" like builder(), build(), from(), of(), valueOf(), and empty(). Also getters like get(), myField(), toString(), hashCode(). But not setters like myField(val) since they can be chained in a fluent style or not.

Carter's comment:

It's difficult to make a call where these annotations should be applied, and where it doesn't necessarily make sense. Adding them will cause some level of rollout friction on existing but non-critical bugs, and increase the load we put on the java compiler. If we can prevent bugs from shipping, this is reasonable, but it depends on the cost vs rate we expect bugs to appear vs their criticality... all of which are difficult to measure!

We ended up deciding to take a small incremental step and add to just the build() method, since that's the one that was involved in the bug that triggered this PR.

svc-autorelease commented 5 months ago

Released 8.18.0