blob: 735aa169bb338d4b4e41517b8b47e7ef6faff873 [file] [log] [blame]
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +02001// Copyright 2020 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package config
16
17import (
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020018 "fmt"
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +020019 "strings"
20
21 "android/soong/android"
22)
23
24// Overarching principles for Rust lints on Android:
25// The Android build system tries to avoid reporting warnings during the build.
26// Therefore, by default, we upgrade warnings to denials. For some of these
27// lints, an allow exception is setup, using the variables below.
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020028//
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +020029// The lints are split into two categories. The first one contains the built-in
30// lints (https://doc.rust-lang.org/rustc/lints/index.html). The second is
31// specific to Clippy lints (https://rust-lang.github.io/rust-clippy/master/).
32//
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020033// For both categories, there are 3 levels of linting possible:
34// - "android", for the strictest lints that applies to all Android platform code.
35// - "vendor", for relaxed rules.
36// - "none", to disable the linting.
37// There is a fourth option ("default") which automatically selects the linting level
38// based on the module's location. See defaultLintSetForPath.
39//
40// When developing a module, you may set `lints = "none"` and `clippy_lints =
41// "none"` to disable all the linting. Expect some questioning during code review
42// if you enable one of these options.
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +020043var (
44 // Default Rust lints that applies to Google-authored modules.
45 defaultRustcLints = []string{
46 "-A deprecated",
Stephen Hinesf8ffb6d2024-02-09 14:41:03 -080047 "-A unknown_lints",
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +020048 "-D missing-docs",
49 "-D warnings",
Andrew Walbran7d61fae2023-07-06 17:29:23 +010050 "-D unsafe_op_in_unsafe_fn",
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +020051 }
52 // Default Clippy lints. These are applied on top of defaultRustcLints.
53 // It should be assumed that any warning lint will be promoted to a
54 // deny.
55 defaultClippyLints = []string{
Stephen Hines5f8dae52024-02-29 10:12:19 -080056 // Let people hack in peace. ;)
57 "-A clippy::disallowed_names",
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +020058 "-A clippy::type-complexity",
Stephen Hinesf8ffb6d2024-02-09 14:41:03 -080059 "-A clippy::unnecessary_fallible_conversions",
Jeff Vander Stoep41f81572021-02-19 16:51:10 +010060 "-A clippy::unnecessary-wraps",
Ivan Lozanob6d0d9c2021-02-25 11:24:35 -050061 "-A clippy::unusual-byte-groupings",
Jeff Vander Stoep0f36d162021-04-01 19:40:37 +020062 "-A clippy::upper-case-acronyms",
Andrew Walbran7d61fae2023-07-06 17:29:23 +010063 "-D clippy::undocumented_unsafe_blocks",
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +020064 }
65
66 // Rust lints for vendor code.
67 defaultRustcVendorLints = []string{
68 "-A deprecated",
69 "-D warnings",
70 }
71 // Clippy lints for vendor source. These are applied on top of
72 // defaultRustcVendorLints. It should be assumed that any warning lint
73 // will be promoted to a deny.
74 defaultClippyVendorLints = []string{
75 "-A clippy::complexity",
76 "-A clippy::perf",
77 "-A clippy::style",
78 }
79
80 // For prebuilts/ and external/, no linting is expected. If a warning
81 // or a deny is reported, it should be fixed upstream.
82 allowAllLints = []string{
83 "--cap-lints allow",
84 }
85)
86
87func init() {
88 // Default Rust lints. These apply to all Google-authored modules.
89 pctx.VariableFunc("RustDefaultLints", func(ctx android.PackageVarContext) string {
90 if override := ctx.Config().Getenv("RUST_DEFAULT_LINTS"); override != "" {
91 return override
92 }
93 return strings.Join(defaultRustcLints, " ")
94 })
95 pctx.VariableFunc("ClippyDefaultLints", func(ctx android.PackageVarContext) string {
96 if override := ctx.Config().Getenv("CLIPPY_DEFAULT_LINTS"); override != "" {
97 return override
98 }
99 return strings.Join(defaultClippyLints, " ")
100 })
101
102 // Rust lints that only applies to external code.
103 pctx.VariableFunc("RustVendorLints", func(ctx android.PackageVarContext) string {
104 if override := ctx.Config().Getenv("RUST_VENDOR_LINTS"); override != "" {
105 return override
106 }
107 return strings.Join(defaultRustcVendorLints, " ")
108 })
109 pctx.VariableFunc("ClippyVendorLints", func(ctx android.PackageVarContext) string {
110 if override := ctx.Config().Getenv("CLIPPY_VENDOR_LINTS"); override != "" {
111 return override
112 }
113 return strings.Join(defaultClippyVendorLints, " ")
114 })
115 pctx.StaticVariable("RustAllowAllLints", strings.Join(allowAllLints, " "))
116}
117
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +0200118const noLint = ""
119const rustcDefault = "${config.RustDefaultLints}"
120const rustcVendor = "${config.RustVendorLints}"
121const rustcAllowAll = "${config.RustAllowAllLints}"
122const clippyDefault = "${config.ClippyDefaultLints}"
123const clippyVendor = "${config.ClippyVendorLints}"
124
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200125// lintConfig defines a set of lints and clippy configuration.
126type lintConfig struct {
127 rustcConfig string // for the lints to apply to rustc.
128 clippyEnabled bool // to indicate if clippy should be executed.
129 clippyConfig string // for the lints to apply to clippy.
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +0200130}
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200131
132const (
133 androidLints = "android"
134 vendorLints = "vendor"
135 noneLints = "none"
136)
137
138// lintSets defines the categories of linting for Android and their mapping to lintConfigs.
139var lintSets = map[string]lintConfig{
140 androidLints: {rustcDefault, true, clippyDefault},
141 vendorLints: {rustcVendor, true, clippyVendor},
142 noneLints: {rustcAllowAll, false, noLint},
143}
144
145type pathLintSet struct {
146 prefix string
147 set string
148}
149
150// This is a map of local path prefixes to a lint set. The first entry
151// matching will be used. If no entry matches, androidLints ("android") will be
152// used.
153var defaultLintSetForPath = []pathLintSet{
154 {"external", noneLints},
155 {"hardware", vendorLints},
156 {"prebuilts", noneLints},
157 {"vendor/google", androidLints},
158 {"vendor", vendorLints},
159}
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +0200160
161// ClippyLintsForDir returns a boolean if Clippy should be executed and if so, the lints to be used.
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200162func ClippyLintsForDir(dir string, clippyLintsProperty *string) (bool, string, error) {
163 if clippyLintsProperty != nil {
164 set, ok := lintSets[*clippyLintsProperty]
165 if ok {
166 return set.clippyEnabled, set.clippyConfig, nil
167 }
168 if *clippyLintsProperty != "default" {
169 return false, "", fmt.Errorf("unknown value for `clippy_lints`: %v, valid options are: default, android, vendor or none", *clippyLintsProperty)
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +0200170 }
171 }
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200172 for _, p := range defaultLintSetForPath {
173 if strings.HasPrefix(dir, p.prefix) {
174 setConfig := lintSets[p.set]
175 return setConfig.clippyEnabled, setConfig.clippyConfig, nil
176 }
177 }
178 return true, clippyDefault, nil
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +0200179}
180
181// RustcLintsForDir returns the standard lints to be used for a repository.
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200182func RustcLintsForDir(dir string, lintProperty *string) (string, error) {
183 if lintProperty != nil {
184 set, ok := lintSets[*lintProperty]
185 if ok {
186 return set.rustcConfig, nil
187 }
188 if *lintProperty != "default" {
189 return "", fmt.Errorf("unknown value for `lints`: %v, valid options are: default, android, vendor or none", *lintProperty)
190 }
191
192 }
193 for _, p := range defaultLintSetForPath {
194 if strings.HasPrefix(dir, p.prefix) {
195 return lintSets[p.set].rustcConfig, nil
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +0200196 }
197 }
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200198 return rustcDefault, nil
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +0200199}