bootstrap/core/build_steps/
clippy.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! Implementation of running clippy on the compiler, standard library and various tools.

use super::compile::{librustc_stamp, libstd_stamp, run_cargo, rustc_cargo, std_cargo};
use super::tool::{SourceType, prepare_tool_cargo};
use super::{check, compile};
use crate::builder::{Builder, ShouldRun};
use crate::core::build_steps::compile::std_crates_for_run_make;
use crate::core::builder;
use crate::core::builder::{Alias, Kind, RunConfig, Step, crate_description};
use crate::{Mode, Subcommand, TargetSelection};

/// Disable the most spammy clippy lints
const IGNORED_RULES_FOR_STD_AND_RUSTC: &[&str] = &[
    "many_single_char_names", // there are a lot in stdarch
    "collapsible_if",
    "type_complexity",
    "missing_safety_doc", // almost 3K warnings
    "too_many_arguments",
    "needless_lifetimes", // people want to keep the lifetimes
    "wrong_self_convention",
];

fn lint_args(builder: &Builder<'_>, config: &LintConfig, ignored_rules: &[&str]) -> Vec<String> {
    fn strings<'a>(arr: &'a [&str]) -> impl Iterator<Item = String> + 'a {
        arr.iter().copied().map(String::from)
    }

    let Subcommand::Clippy { fix, allow_dirty, allow_staged, .. } = &builder.config.cmd else {
        unreachable!("clippy::lint_args can only be called from `clippy` subcommands.");
    };

    let mut args = vec![];
    if *fix {
        #[rustfmt::skip]
            args.extend(strings(&[
                "--fix", "-Zunstable-options",
                // FIXME: currently, `--fix` gives an error while checking tests for libtest,
                // possibly because libtest is not yet built in the sysroot.
                // As a workaround, avoid checking tests and benches when passed --fix.
                "--lib", "--bins", "--examples",
            ]));

        if *allow_dirty {
            args.push("--allow-dirty".to_owned());
        }

        if *allow_staged {
            args.push("--allow-staged".to_owned());
        }
    }

    args.extend(strings(&["--"]));

    if config.deny.is_empty() && config.forbid.is_empty() {
        args.extend(strings(&["--cap-lints", "warn"]));
    }

    let all_args = std::env::args().collect::<Vec<_>>();
    args.extend(get_clippy_rules_in_order(&all_args, config));

    args.extend(ignored_rules.iter().map(|lint| format!("-Aclippy::{}", lint)));
    args.extend(builder.config.free_args.clone());
    args
}

