diff --git a/internal/sidekick/internal/rust/annotate.go b/internal/sidekick/internal/rust/annotate.go index 8876397b25..016712ed5b 100644 --- a/internal/sidekick/internal/rust/annotate.go +++ b/internal/sidekick/internal/rust/annotate.go @@ -46,9 +46,10 @@ type modelAnnotations struct { Services []*api.Service NameToLower string NotForPublication bool - // If true, disable rustdoc warnings known to be triggered by our generated - // documentation. + // A list of `#[allow(rustdoc::*)]` warnings to disable. DisabledRustdocWarnings []string + // A list of `#[allow(clippy::*)]` warnings to disable. + DisabledClippyWarnings []string // Sets the default system parameters. DefaultSystemParameters []systemParameter // Enables per-service features. @@ -587,6 +588,7 @@ func annotateModel(model *api.API, codec *codec) *modelAnnotations { NameToLower: strings.ToLower(model.Name), NotForPublication: codec.doNotPublish, DisabledRustdocWarnings: codec.disabledRustdocWarnings, + DisabledClippyWarnings: codec.disabledClippyWarnings, PerServiceFeatures: codec.perServiceFeatures && len(servicesSubset) > 0, ExtraModules: codec.extraModules, Incomplete: slices.ContainsFunc(model.Services, func(s *api.Service) bool { diff --git a/internal/sidekick/internal/rust/annotate_model_test.go b/internal/sidekick/internal/rust/annotate_model_test.go index cff8dee4db..8ba857382b 100644 --- a/internal/sidekick/internal/rust/annotate_model_test.go +++ b/internal/sidekick/internal/rust/annotate_model_test.go @@ -66,6 +66,76 @@ func TestDefaultFeatures(t *testing.T) { } } +func TestRustdocWarnings(t *testing.T) { + for _, test := range []struct { + Options map[string]string + Want []string + }{ + { + Options: map[string]string{}, + Want: nil, + }, + { + Options: map[string]string{ + "disabled-rustdoc-warnings": "", + }, + Want: []string{}, + }, + { + Options: map[string]string{ + "disabled-rustdoc-warnings": "a,b,c", + }, + Want: []string{"a", "b", "c"}, + }, + } { + model := newTestAnnotateModelAPI() + codec, err := newCodec("protobuf", test.Options) + if err != nil { + t.Fatal(err) + } + got := annotateModel(model, codec) + t.Logf("Options=%v", test.Options) + if diff := cmp.Diff(test.Want, got.DisabledRustdocWarnings); diff != "" { + t.Errorf("mismatch (-want, +got):\n%s", diff) + } + } +} + +func TestClippyWarnings(t *testing.T) { + for _, test := range []struct { + Options map[string]string + Want []string + }{ + { + Options: map[string]string{}, + Want: nil, + }, + { + Options: map[string]string{ + "disabled-clippy-warnings": "", + }, + Want: []string{}, + }, + { + Options: map[string]string{ + "disabled-clippy-warnings": "a,b,c", + }, + Want: []string{"a", "b", "c"}, + }, + } { + model := newTestAnnotateModelAPI() + codec, err := newCodec("protobuf", test.Options) + if err != nil { + t.Fatal(err) + } + got := annotateModel(model, codec) + t.Logf("Options=%v", test.Options) + if diff := cmp.Diff(test.Want, got.DisabledClippyWarnings); diff != "" { + t.Errorf("mismatch (-want, +got):\n%s", diff) + } + } +} + func newTestAnnotateModelAPI() *api.API { service0 := &api.Service{ Name: "Service0", diff --git a/internal/sidekick/internal/rust/codec.go b/internal/sidekick/internal/rust/codec.go index 524da50b37..39fcda935b 100644 --- a/internal/sidekick/internal/rust/codec.go +++ b/internal/sidekick/internal/rust/codec.go @@ -113,11 +113,9 @@ func newCodec(specificationFormat string, options map[string]string) (*codec, er codec.packageMapping[source] = pkgOption.pkg } case key == "disabled-rustdoc-warnings": - if definition == "" { - codec.disabledRustdocWarnings = []string{} - } else { - codec.disabledRustdocWarnings = strings.Split(definition, ",") - } + codec.disabledRustdocWarnings = splitOption(definition) + case key == "disabled-clippy-warnings": + codec.disabledClippyWarnings = splitOption(definition) case key == "template-override": codec.templateOverride = definition case key == "include-grpc-only-methods": @@ -133,11 +131,7 @@ func newCodec(specificationFormat string, options map[string]string) (*codec, er } codec.perServiceFeatures = value case key == "default-features": - if definition == "" { - codec.defaultFeatures = []string{} - } else { - codec.defaultFeatures = strings.Split(definition, ",") - } + codec.defaultFeatures = splitOption(definition) case key == "detailed-tracing-attributes": value, err := strconv.ParseBool(definition) if err != nil { @@ -151,9 +145,9 @@ func newCodec(specificationFormat string, options map[string]string) (*codec, er } codec.hasVeneer = value case key == "extra-modules": - codec.extraModules = strings.Split(definition, ",") + codec.extraModules = splitOption(definition) case key == "internal-types": - codec.internalTypes = strings.Split(definition, ",") + codec.internalTypes = splitOption(definition) case key == "routing-required": value, err := strconv.ParseBool(definition) if err != nil { @@ -173,6 +167,13 @@ func newCodec(specificationFormat string, options map[string]string) (*codec, er return codec, nil } +func splitOption(definition string) []string { + if definition == "" { + return []string{} + } + return strings.Split(definition, ",") +} + type packageOption struct { pkg *packagez otherNames []string @@ -254,8 +255,10 @@ type codec struct { releaseLevel string // True if the API model includes any services hasServices bool - // A list of `rustdoc` warnings disabled for specific services. + // A list of `rustdoc` warnings to disable. disabledRustdocWarnings []string + // A list of `clippy` warnings to disable. + disabledClippyWarnings []string // The default system parameters included in all requests. systemParameters []systemParameter // If true, enums are serialized as strings. diff --git a/internal/sidekick/internal/rust/codec_test.go b/internal/sidekick/internal/rust/codec_test.go index 94f80f3047..4a97a53b49 100644 --- a/internal/sidekick/internal/rust/codec_test.go +++ b/internal/sidekick/internal/rust/codec_test.go @@ -179,6 +179,24 @@ func TestParseOptions(t *testing.T) { c.disabledRustdocWarnings = []string{"a", "b", "c"} }, }, + { + Format: "protobuf", + Options: map[string]string{ + "disabled-clippy-warnings": "", + }, + Update: func(c *codec) { + c.disabledClippyWarnings = []string{} + }, + }, + { + Format: "protobuf", + Options: map[string]string{ + "disabled-clippy-warnings": "a,b,c", + }, + Update: func(c *codec) { + c.disabledClippyWarnings = []string{"a", "b", "c"} + }, + }, { Format: "protobuf", Options: map[string]string{ diff --git a/internal/sidekick/internal/rust/templates/common/model.rs.mustache b/internal/sidekick/internal/rust/templates/common/model.rs.mustache index 25014faae7..f9126b4b78 100644 --- a/internal/sidekick/internal/rust/templates/common/model.rs.mustache +++ b/internal/sidekick/internal/rust/templates/common/model.rs.mustache @@ -22,6 +22,9 @@ limitations under the License. {{#Codec.DisabledRustdocWarnings}} #![allow(rustdoc::{{.}})] {{/Codec.DisabledRustdocWarnings}} +{{#Codec.DisabledClippyWarnings}} +#![allow(clippy::{{.}})] +{{/Codec.DisabledClippyWarnings}} #![no_implicit_prelude] extern crate std; {{#Codec.ExternPackages}}