Skip to content

Add FIELD_EXPLICIT_PRESENCE lint rule #4415

@lberrymage

Description

@lberrymage

Feature

The protocol buffers documentation recommends using optional / explicit presence fields over implicit fields for maximum compatibility with protobuf editions and proto2. It would be nice to be able to enforce this official recommendation with a lint rule to prevent implicit presence fields from accidentally being added to a schema. For example:

syntax = "proto3";

enum Baz {
  BAZ_UNSPECIFIED = 0;
  BAZ_ONE = 1;
}

message Foo {
  // Error: `Field "bar" should be declared "optional".`.
  int32 bar = 1;
  // Error: `Field "baz" should be declared "optional".`
  Baz baz = 2;
  // No error; messages always have explicit presence in proto3.
  Foo foo = 3;
  // No error; messages always have explicit presence in proto3.
  optional Foo foo2 = 4;
}
edition = "2023";

enum Baz {
  BAZ_UNSPECIFIED = 0;
  BAZ_ONE = 1;
}

message Foo {
  // No error; int32 has explicit presence by default in edition 2023.
  int32 bar = 1;
  // No error; enums have explicit presence by default in edition 2023.
  Baz baz = 2;
  // Error: `Field "quux" should use explicit presence.`
  string quux = 3 [features.field_presence = IMPLICIT];
  // No error; field explicitly uses explicit presence, although the annotation
  // is unnecessary in edition 2023.
  uint64 quux2 = 4 [fetaures.field_presence = EXPLICIT];
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions