• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// 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 (
18	"fmt"
19	"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.
28//
29// 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//
33// 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.
43var (
44	// Default Rust lints that applies to Google-authored modules.
45	defaultRustcLints = []string{
46		"-A deprecated",
47		"-A unknown_lints",
48		"-D missing-docs",
49		"-D warnings",
50		"-D unsafe_op_in_unsafe_fn",
51	}
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{
56		// Let people hack in peace. ;)
57		"-A clippy::disallowed_names",
58		"-A clippy::empty_line_after_doc_comments",
59		"-A clippy::type-complexity",
60		"-A clippy::unnecessary_fallible_conversions",
61		"-A clippy::unnecessary-wraps",
62		"-A clippy::unusual-byte-groupings",
63		"-A clippy::upper-case-acronyms",
64		"-D clippy::undocumented_unsafe_blocks",
65	}
66
67	// Rust lints for vendor code.
68	defaultRustcVendorLints = []string{
69		"-A deprecated",
70		"-D warnings",
71	}
72	// Clippy lints for vendor source. These are applied on top of
73	// defaultRustcVendorLints.  It should be assumed that any warning lint
74	// will be promoted to a deny.
75	defaultClippyVendorLints = []string{
76		"-A clippy::complexity",
77		"-A clippy::perf",
78		"-A clippy::style",
79	}
80
81	// For prebuilts/ and external/, no linting is expected. If a warning
82	// or a deny is reported, it should be fixed upstream.
83	allowAllLints = []string{
84		"--cap-lints allow",
85	}
86)
87
88func init() {
89	// Default Rust lints. These apply to all Google-authored modules.
90	pctx.VariableFunc("RustDefaultLints", func(ctx android.PackageVarContext) string {
91		if override := ctx.Config().Getenv("RUST_DEFAULT_LINTS"); override != "" {
92			return override
93		}
94		return strings.Join(defaultRustcLints, " ")
95	})
96	pctx.VariableFunc("ClippyDefaultLints", func(ctx android.PackageVarContext) string {
97		if override := ctx.Config().Getenv("CLIPPY_DEFAULT_LINTS"); override != "" {
98			return override
99		}
100		return strings.Join(defaultClippyLints, " ")
101	})
102
103	// Rust lints that only applies to external code.
104	pctx.VariableFunc("RustVendorLints", func(ctx android.PackageVarContext) string {
105		if override := ctx.Config().Getenv("RUST_VENDOR_LINTS"); override != "" {
106			return override
107		}
108		return strings.Join(defaultRustcVendorLints, " ")
109	})
110	pctx.VariableFunc("ClippyVendorLints", func(ctx android.PackageVarContext) string {
111		if override := ctx.Config().Getenv("CLIPPY_VENDOR_LINTS"); override != "" {
112			return override
113		}
114		return strings.Join(defaultClippyVendorLints, " ")
115	})
116	pctx.StaticVariable("RustAllowAllLints", strings.Join(allowAllLints, " "))
117}
118
119const noLint = ""
120const rustcDefault = "${config.RustDefaultLints}"
121const rustcVendor = "${config.RustVendorLints}"
122const rustcAllowAll = "${config.RustAllowAllLints}"
123const clippyDefault = "${config.ClippyDefaultLints}"
124const clippyVendor = "${config.ClippyVendorLints}"
125
126// lintConfig defines a set of lints and clippy configuration.
127type lintConfig struct {
128	rustcConfig   string // for the lints to apply to rustc.
129	clippyEnabled bool   // to indicate if clippy should be executed.
130	clippyConfig  string // for the lints to apply to clippy.
131}
132
133const (
134	androidLints = "android"
135	vendorLints  = "vendor"
136	noneLints    = "none"
137)
138
139// lintSets defines the categories of linting for Android and their mapping to lintConfigs.
140var lintSets = map[string]lintConfig{
141	androidLints: {rustcDefault, true, clippyDefault},
142	vendorLints:  {rustcVendor, true, clippyVendor},
143	noneLints:    {rustcAllowAll, false, noLint},
144}
145
146type pathLintSet struct {
147	prefix string
148	set    string
149}
150
151// This is a map of local path prefixes to a lint set.  The first entry
152// matching will be used. If no entry matches, androidLints ("android") will be
153// used.
154var defaultLintSetForPath = []pathLintSet{
155	{"external", noneLints},
156	{"hardware", vendorLints},
157	{"prebuilts", noneLints},
158	{"vendor/google", androidLints},
159	{"vendor", vendorLints},
160}
161
162// ClippyLintsForDir returns a boolean if Clippy should be executed and if so, the lints to be used.
163func ClippyLintsForDir(dir string, clippyLintsProperty *string) (bool, string, error) {
164	if clippyLintsProperty != nil {
165		set, ok := lintSets[*clippyLintsProperty]
166		if ok {
167			return set.clippyEnabled, set.clippyConfig, nil
168		}
169		if *clippyLintsProperty != "default" {
170			return false, "", fmt.Errorf("unknown value for `clippy_lints`: %v, valid options are: default, android, vendor or none", *clippyLintsProperty)
171		}
172	}
173	for _, p := range defaultLintSetForPath {
174		if strings.HasPrefix(dir, p.prefix) {
175			setConfig := lintSets[p.set]
176			return setConfig.clippyEnabled, setConfig.clippyConfig, nil
177		}
178	}
179	return true, clippyDefault, nil
180}
181
182// RustcLintsForDir returns the standard lints to be used for a repository.
183func RustcLintsForDir(dir string, lintProperty *string) (string, error) {
184	if lintProperty != nil {
185		set, ok := lintSets[*lintProperty]
186		if ok {
187			return set.rustcConfig, nil
188		}
189		if *lintProperty != "default" {
190			return "", fmt.Errorf("unknown value for `lints`: %v, valid options are: default, android, vendor or none", *lintProperty)
191		}
192
193	}
194	for _, p := range defaultLintSetForPath {
195		if strings.HasPrefix(dir, p.prefix) {
196			return lintSets[p.set].rustcConfig, nil
197		}
198	}
199	return rustcDefault, nil
200}
201