6565 // Ignore a specific import when resolving dependencies.
6666 // usage: # gazelle:rust_ignore_import <import name>
6767 ignoreImportDirective string = "rust_ignore_import"
68+
69+ // Extract cargo lints from Cargo.toml files.
70+ // When enabled in generate_from_cargo mode, creates extract_cargo_lints targets
71+ // and adds lint_config attributes to all generated targets.
72+ extractCargoLintsDirective string = "rust_extract_cargo_lints"
6873)
6974
7075type rustConfig struct {
@@ -78,6 +83,7 @@ type rustConfig struct {
7883 DefaultEdition string
7984 SrcsGlob bool
8085 IgnoredImports map [string ]bool
86+ ExtractCargoLints bool
8187}
8288
8389func (cfg * rustConfig ) Clone () * rustConfig {
@@ -121,9 +127,10 @@ func (*rustLang) Name() string { return langName }
121127var (
122128 commonDefs []string = []string {"rust_library" , "rust_binary" , "rust_test" ,
123129 "rust_proc_macro" , "rust_shared_library" , "rust_static_library" }
124- protoDefs []string = []string {"rust_proto_library" , "rust_grpc_library" }
125- prostDefs []string = []string {"rust_prost_library" }
126- cargoDefs []string = []string {"cargo_build_script" }
130+ protoDefs []string = []string {"rust_proto_library" , "rust_grpc_library" }
131+ prostDefs []string = []string {"rust_prost_library" }
132+ cargoDefs []string = []string {"cargo_build_script" }
133+ cargoLintsDefs []string = []string {"extract_cargo_lints" }
127134)
128135
129136var resolvableDefs = append (
@@ -164,6 +171,13 @@ func (*rustLang) Kinds() map[string]rule.KindInfo {
164171 }
165172 }
166173
174+ for _ , cargoLintsDef := range cargoLintsDefs {
175+ kinds [cargoLintsDef ] = rule.KindInfo {
176+ MergeableAttrs : map [string ]bool {},
177+ ResolveAttrs : map [string ]bool {},
178+ }
179+ }
180+
167181 return kinds
168182}
169183
@@ -183,7 +197,7 @@ func (*rustLang) Loads() []rule.LoadInfo {
183197 },
184198 {
185199 Name : "@rules_rust//cargo:defs.bzl" ,
186- Symbols : cargoDefs ,
200+ Symbols : append ( cargoDefs , cargoLintsDefs ... ) ,
187201 },
188202 }
189203}
@@ -201,7 +215,7 @@ func (*rustLang) KnownDirectives() []string {
201215 return []string {modeDirective , lockfileDirective , cargoLockfileDirective ,
202216 cratesPrefixDirective , procMacroOverrideDirective , allowUnusedCrateDirective ,
203217 rustFeatureDirective , defaultFeaturesDirective , defaultEditionDirective ,
204- srcsGlobDirective , ignoreImportDirective }
218+ srcsGlobDirective , ignoreImportDirective , extractCargoLintsDirective }
205219}
206220
207221func (l * rustLang ) GetConfig (c * config.Config ) * rustConfig {
@@ -238,6 +252,7 @@ func (l *rustLang) Configure(c *config.Config, rel string, from *rule.File) {
238252 DefaultEdition : "" ,
239253 SrcsGlob : false ,
240254 IgnoredImports : make (map [string ]bool ),
255+ ExtractCargoLints : false ,
241256 }
242257 } else {
243258 // NOTE(will): important to clone so that we don't leak state across directories
@@ -306,6 +321,13 @@ func (l *rustLang) Configure(c *config.Config, rel string, from *rule.File) {
306321 cfg .SrcsGlob = value
307322 } else if directive .Key == ignoreImportDirective {
308323 cfg .IgnoredImports [directive .Value ] = true
324+ } else if directive .Key == extractCargoLintsDirective {
325+ value , err := strconv .ParseBool (directive .Value )
326+ if err != nil {
327+ l .Log (c , logFatal , "bad %s, should be gazelle:%s <true|false>" ,
328+ directive .Key , directive .Key )
329+ }
330+ cfg .ExtractCargoLints = value
309331 }
310332 }
311333 }
0 commit comments