Skip to content

Commit 9f51084

Browse files
authored
feat(sidekick/rust): disable some clippy warnings (#2567)
Clippy warns about the indentation of some markdown text. The *output* of the markdown looks fine, it is just that the comments are less readable with bad indentation. This PR will disable that warning for all generated clients. Trying to fix the comments is not worth it.
1 parent 71a4d07 commit 9f51084

File tree

5 files changed

+111
-15
lines changed

5 files changed

+111
-15
lines changed

internal/sidekick/internal/rust/annotate.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ type modelAnnotations struct {
4646
Services []*api.Service
4747
NameToLower string
4848
NotForPublication bool
49-
// If true, disable rustdoc warnings known to be triggered by our generated
50-
// documentation.
49+
// A list of `#[allow(rustdoc::*)]` warnings to disable.
5150
DisabledRustdocWarnings []string
51+
// A list of `#[allow(clippy::*)]` warnings to disable.
52+
DisabledClippyWarnings []string
5253
// Sets the default system parameters.
5354
DefaultSystemParameters []systemParameter
5455
// Enables per-service features.
@@ -594,6 +595,7 @@ func annotateModel(model *api.API, codec *codec) *modelAnnotations {
594595
NameToLower: strings.ToLower(model.Name),
595596
NotForPublication: codec.doNotPublish,
596597
DisabledRustdocWarnings: codec.disabledRustdocWarnings,
598+
DisabledClippyWarnings: codec.disabledClippyWarnings,
597599
PerServiceFeatures: codec.perServiceFeatures && len(servicesSubset) > 0,
598600
ExtraModules: codec.extraModules,
599601
Incomplete: slices.ContainsFunc(model.Services, func(s *api.Service) bool {

internal/sidekick/internal/rust/annotate_model_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,76 @@ func TestDefaultFeatures(t *testing.T) {
6666
}
6767
}
6868

69+
func TestRustdocWarnings(t *testing.T) {
70+
for _, test := range []struct {
71+
Options map[string]string
72+
Want []string
73+
}{
74+
{
75+
Options: map[string]string{},
76+
Want: nil,
77+
},
78+
{
79+
Options: map[string]string{
80+
"disabled-rustdoc-warnings": "",
81+
},
82+
Want: []string{},
83+
},
84+
{
85+
Options: map[string]string{
86+
"disabled-rustdoc-warnings": "a,b,c",
87+
},
88+
Want: []string{"a", "b", "c"},
89+
},
90+
} {
91+
model := newTestAnnotateModelAPI()
92+
codec, err := newCodec("protobuf", test.Options)
93+
if err != nil {
94+
t.Fatal(err)
95+
}
96+
got := annotateModel(model, codec)
97+
t.Logf("Options=%v", test.Options)
98+
if diff := cmp.Diff(test.Want, got.DisabledRustdocWarnings); diff != "" {
99+
t.Errorf("mismatch (-want, +got):\n%s", diff)
100+
}
101+
}
102+
}
103+
104+
func TestClippyWarnings(t *testing.T) {
105+
for _, test := range []struct {
106+
Options map[string]string
107+
Want []string
108+
}{
109+
{
110+
Options: map[string]string{},
111+
Want: nil,
112+
},
113+
{
114+
Options: map[string]string{
115+
"disabled-clippy-warnings": "",
116+
},
117+
Want: []string{},
118+
},
119+
{
120+
Options: map[string]string{
121+
"disabled-clippy-warnings": "a,b,c",
122+
},
123+
Want: []string{"a", "b", "c"},
124+
},
125+
} {
126+
model := newTestAnnotateModelAPI()
127+
codec, err := newCodec("protobuf", test.Options)
128+
if err != nil {
129+
t.Fatal(err)
130+
}
131+
got := annotateModel(model, codec)
132+
t.Logf("Options=%v", test.Options)
133+
if diff := cmp.Diff(test.Want, got.DisabledClippyWarnings); diff != "" {
134+
t.Errorf("mismatch (-want, +got):\n%s", diff)
135+
}
136+
}
137+
}
138+
69139
func newTestAnnotateModelAPI() *api.API {
70140
service0 := &api.Service{
71141
Name: "Service0",

internal/sidekick/internal/rust/codec.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,9 @@ func newCodec(specificationFormat string, options map[string]string) (*codec, er
113113
codec.packageMapping[source] = pkgOption.pkg
114114
}
115115
case key == "disabled-rustdoc-warnings":
116-
if definition == "" {
117-
codec.disabledRustdocWarnings = []string{}
118-
} else {
119-
codec.disabledRustdocWarnings = strings.Split(definition, ",")
120-
}
116+
codec.disabledRustdocWarnings = splitOption(definition)
117+
case key == "disabled-clippy-warnings":
118+
codec.disabledClippyWarnings = splitOption(definition)
121119
case key == "template-override":
122120
codec.templateOverride = definition
123121
case key == "include-grpc-only-methods":
@@ -133,11 +131,7 @@ func newCodec(specificationFormat string, options map[string]string) (*codec, er
133131
}
134132
codec.perServiceFeatures = value
135133
case key == "default-features":
136-
if definition == "" {
137-
codec.defaultFeatures = []string{}
138-
} else {
139-
codec.defaultFeatures = strings.Split(definition, ",")
140-
}
134+
codec.defaultFeatures = splitOption(definition)
141135
case key == "detailed-tracing-attributes":
142136
value, err := strconv.ParseBool(definition)
143137
if err != nil {
@@ -151,9 +145,9 @@ func newCodec(specificationFormat string, options map[string]string) (*codec, er
151145
}
152146
codec.hasVeneer = value
153147
case key == "extra-modules":
154-
codec.extraModules = strings.Split(definition, ",")
148+
codec.extraModules = splitOption(definition)
155149
case key == "internal-types":
156-
codec.internalTypes = strings.Split(definition, ",")
150+
codec.internalTypes = splitOption(definition)
157151
case key == "routing-required":
158152
value, err := strconv.ParseBool(definition)
159153
if err != nil {
@@ -173,6 +167,13 @@ func newCodec(specificationFormat string, options map[string]string) (*codec, er
173167
return codec, nil
174168
}
175169

170+
func splitOption(definition string) []string {
171+
if definition == "" {
172+
return []string{}
173+
}
174+
return strings.Split(definition, ",")
175+
}
176+
176177
type packageOption struct {
177178
pkg *packagez
178179
otherNames []string
@@ -254,8 +255,10 @@ type codec struct {
254255
releaseLevel string
255256
// True if the API model includes any services
256257
hasServices bool
257-
// A list of `rustdoc` warnings disabled for specific services.
258+
// A list of `rustdoc` warnings to disable.
258259
disabledRustdocWarnings []string
260+
// A list of `clippy` warnings to disable.
261+
disabledClippyWarnings []string
259262
// The default system parameters included in all requests.
260263
systemParameters []systemParameter
261264
// If true, enums are serialized as strings.

internal/sidekick/internal/rust/codec_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,24 @@ func TestParseOptions(t *testing.T) {
179179
c.disabledRustdocWarnings = []string{"a", "b", "c"}
180180
},
181181
},
182+
{
183+
Format: "protobuf",
184+
Options: map[string]string{
185+
"disabled-clippy-warnings": "",
186+
},
187+
Update: func(c *codec) {
188+
c.disabledClippyWarnings = []string{}
189+
},
190+
},
191+
{
192+
Format: "protobuf",
193+
Options: map[string]string{
194+
"disabled-clippy-warnings": "a,b,c",
195+
},
196+
Update: func(c *codec) {
197+
c.disabledClippyWarnings = []string{"a", "b", "c"}
198+
},
199+
},
182200
{
183201
Format: "protobuf",
184202
Options: map[string]string{

internal/sidekick/internal/rust/templates/common/model.rs.mustache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ limitations under the License.
2222
{{#Codec.DisabledRustdocWarnings}}
2323
#![allow(rustdoc::{{.}})]
2424
{{/Codec.DisabledRustdocWarnings}}
25+
{{#Codec.DisabledClippyWarnings}}
26+
#![allow(clippy::{{.}})]
27+
{{/Codec.DisabledClippyWarnings}}
2528
#![no_implicit_prelude]
2629
extern crate std;
2730
{{#Codec.ExternPackages}}

0 commit comments

Comments
 (0)