summaryrefslogtreecommitdiffstats
path: root/tools/lint/clippy
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--tools/lint/clippy.yml110
-rw-r--r--tools/lint/clippy/__init__.py99
2 files changed, 209 insertions, 0 deletions
diff --git a/tools/lint/clippy.yml b/tools/lint/clippy.yml
new file mode 100644
index 0000000000..7b8d196e21
--- /dev/null
+++ b/tools/lint/clippy.yml
@@ -0,0 +1,110 @@
+---
+clippy:
+ description: Lint rust
+ include:
+ - build/workspace-hack/
+ - dom/midi/midir_impl/
+ - dom/media/gtest/
+ - dom/webauthn/libudev-sys/
+ - gfx/webrender_bindings/
+ - gfx/wr/peek-poke/
+ - gfx/wr/peek-poke/peek-poke-derive/
+ - gfx/wr/webrender_build/
+ - gfx/wr/wr_malloc_size_of/
+ - js/src/frontend/smoosh/
+ - js/src/rust/shared/
+ - modules/libpref/init/static_prefs/
+ - mozglue/static/rust/
+ - netwerk/base/mozurl/
+ - security/manager/ssl/data_storage/
+ - servo/components/derive_common/
+ - servo/components/selectors/
+ - servo/components/servo_arc/
+ - servo/components/style/
+ - servo/components/style_derive/
+ - servo/components/style_traits/
+ - servo/components/to_shmem/
+ - servo/components/to_shmem_derive/
+ - servo/tests/unit/style/
+ - testing/geckodriver/
+ - testing/mozbase/rust/mozdevice/
+ - testing/mozbase/rust/mozprofile/
+ - testing/mozbase/rust/mozrunner/
+ - testing/mozbase/rust/mozversion/
+ - testing/webdriver/
+ - third_party/rust/mp4parse/
+ - third_party/rust/mp4parse_capi/
+ - toolkit/crashreporter/mozannotation_client/
+ - toolkit/crashreporter/mozannotation_server/
+ - toolkit/components/kvstore/
+ - toolkit/components/glean/
+ - toolkit/library/rust/
+ - tools/fuzzing/rust/
+ - tools/profiler/rust-api/
+ - xpcom/rust/gtest/bench-collections/
+ - xpcom/rust/xpcom/xpcom_macros/
+ exclude:
+ # Many are failing for the same reasons:
+ # https://bugzilla.mozilla.org/show_bug.cgi?id=1606073
+ # https://bugzilla.mozilla.org/show_bug.cgi?id=1606077
+ - Cargo.toml
+ # nsstring
+ # derive_hash_xor_eq
+ - gfx/wr/
+ - gfx/wr/webrender/
+ - gfx/wr/examples/
+ # windows-only
+ - gfx/wr/example-compositor/compositor-windows/
+ - gfx/wr/webrender_api/
+ - gfx/wr/wrench/
+ - gfx/wgpu_bindings/
+ # not_unsafe_ptr_arg_deref
+ - modules/libpref/parser/
+ - tools/profiler/rust-helper/
+ - toolkit/library/rust/shared/
+ - toolkit/library/gtest/rust/
+ # not_unsafe_ptr_arg_deref
+ - remote/
+ - dom/media/webrtc/sdp/rsdparsa_capi/
+ - intl/encoding_glue/
+ # not_unsafe_ptr_arg_deref
+ - storage/rust/
+ - storage/variant/
+ # nsstring
+ - servo/ports/geckolib/tests/
+ - xpcom/rust/xpcom/
+ - xpcom/rust/nsstring/
+ - xpcom/rust/gtest/xpcom/
+ - xpcom/rust/gtest/nsstring/
+ - security/manager/ssl/cert_storage/
+ - intl/locale/rust/fluent-langneg-ffi/
+ - intl/locale/rust/unic-langid-ffi/
+ - toolkit/components/places/bookmark_sync/
+ - xpcom/rust/nserror/
+ - xpcom/rust/moz_task/
+ - xpcom/rust/gkrust_utils/
+ - netwerk/socket/neqo_glue/
+ - dom/media/webrtc/transport/mdns_service/
+ - tools/lint/test/files/clippy/
+ - servo/ports/geckolib/
+ - servo/ports/geckolib/tests/
+ - servo/tests/unit/malloc_size_of/
+ - servo/components/malloc_size_of/
+ - dom/media/webrtc/sdp/rsdparsa_capi/
+ - testing/geckodriver/marionette/
+ - toolkit/components/bitsdownload/bits_client/
+ - gfx/wr/example-compositor/compositor/
+ - toolkit/components/bitsdownload/bits_client/bits/
+ # mac and windows only
+ - security/manager/ssl/osclientcerts/
+ extensions:
+ - rs
+ support-files:
+ - 'tools/lint/clippy/**'
+ # the version of cargo-clippy is:
+ # clippy 0.1.65 (2019147 2022-09-19)
+ # we use the date instead to facilitate the check
+ # replacing - by . because Python packaging.version.Version expects this
+ min_clippy_version: 2022.09.19
+ type: external
+ payload: clippy:lint
diff --git a/tools/lint/clippy/__init__.py b/tools/lint/clippy/__init__.py
new file mode 100644
index 0000000000..1facb246fc
--- /dev/null
+++ b/tools/lint/clippy/__init__.py
@@ -0,0 +1,99 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+import bisect
+import json
+import os
+import subprocess
+import sys
+
+from mozlint import result
+from mozlint.pathutils import expand_exclusions
+
+
+def in_sorted_list(l, x):
+ i = bisect.bisect_left(l, x)
+ return i < len(l) and l[i] == x
+
+
+def handle_clippy_msg(config, line, log, base_path, files):
+ try:
+ detail = json.loads(line)
+ if "message" in detail:
+ p = detail["target"]["src_path"]
+ detail = detail["message"]
+ if "level" in detail:
+ if (
+ detail["level"] == "error" or detail["level"] == "failure-note"
+ ) and not detail["code"]:
+ log.debug(
+ "Error outside of clippy."
+ "This means that the build failed. Therefore, skipping this"
+ )
+ log.debug("File = {} / Detail = {}".format(p, detail))
+ return
+ # We are in a clippy warning
+ if len(detail["spans"]) == 0:
+ # For some reason, at the end of the summary, we can
+ # get the following line
+ # {'rendered': 'warning: 5 warnings emitted\n\n', 'children':
+ # [], 'code': None, 'level': 'warning', 'message':
+ # '5 warnings emitted', 'spans': []}
+ # if this is the case, skip it
+ log.debug(
+ "Skipping the summary line {} for file {}".format(detail, p)
+ )
+ return
+
+ l = detail["spans"][0]
+ if not in_sorted_list(files, p):
+ return
+ p = os.path.join(base_path, l["file_name"])
+ res = {
+ "path": p,
+ "level": detail["level"],
+ "lineno": l["line_start"],
+ "column": l["column_start"],
+ "message": detail["message"],
+ "hint": detail["rendered"],
+ "rule": detail["code"]["code"],
+ "lineoffset": l["line_end"] - l["line_start"],
+ }
+ return result.from_config(config, **res)
+
+ except json.decoder.JSONDecodeError:
+ log.debug("Could not parse the output:")
+ log.debug("clippy output: {}".format(line))
+ return
+
+
+def lint(paths, config, fix=None, **lintargs):
+ files = list(expand_exclusions(paths, config, lintargs["root"]))
+ files.sort()
+ log = lintargs["log"]
+ results = []
+ mach_path = lintargs["root"] + "/mach"
+ march_cargo_process = subprocess.Popen(
+ [
+ sys.executable,
+ mach_path,
+ "--log-no-times",
+ "cargo",
+ "clippy",
+ "--",
+ "--message-format=json",
+ ],
+ stdout=subprocess.PIPE,
+ text=True,
+ )
+ for l in march_cargo_process.stdout:
+ r = handle_clippy_msg(config, l, log, lintargs["root"], files)
+ if r is not None:
+ results.append(r)
+ march_cargo_process.wait()
+
+ if fix:
+ log.error("Rust linting in mach does not support --fix")
+
+ return results