Skip to content

New check request: flag method annotations on record components when they have no effect #5183

Closed as duplicate

Description

@eamonnmcmanus

Suppose you have this:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Interesting {}

public record Foo(@Interesting int bar, @Interesting String baz) {
  @Override
  public int bar() {
    return this.bar;
  }
}

The annotation on baz has an effect: it will show up in the list returned by Foo.class.getMethod("baz").getAnnotations(), because METHOD-target annotations get copied from a record component to an implicit accessor method. However, they are not copied to an explicit accessor method such as bar(). So the annotation on bar has no effect. It does not appear anywhere in the resultant class-file. Presumably if someone writes something like the above, they mean it to have an effect, so it would make sense for Error Prone to warn them that it doesn't.

Specifically, if you have:

  • an annotation on a record component
  • and the annotation only applies to methods (of the possible places it could apply)
  • and the component has an explicit accessor

then the annotation has no effect and merits a warning.

(This was inspired by #5174, which is specifically about @Override. It turns out that @Override on a record component has no effect whatsoever, and that's a knottier question.)

Activity

  1. eamonnmcmanus commented on Jul 28, 2025

    @eamonnmcmanus
    MemberAuthor

    I'm realizing that this is basically a dup of #5177.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions