Skip to content

Annotate methods of generated descendants of Message.Builder with @CanIgnoreReturnValue #17

@alexander-yevsyukov

Description

@alexander-yevsyukov

Sometimes a message builder is called not as a chain of seXxx() calls but depending on some condition or within an iteration. For example:

    private static <I> InboxAddresses inboxAddressesOf(Set<I> entityIds) {
        var builder = InboxAddresses.newBuilder();
        for (var id : entityIds) {
            var packed = Identifier.pack(id);
            builder.addId(packed);
        }
        return builder.build();
    }

IntelliJ IDEA highlights the call to builder.addId(packed) as the one with the ignored return value. At the time of writing, it is not possible to configure the IDEA settings so that it understands ignoring methods called on a class derived from the one specified in these settings:

Image

Adding com.google.protobuf.Message.Builder with regex for set* and add* methods has no effect. Still, we do want to keep this inspection in IDEA and in other static code analysis tools because usually not treating a result is a mistake made by a programming in a rush. On the other hand, false alarms by IDEA and other tools unnecessary distracts the developer's attention.

To solve this issue we need to add the annotation com.google.errorprone.annotations.CanIgnoreReturnValue to all generated Builder methods that modify the state of the builder such as setXxx(), addXxx(), clearXxx(), etc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions