xref: /aosp_15_r20/external/crosvm/tools/clippy (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
1#!/usr/bin/env python3
2# Copyright 2019 The ChromiumOS Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6# To check for violations:
7# $ ./tools/clippy
8#
9# To fix violations where possible:
10# $ ./tools/clippy --fix
11
12from typing import Optional
13from impl.common import CROSVM_ROOT, run_main, cmd, chdir, Triple, SHORTHANDS
14from impl.test_config import DO_NOT_BUILD_RISCV64
15
16clippy = cmd("cargo clippy").with_color_flag()
17
18
19def main(
20    fix: bool = False,
21    json: bool = False,
22    locked: bool = False,
23    platform: Optional[str] = None,
24):
25    try:
26        triple: Triple = Triple.from_shorthand(platform) if platform else Triple.host_default()
27    except Exception as e:
28        raise type(e)(str(e) + f"\nValid platforms are {', '.join(SHORTHANDS.keys())}")
29
30    chdir(CROSVM_ROOT)
31
32    # Note: Clippy checks are configured in .cargo/config.toml
33    args = [
34        "--message-format=json" if json else None,
35        "--locked" if locked else None,
36        "--all-targets",
37        "--",
38        "-Dwarnings",
39    ]
40    if fix:
41        args = [
42            "--fix",
43            "--allow-no-vcs",
44            "--allow-dirty",
45            "--allow-staged",
46            *args,
47        ]
48    # For experimental builds, don't clippy the whole workspace, just what's enabled by features.
49    if triple in (Triple.from_shorthand("riscv64"), Triple.from_shorthand("android")):
50        args = ["--no-default-features", *args]
51    else:
52        args = ["--workspace", *args]
53
54    print("Clippy crosvm workspace")
55    clippy(
56        f"--features={triple.feature_flag}",
57        *args,
58    ).with_envs(triple.get_cargo_env()).fg()
59
60
61if __name__ == "__main__":
62    run_main(main)
63