bootstrap/core/build_steps/
clippy.rs

1//! Implementation of running clippy on the compiler, standard library and various tools.
2//!
3//! This serves a double purpose:
4//! - The first is to run Clippy itself on in-tree code, in order to test and dogfood it.
5//! - The second is to actually lint the in-tree codebase on CI, with a hard-coded set of rules,
6//!   which is performed by the `x clippy ci` command.
7//!
8//! In order to prepare a build compiler for running clippy, use the
9//! [prepare_compiler_for_check] function. That prepares a
10//! compiler and a standard library
11//! for running Clippy. The second part (actually building Clippy) is performed inside
12//! [Builder::cargo_clippy_cmd]. It would be nice if this was more explicit, and we actually had
13//! to pass a prebuilt Clippy from the outside when running `cargo clippy`, but that would be
14//! (as usual) a massive undertaking/refactoring.
15
16use build_helper::exit;
17
18use super::compile::{run_cargo, rustc_cargo, std_cargo};
19use super::tool::{SourceType, prepare_tool_cargo};
20use crate::builder::{Builder, ShouldRun};
21use crate::core::build_steps::check::{CompilerForCheck, prepare_compiler_for_check};
22use crate::core::build_steps::compile::std_crates_for_run_make;
23use crate::core::builder;
24use crate::core::builder::{Alias, Kind, RunConfig, Step, StepMetadata, crate_description};
25use crate::utils::build_stamp::{self, BuildStamp};
26use crate::{Compiler, Mode, Subcommand, TargetSelection};
27
28/// Disable the most spammy clippy lints
29const IGNORED_RULES_FOR_STD_AND_RUSTC: &[&str] = &[
30    "many_single_char_names", // there are a lot in stdarch
31    "collapsible_if",
32    "type_complexity",
33    "missing_safety_doc", // almost 3K warnings
34    "too_many_arguments",
35    "needless_lifetimes", // people want to keep the lifetimes
36    "wrong_self_convention",
37    "approx_constant", // libcore is what defines those
38];
39
40fn lint_args(builder: &Builder<'_>, config: &LintConfig, ignored_rules: &[&str]) -> Vec<String> {
41    fn strings<'a>(arr: &'a [&str]) -> impl Iterator<Item = String> + 'a {
42        arr.iter().copied().map(String::from)
43    }
44
45    let Subcommand::Clippy { fix, allow_dirty, allow_staged, .. } = &builder.config.cmd else {
46        unreachable!("clippy::lint_args can only be called from `clippy` subcommands.");
47    };
48
49    let mut args = vec![];
50    if *fix {
51        #[rustfmt::skip]
52            args.extend(strings(&[
53                "--fix", "-Zunstable-options",
54                // FIXME: currently, `--fix` gives an error while checking tests for libtest,
55                // possibly because libtest is not yet built in the sysroot.
56                // As a workaround, avoid checking tests and benches when passed --fix.
57                "--lib", "--bins", "--examples",
58            ]));
59
60        if *allow_dirty {
61            args.push("--allow-dirty".to_owned());
62        }
63
64        if *allow_staged {
65            args.push("--allow-staged".to_owned());
66        }
67    }
68
69    args.extend(strings(&["--"]));
70
71    if config.deny.is_empty() && config.forbid.is_empty() {
72        args.extend(strings(&["--cap-lints", "warn"]));
73    }
74
75    let all_args = std::env::args().collect::<Vec<_>>();
76    args.extend(get_clippy_rules_in_order(&all_args, config));
77
78    args.extend(ignored_rules.iter().map(|lint| format!("-Aclippy::{lint}")));
79    args.extend(builder.config.free_args.clone());
80    args
81}
82
83/// We need to keep the order of the given clippy lint rules before passing them.
84/// Since clap doesn't offer any useful interface for this purpose out of the box,
85/// we have to handle it manually.
86pub fn get_clippy_rules_in_order(all_args: &[String], config: &LintConfig) -> Vec<String> {
87    let mut result = vec![];
88
89    for (prefix, item) in
90        [("-A", &config.allow), ("-D", &config.deny), ("-W", &config.warn), ("-F", &config.forbid)]
91    {
92        item.iter().for_each(|v| {
93            let rule = format!("{prefix}{v}");
94            // Arguments added by bootstrap in LintConfig won't show up in the all_args list, so
95            // put them at the end of the command line.
96            let position = all_args.iter().position(|t| t == &rule || t == v).unwrap_or(usize::MAX);
97            result.push((position, rule));
98        });
99    }
100
101    result.sort_by_key(|&(position, _)| position);
102    result.into_iter().map(|v| v.1).collect()
103}
104
105#[derive(Debug, Clone, PartialEq, Eq, Hash)]
106pub struct LintConfig {
107    pub allow: Vec<String>,
108    pub warn: Vec<String>,
109    pub deny: Vec<String>,
110    pub forbid: Vec<String>,
111}
112
113impl LintConfig {
114    fn new(builder: &Builder<'_>) -> Self {
115        match builder.config.cmd.clone() {
116            Subcommand::Clippy { allow, deny, warn, forbid, .. } => {
117                Self { allow, warn, deny, forbid }
118            }
119            _ => unreachable!("LintConfig can only be called from `clippy` subcommands."),
120        }
121    }
122
123    fn merge(&self, other: &Self) -> Self {
124        let merged = |self_attr: &[String], other_attr: &[String]| -> Vec<String> {
125            self_attr.iter().cloned().chain(other_attr.iter().cloned()).collect()
126        };
127        // This is written this way to ensure we get a compiler error if we add a new field.
128        Self {
129            allow: merged(&self.allow, &other.allow),
130            warn: merged(&self.warn, &other.warn),
131            deny: merged(&self.deny, &other.deny),
132            forbid: merged(&self.forbid, &other.forbid),
133        }
134    }
135}
136
137#[derive(Debug, Clone, PartialEq, Eq, Hash)]
138pub struct Std {
139    build_compiler: Compiler,
140    target: TargetSelection,
141    config: LintConfig,
142    /// Whether to lint only a subset of crates.
143    crates: Vec<String>,
144}
145
146impl Std {
147    fn new(
148        builder: &Builder<'_>,
149        target: TargetSelection,
150        config: LintConfig,
151        crates: Vec<String>,
152    ) -> Self {
153        Self {
154            build_compiler: builder.compiler(builder.top_stage, builder.host_target),
155            target,
156            config,
157            crates,
158        }
159    }
160
161    fn from_build_compiler(
162        build_compiler: Compiler,
163        target: TargetSelection,
164        config: LintConfig,
165        crates: Vec<String>,
166    ) -> Self {
167        Self { build_compiler, target, config, crates }
168    }
169}
170
171impl Step for Std {
172    type Output = ();
173
174    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
175        run.crate_or_deps("sysroot").path("library")
176    }
177
178    fn is_default_step(_builder: &Builder<'_>) -> bool {
179        true
180    }
181
182    fn make_run(run: RunConfig<'_>) {
183        let crates = std_crates_for_run_make(&run);
184        let config = LintConfig::new(run.builder);
185        run.builder.ensure(Std::new(run.builder, run.target, config, crates));
186    }
187
188    fn run(self, builder: &Builder<'_>) {
189        let target = self.target;
190        let build_compiler = self.build_compiler;
191
192        let mut cargo = builder::Cargo::new(
193            builder,
194            build_compiler,
195            Mode::Std,
196            SourceType::InTree,
197            target,
198            Kind::Clippy,
199        );
200
201        std_cargo(builder, target, &mut cargo, &self.crates);
202
203        let _guard = builder.msg(
204            Kind::Clippy,
205            format_args!("library{}", crate_description(&self.crates)),
206            Mode::Std,
207            build_compiler,
208            target,
209        );
210
211        run_cargo(
212            builder,
213            cargo,
214            lint_args(builder, &self.config, IGNORED_RULES_FOR_STD_AND_RUSTC),
215            &build_stamp::libstd_stamp(builder, build_compiler, target),
216            vec![],
217            true,
218            false,
219        );
220    }
221
222    fn metadata(&self) -> Option<StepMetadata> {
223        Some(StepMetadata::clippy("std", self.target).built_by(self.build_compiler))
224    }
225}
226
227/// Lints the compiler.
228///
229/// This will build Clippy with the `build_compiler` and use it to lint
230/// in-tree rustc.
231#[derive(Debug, Clone, PartialEq, Eq, Hash)]
232pub struct Rustc {
233    build_compiler: CompilerForCheck,
234    target: TargetSelection,
235    config: LintConfig,
236    /// Whether to lint only a subset of crates.
237    crates: Vec<String>,
238}
239
240impl Rustc {
241    fn new(
242        builder: &Builder<'_>,
243        target: TargetSelection,
244        config: LintConfig,
245        crates: Vec<String>,
246    ) -> Self {
247        Self {
248            build_compiler: prepare_compiler_for_check(builder, target, Mode::Rustc),
249            target,
250            config,
251            crates,
252        }
253    }
254}
255
256impl Step for Rustc {
257    type Output = ();
258    const IS_HOST: bool = true;
259
260    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
261        run.crate_or_deps("rustc-main").path("compiler")
262    }
263
264    fn is_default_step(_builder: &Builder<'_>) -> bool {
265        true
266    }
267
268    fn make_run(run: RunConfig<'_>) {
269        let builder = run.builder;
270        let crates = run.make_run_crates(Alias::Compiler);
271        let config = LintConfig::new(run.builder);
272        run.builder.ensure(Rustc::new(builder, run.target, config, crates));
273    }
274
275    fn run(self, builder: &Builder<'_>) {
276        let build_compiler = self.build_compiler.build_compiler();
277        let target = self.target;
278
279        let mut cargo = builder::Cargo::new(
280            builder,
281            build_compiler,
282            Mode::Rustc,
283            SourceType::InTree,
284            target,
285            Kind::Clippy,
286        );
287
288        rustc_cargo(builder, &mut cargo, target, &build_compiler, &self.crates);
289        self.build_compiler.configure_cargo(&mut cargo);
290
291        // Explicitly pass -p for all compiler crates -- this will force cargo
292        // to also lint the tests/benches/examples for these crates, rather
293        // than just the leaf crate.
294        for krate in &*self.crates {
295            cargo.arg("-p").arg(krate);
296        }
297
298        let _guard = builder.msg(
299            Kind::Clippy,
300            format_args!("compiler{}", crate_description(&self.crates)),
301            Mode::Rustc,
302            build_compiler,
303            target,
304        );
305
306        run_cargo(
307            builder,
308            cargo,
309            lint_args(builder, &self.config, IGNORED_RULES_FOR_STD_AND_RUSTC),
310            &build_stamp::librustc_stamp(builder, build_compiler, target),
311            vec![],
312            true,
313            false,
314        );
315    }
316
317    fn metadata(&self) -> Option<StepMetadata> {
318        Some(
319            StepMetadata::clippy("rustc", self.target)
320                .built_by(self.build_compiler.build_compiler()),
321        )
322    }
323}
324
325#[derive(Debug, Clone, Hash, PartialEq, Eq)]
326pub struct CodegenGcc {
327    build_compiler: CompilerForCheck,
328    target: TargetSelection,
329    config: LintConfig,
330}
331
332impl CodegenGcc {
333    fn new(builder: &Builder<'_>, target: TargetSelection, config: LintConfig) -> Self {
334        Self {
335            build_compiler: prepare_compiler_for_check(builder, target, Mode::Codegen),
336            target,
337            config,
338        }
339    }
340}
341
342impl Step for CodegenGcc {
343    type Output = ();
344
345    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
346        run.alias("rustc_codegen_gcc")
347    }
348
349    fn make_run(run: RunConfig<'_>) {
350        let builder = run.builder;
351        let config = LintConfig::new(builder);
352        builder.ensure(CodegenGcc::new(builder, run.target, config));
353    }
354
355    fn run(self, builder: &Builder<'_>) -> Self::Output {
356        let build_compiler = self.build_compiler.build_compiler();
357        let target = self.target;
358
359        let mut cargo = prepare_tool_cargo(
360            builder,
361            build_compiler,
362            Mode::Codegen,
363            target,
364            Kind::Clippy,
365            "compiler/rustc_codegen_gcc",
366            SourceType::InTree,
367            &[],
368        );
369        self.build_compiler.configure_cargo(&mut cargo);
370
371        let _guard = builder.msg(
372            Kind::Clippy,
373            "rustc_codegen_gcc",
374            Mode::ToolRustcPrivate,
375            build_compiler,
376            target,
377        );
378
379        let stamp = BuildStamp::new(&builder.cargo_out(build_compiler, Mode::Codegen, target))
380            .with_prefix("rustc_codegen_gcc-check");
381
382        let args = lint_args(builder, &self.config, &[]);
383        run_cargo(builder, cargo, args.clone(), &stamp, vec![], true, false);
384
385        // Same but we disable the features enabled by default.
386        let mut cargo = prepare_tool_cargo(
387            builder,
388            build_compiler,
389            Mode::Codegen,
390            target,
391            Kind::Clippy,
392            "compiler/rustc_codegen_gcc",
393            SourceType::InTree,
394            &[],
395        );
396        self.build_compiler.configure_cargo(&mut cargo);
397        println!("Now running clippy on `rustc_codegen_gcc` with `--no-default-features`");
398        cargo.arg("--no-default-features");
399        run_cargo(builder, cargo, args, &stamp, vec![], true, false);
400    }
401
402    fn metadata(&self) -> Option<StepMetadata> {
403        Some(
404            StepMetadata::clippy("rustc_codegen_gcc", self.target)
405                .built_by(self.build_compiler.build_compiler()),
406        )
407    }
408}
409
410macro_rules! lint_any {
411    ($(
412        $name:ident,
413        $path:expr,
414        $readable_name:expr,
415        $mode:expr
416        $(, lint_by_default = $lint_by_default:expr )?
417        ;
418    )+) => {
419        $(
420
421        #[derive(Debug, Clone, Hash, PartialEq, Eq)]
422        pub struct $name {
423            build_compiler: CompilerForCheck,
424            target: TargetSelection,
425            config: LintConfig,
426        }
427
428        impl Step for $name {
429            type Output = ();
430
431            fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
432                run.path($path)
433            }
434
435            fn is_default_step(_builder: &Builder<'_>) -> bool {
436                false $( || const { $lint_by_default } )?
437            }
438
439            fn make_run(run: RunConfig<'_>) {
440                let config = LintConfig::new(run.builder);
441                run.builder.ensure($name {
442                    build_compiler: prepare_compiler_for_check(run.builder, run.target, $mode),
443                    target: run.target,
444                    config,
445                });
446            }
447
448            fn run(self, builder: &Builder<'_>) -> Self::Output {
449                let build_compiler = self.build_compiler.build_compiler();
450                let target = self.target;
451                let mut cargo = prepare_tool_cargo(
452                    builder,
453                    build_compiler,
454                    $mode,
455                    target,
456                    Kind::Clippy,
457                    $path,
458                    SourceType::InTree,
459                    &[],
460                );
461                self.build_compiler.configure_cargo(&mut cargo);
462
463                let _guard = builder.msg(
464                    Kind::Clippy,
465                    $readable_name,
466                    $mode,
467                    build_compiler,
468                    target,
469                );
470
471                let stringified_name = stringify!($name).to_lowercase();
472                let stamp = BuildStamp::new(&builder.cargo_out(build_compiler, $mode, target))
473                    .with_prefix(&format!("{}-check", stringified_name));
474
475                run_cargo(
476                    builder,
477                    cargo,
478                    lint_args(builder, &self.config, &[]),
479                    &stamp,
480                    vec![],
481                    true,
482                    false,
483                );
484            }
485
486            fn metadata(&self) -> Option<StepMetadata> {
487                Some(StepMetadata::clippy($readable_name, self.target).built_by(self.build_compiler.build_compiler()))
488            }
489        }
490        )+
491    }
492}
493
494// Note: we use ToolTarget instead of ToolBootstrap here, to allow linting in-tree host tools
495// using the in-tree Clippy. Because Mode::ToolBootstrap would always use stage 0 rustc/Clippy.
496lint_any!(
497    Bootstrap, "src/bootstrap", "bootstrap", Mode::ToolTarget;
498    BuildHelper, "src/build_helper", "build_helper", Mode::ToolTarget;
499    BuildManifest, "src/tools/build-manifest", "build-manifest", Mode::ToolTarget;
500    CargoMiri, "src/tools/miri/cargo-miri", "cargo-miri", Mode::ToolRustcPrivate;
501    Clippy, "src/tools/clippy", "clippy", Mode::ToolRustcPrivate;
502    CollectLicenseMetadata, "src/tools/collect-license-metadata", "collect-license-metadata", Mode::ToolTarget;
503    Compiletest, "src/tools/compiletest", "compiletest", Mode::ToolTarget;
504    CoverageDump, "src/tools/coverage-dump", "coverage-dump", Mode::ToolTarget;
505    Jsondocck, "src/tools/jsondocck", "jsondocck", Mode::ToolTarget;
506    Jsondoclint, "src/tools/jsondoclint", "jsondoclint", Mode::ToolTarget;
507    LintDocs, "src/tools/lint-docs", "lint-docs", Mode::ToolTarget;
508    LlvmBitcodeLinker, "src/tools/llvm-bitcode-linker", "llvm-bitcode-linker", Mode::ToolTarget;
509    Miri, "src/tools/miri", "miri", Mode::ToolRustcPrivate;
510    MiroptTestTools, "src/tools/miropt-test-tools", "miropt-test-tools", Mode::ToolTarget;
511    OptDist, "src/tools/opt-dist", "opt-dist", Mode::ToolTarget;
512    RemoteTestClient, "src/tools/remote-test-client", "remote-test-client", Mode::ToolTarget;
513    RemoteTestServer, "src/tools/remote-test-server", "remote-test-server", Mode::ToolTarget;
514    RustAnalyzer, "src/tools/rust-analyzer", "rust-analyzer", Mode::ToolRustcPrivate;
515    Rustdoc, "src/librustdoc", "clippy", Mode::ToolRustcPrivate;
516    Rustfmt, "src/tools/rustfmt", "rustfmt", Mode::ToolRustcPrivate;
517    RustInstaller, "src/tools/rust-installer", "rust-installer", Mode::ToolTarget;
518    Tidy, "src/tools/tidy", "tidy", Mode::ToolTarget;
519    TestFloatParse, "src/tools/test-float-parse", "test-float-parse", Mode::ToolStd;
520);
521
522/// Runs Clippy on in-tree sources of selected projects using in-tree CLippy.
523#[derive(Debug, Clone, PartialEq, Eq, Hash)]
524pub struct CI {
525    target: TargetSelection,
526    config: LintConfig,
527}
528
529impl Step for CI {
530    type Output = ();
531
532    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
533        run.alias("ci")
534    }
535
536    fn is_default_step(_builder: &Builder<'_>) -> bool {
537        false
538    }
539
540    fn make_run(run: RunConfig<'_>) {
541        let config = LintConfig::new(run.builder);
542        run.builder.ensure(CI { target: run.target, config });
543    }
544
545    fn run(self, builder: &Builder<'_>) -> Self::Output {
546        if builder.top_stage != 2 {
547            eprintln!("ERROR: `x clippy ci` should always be executed with --stage 2");
548            exit!(1);
549        }
550
551        // We want to check in-tree source using in-tree clippy. However, if we naively did
552        // a stage 2 `x clippy ci`, it would *build* a stage 2 rustc, in order to lint stage 2
553        // std, which is wasteful.
554        // So we want to lint stage 2 [bootstrap/rustc/...], but only stage 1 std rustc_codegen_gcc.
555        // We thus construct the compilers in this step manually, to optimize the number of
556        // steps that get built.
557
558        builder.ensure(Bootstrap {
559            // This will be the stage 1 compiler
560            build_compiler: prepare_compiler_for_check(builder, self.target, Mode::ToolTarget),
561            target: self.target,
562            config: self.config.merge(&LintConfig {
563                allow: vec![],
564                warn: vec![],
565                deny: vec!["warnings".into()],
566                forbid: vec![],
567            }),
568        });
569
570        let library_clippy_cfg = LintConfig {
571            allow: vec!["clippy::all".into()],
572            warn: vec![],
573            deny: vec![
574                "clippy::correctness".into(),
575                "clippy::char_lit_as_u8".into(),
576                "clippy::four_forward_slashes".into(),
577                "clippy::needless_bool".into(),
578                "clippy::needless_bool_assign".into(),
579                "clippy::non_minimal_cfg".into(),
580                "clippy::print_literal".into(),
581                "clippy::same_item_push".into(),
582                "clippy::single_char_add_str".into(),
583                "clippy::to_string_in_format_args".into(),
584                "clippy::unconditional_recursion".into(),
585            ],
586            forbid: vec![],
587        };
588        builder.ensure(Std::from_build_compiler(
589            // This will be the stage 1 compiler, to avoid building rustc stage 2 just to lint std
590            builder.compiler(1, self.target),
591            self.target,
592            self.config.merge(&library_clippy_cfg),
593            vec![],
594        ));
595
596        let compiler_clippy_cfg = LintConfig {
597            allow: vec!["clippy::all".into()],
598            warn: vec![],
599            deny: vec![
600                "clippy::correctness".into(),
601                "clippy::char_lit_as_u8".into(),
602                "clippy::clone_on_ref_ptr".into(),
603                "clippy::format_in_format_args".into(),
604                "clippy::four_forward_slashes".into(),
605                "clippy::needless_bool".into(),
606                "clippy::needless_bool_assign".into(),
607                "clippy::non_minimal_cfg".into(),
608                "clippy::print_literal".into(),
609                "clippy::same_item_push".into(),
610                "clippy::single_char_add_str".into(),
611                "clippy::to_string_in_format_args".into(),
612                "clippy::unconditional_recursion".into(),
613            ],
614            forbid: vec![],
615        };
616        // This will lint stage 2 rustc using stage 1 Clippy
617        builder.ensure(Rustc::new(
618            builder,
619            self.target,
620            self.config.merge(&compiler_clippy_cfg),
621            vec![],
622        ));
623
624        let rustc_codegen_gcc = LintConfig {
625            allow: vec![],
626            warn: vec![],
627            deny: vec!["warnings".into()],
628            forbid: vec![],
629        };
630        // This will check stage 2 rustc
631        builder.ensure(CodegenGcc::new(
632            builder,
633            self.target,
634            self.config.merge(&rustc_codegen_gcc),
635        ));
636    }
637}