/// We need to keep the order of the given clippy lint rules before passing them.
/// Since clap doesn't offer any useful interface for this purpose out of the box,
/// we have to handle it manually.
pub fn get_clippy_rules_in_order(all_args: &[String], config: &LintConfig) -> Vec<String> {
    let mut result = vec![];

    for (prefix, item) in
        [("-A", &config.allow), ("-D", &config.deny), ("-W", &config.warn), ("-F", &config.forbid)]
    {
        item.iter().for_each(|v| {
            let rule = format!("{prefix}{v}");
            // Arguments added by bootstrap in LintConfig won't show up in the all_args list, so
            // put them at the end of the command line.
            let position = all_args.iter().position(|t| t == &rule || t == v).unwrap_or(usize::MAX);
            result.push((position, rule));
        });
    }

    result.sort_by_key(|&(position, _)| position);
    result.into_iter().map(|v| v.1).collect()
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct LintConfig {
    pub allow: Vec<String>,
    pub warn: Vec<String>,
    pub deny: Vec<String>,
    pub forbid: Vec<String>,
}

impl LintConfig {
    fn new(builder: &Builder<'_>) -> Self {
        match builder.config.cmd.clone() {
            Subcommand::Clippy { allow, deny, warn, forbid, .. } => {
                Self { allow, warn, deny, forbid }
            }
            _ => unreachable!("LintConfig can only be called from `clippy` subcommands."),
        }
    }

    fn merge(&self, other: &Self) -> Self {
        let merged = |self_attr: &[String], other_attr: &[String]| -> Vec<String> {
            self_attr.iter().cloned().chain(other_attr.iter().cloned()).collect()
        };
        // This is written this way to ensure we get a compiler error if we add a new field.
        Self {
            allow: merged(&self.allow, &other.allow),
            warn: merged(&self.warn, &other.warn),
            deny: merged(&self.deny, &other.deny),
            forbid: merged(&self.forbid, &other.forbid),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Std {
    pub target: TargetSelection,
    config: LintConfig,
    /// Whether to lint only a subset of crates.
    crates: Vec<String>,
}

impl Step for Std {
    type Output = ();
    const DEFAULT: bool = true;

    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
        run.crate_or_deps("sysroot").path("library")
    }

    fn make_run(run: RunConfig<'_>) {
        let crates = std_crates_for_run_make(&run);
        let config = LintConfig::new(run.builder);
        run.builder.ensure(Std { target: run.target, config, crates });
    }

    fn run(self, builder: &Builder<'_>) {
        builder.require_submodule("library/stdarch", None);

        let target = self.target;
        let compiler = builder.compiler(builder.top_stage, builder.config.build);

        let mut cargo = builder::Cargo::new(
            builder,
            compiler,
            Mode::Std,
            SourceType::InTree,
            target,
            Kind::Clippy,
        );

        std_cargo(builder, target, compiler.stage, &mut cargo);

        for krate in &*self.crates {
            cargo.arg("-p").arg(krate);
        }

        let _guard =
            builder.msg_clippy(format_args!("library{}", crate_description(&self.crates)), target);

        run_cargo(
            builder,
            cargo,
            lint_args(builder, &self.config, IGNORED_RULES_FOR_STD_AND_RUSTC),
            &libstd_stamp(builder, compiler, target),
            vec![],
            true,
            false,
        );
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Rustc {
    pub target: TargetSelection,
    config: LintConfig,
    /// Whether to lint only a subset of crates.
    crates: Vec<String>,
}

impl Step for Rustc {
    type Output = ();
    const ONLY_HOSTS: bool = true;
    const DEFAULT: bool = true;

    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
        run.crate_or_deps("rustc-main").path("compiler")
    }

    fn make_run(run: RunConfig<'_>) {
        let crates = run.make_run_crates(Alias::Compiler);
        let config = LintConfig::new(run.builder);
        run.builder.ensure(Rustc { target: run.target, config, crates });
    }

    /// Lints the compiler.
    ///
    /// This will lint the compiler for a particular stage of the build using
    /// the `compiler` targeting the `target` architecture.
    fn run(self, builder: &Builder<'_>) {
        let compiler = builder.compiler(builder.top_stage, builder.config.build);
        let target = self.target;

        if compiler.stage != 0 {
            // If we're not in stage 0, then we won't have a std from the beta
            // compiler around. That means we need to make sure there's one in
            // the sysroot for the compiler to find. Otherwise, we're going to
            // fail when building crates that need to generate code (e.g., build
            // scripts and their dependencies).
            builder.ensure(compile::Std::new(compiler, compiler.host));
            builder.ensure(compile::Std::new(compiler, target));
        } else {
            builder.ensure(check::Std::new_with_build_kind(target, Some(Kind::Check)));
        }

        let mut cargo = builder::Cargo::new(
            builder,
            compiler,
            Mode::Rustc,
            SourceType::InTree,
            target,
            Kind::Clippy,
        );

        rustc_cargo(builder, &mut cargo, target, &compiler, &self.crates);

        // Explicitly pass -p for all compiler crates -- this will force cargo
        // to also lint the tests/benches/examples for these crates, rather
        // than just the leaf crate.
        for krate in &*self.crates {
            cargo.arg("-p").arg(krate);
        }

        let _guard =
            builder.msg_clippy(format_args!("compiler{}", crate_description(&self.crates)), target);

        run_cargo(
            builder,
            cargo,
            lint_args(builder, &self.config, IGNORED_RULES_FOR_STD_AND_RUSTC),
            &librustc_stamp(builder, compiler, target),
            vec![],
            true,
            false,
        );
    }
}

macro_rules! lint_any {
    ($(
        $name:ident, $path:expr, $readable_name:expr
        $(,lint_by_default = $lint_by_default:expr)*
        ;
    )+) => {
        $(

        #[derive(Debug, Clone, Hash, PartialEq, Eq)]
        pub struct $name {
            pub target: TargetSelection,
            config: LintConfig,
        }

        impl Step for $name {
            type Output = ();
            const DEFAULT: bool = if false $(|| $lint_by_default)* { true } else { false };

            fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
                run.path($path)
            }

            fn make_run(run: RunConfig<'_>) {
                let config = LintConfig::new(run.builder);
                run.builder.ensure($name {
                    target: run.target,
                    config,
                });
            }

            fn run(self, builder: &Builder<'_>) -> Self::Output {
                let compiler = builder.compiler(builder.top_stage, builder.config.build);
                let target = self.target;

                builder.ensure(check::Rustc::new_with_build_kind(target, builder, Some(Kind::Check)));

                let cargo = prepare_tool_cargo(
                    builder,
                    compiler,
                    Mode::ToolRustc,
                    target,
                    Kind::Clippy,
                    $path,
                    SourceType::InTree,
                    &[],
                );

                let _guard = builder.msg_tool(
                    Kind::Clippy,
                    Mode::ToolRustc,
                    $readable_name,
                    compiler.stage,
                    &compiler.host,
                    &target,
                );

                let stamp = builder
                    .cargo_out(compiler, Mode::ToolRustc, target)
                    .join(format!(".{}-check.stamp", stringify!($name).to_lowercase()));

                run_cargo(
                    builder,
                    cargo,
                    lint_args(builder, &self.config, &[]),
                    &stamp,
                    vec![],
                    true,
                    false,
                );
            }
        }
        )+
    }
}

lint_any!(
    Bootstrap, "src/bootstrap", "bootstrap";
    BuildHelper, "src/build_helper", "build_helper";
    BuildManifest, "src/tools/build-manifest", "build-manifest";
    CargoMiri, "src/tools/miri/cargo-miri", "cargo-miri";
    Clippy, "src/tools/clippy", "clippy";
    CollectLicenseMetadata, "src/tools/collect-license-metadata", "collect-license-metadata";
    Compiletest, "src/tools/compiletest", "compiletest";
    CoverageDump, "src/tools/coverage-dump", "coverage-dump";
    Jsondocck, "src/tools/jsondocck", "jsondocck";
    Jsondoclint, "src/tools/jsondoclint", "jsondoclint";
    LintDocs, "src/tools/lint-docs", "lint-docs";
    LlvmBitcodeLinker, "src/tools/llvm-bitcode-linker", "llvm-bitcode-linker";
    Miri, "src/tools/miri", "miri";
    MiroptTestTools, "src/tools/miropt-test-tools", "miropt-test-tools";
    OptDist, "src/tools/opt-dist", "opt-dist";
    RemoteTestClient, "src/tools/remote-test-client", "remote-test-client";
    RemoteTestServer, "src/tools/remote-test-server", "remote-test-server";
    Rls, "src/tools/rls", "rls";
    RustAnalyzer, "src/tools/rust-analyzer", "rust-analyzer";
    Rustdoc, "src/librustdoc", "clippy";
    Rustfmt, "src/tools/rustfmt", "rustfmt";
    RustInstaller, "src/tools/rust-installer", "rust-installer";
    Tidy, "src/tools/tidy", "tidy";
    TestFloatParse, "src/etc/test-float-parse", "test-float-parse";
);

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CI {
    target: TargetSelection,
    config: LintConfig,
}

impl Step for CI {
    type Output = ();
    const DEFAULT: bool = false;

    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
        run.alias("ci")
    }

    fn make_run(run: RunConfig<'_>) {
        let config = LintConfig::new(run.builder);
        run.builder.ensure(CI { target: run.target, config });
    }

    fn run(self, builder: &Builder<'_>) -> Self::Output {
        builder.ensure(Bootstrap {
            target: self.target,
            config: self.config.merge(&LintConfig {
                allow: vec![],
                warn: vec![],
                deny: vec!["warnings".into()],
                forbid: vec![],
            }),
        });
        let library_clippy_cfg = LintConfig {
            allow: vec!["clippy::all".into()],
            warn: vec![],
            deny: vec!["clippy::correctness".into()],
            forbid: vec![],
        };
        let compiler_clippy_cfg = LintConfig {
            allow: vec!["clippy::all".into()],
            warn: vec![],
            deny: vec!["clippy::correctness".into(), "clippy::clone_on_ref_ptr".into()],
            forbid: vec![],
        };

        builder.ensure(Std {
            target: self.target,
            config: self.config.merge(&library_clippy_cfg),
            crates: vec![],
        });
        builder.ensure(Rustc {
            target: self.target,
            config: self.config.merge(&compiler_clippy_cfg),
            crates: vec![],
        });
    }
}