Skip to content

Instantly share code, notes, and snippets.

@coolreader18
Last active February 28, 2025 00:54
Show Gist options
  • Save coolreader18/b3fa6a1435c579e3078613a88f49a3d3 to your computer and use it in GitHub Desktop.
Save coolreader18/b3fa6a1435c579e3078613a88f49a3d3 to your computer and use it in GitHub Desktop.
Make any rustc lint machine-applicable :)
#!/bin/bash
# this is gross
#
# to use, set RUSTC_WORKSPACE_WRAPPER to this file
# useful with `cargo fix` when adding a new lint to your codebase
#
# if you'd like to filter only to specific diagnostics, set the env variable
# MAKE_MACHINE_APPLICABLE_FILTER to a comma-separated list of lint names or error codes
rustc="$1"
shift
case "$*" in
*error-format=json*)
set -o pipefail
{ "$rustc" "$@"; exit $?; } 2>&1 | python >&2 -c '
import json
import os
import sys
codes = os.environ.get("MAKE_MACHINE_APPLICABLE_FILTER")
if codes:
codes = set(codes.split(","))
def check_code(diag):
if not codes:
return True
code = (diag.get("code") or {}).get("code")
return code in codes
def edit_diag(diag):
for span in diag.get("spans", []):
if span["suggestion_applicability"] == "Unspecified":
span["suggestion_applicability"] = "MachineApplicable"
for child in diag.get("children", []):
edit_diag(child)
for line in sys.stdin.readlines():
obj = json.loads(line)
if obj.get("$message_type") == "diagnostic" and check_code(obj):
edit_diag(obj)
print(json.dumps(obj))
'
;;
*)
exec "$rustc" "$@"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment