File: clippy_wrapper.py

package info (click to toggle)
chromium 145.0.7632.159-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,976,224 kB
  • sloc: cpp: 36,198,469; ansic: 7,634,080; javascript: 3,564,060; python: 1,649,622; xml: 838,470; asm: 717,087; pascal: 185,708; sh: 88,786; perl: 88,718; objc: 79,984; sql: 59,811; cs: 42,452; fortran: 24,101; makefile: 21,144; tcl: 15,277; php: 14,022; yacc: 9,066; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (65 lines) | stat: -rwxr-xr-x 2,378 bytes parent folder | download | duplicates (6)
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
#!/usr/bin/env python3

# Copyright 2026 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import argparse
import subprocess
import pathlib
import sys
import tempfile

from rustc_wrapper import (ConvertPathsToAbsolute, LoadRustEnvAndFlags)


def main():
  parser = argparse.ArgumentParser()
  parser.add_argument('--clippy-driver', required=True, type=pathlib.Path)
  parser.add_argument('--rustc-env-and-flags', type=pathlib.Path, required=True)
  parser.add_argument('--build-stamp-file', type=pathlib.Path, required=True)
  args = parser.parse_args()

  (rustenv, rustflags) = LoadRustEnvAndFlags(args.rustc_env_and_flags)
  ConvertPathsToAbsolute(rustenv)

  rustflags += [
      # Ask `clippy` to treat lint failures either as errors, or ignore
      # them altogether (i.e. avoid reporting failures as warnings which
      # are noisy but ignorable).  We cover lint categories from
      # https://doc.rust-lang.org/stable/clippy/lints.html that are either
      # `deny` or `warn` by default.
      #
      # If certain targets want to opt into additional lints, then they can
      # do so by using `#![deny(clippy::pedantic)]` or a similar attribute.
      "-Dclippy::correctness",
      "-Dclippy::suspicious",
      "-Dclippy::complexity",
      "-Dclippy::perf",
      "-Dclippy::style",
  ]

  # `clippy-driver` should not write any files into the build directory
  # (e.g. into `out/`).
  #
  # `--emit=metadata` asks Clippy to only emit `.rmeta`.  This:
  # * Matches how `cargo clippy` invokes `clippy-driver` - see
  #   https://crrev.com/c/7316228/17#message-fa234861e30ee37399388ca5a6a048822b658833
  # * Seems sufficient for triggering Clippy lints.
  # * Avoids performance overhead (and general ickiness) of also building
  #   `.rlib` from within `clippy-driver`.
  assert not [x for x in rustflags if x.startswith("--emit")]
  assert not [x for x in rustflags if x.startswith("-o")]
  assert not [x for x in rustflags if x.startswith("--out-dir")]
  temp_dir = tempfile.TemporaryDirectory()
  rustflags += ["--out-dir", temp_dir.name]
  rustflags += ["--emit=metadata"]

  r = subprocess.run([args.clippy_driver, *rustflags], env=rustenv, check=False)
  if r.returncode == 0:
    args.build_stamp_file.touch()
  return r.returncode


if __name__ == '__main__':
  sys.exit(main())