Cotton code
36 lines · 1.2 KB · PythonRaw
#!/usr/bin/env python3# Report and count clippy warnings without promoting them to errors.import jsonimport osfrom pathlib import Pathimport subprocessimport sysdef run(args):    process = subprocess.Popen(["cargo", "clippy", "--message-format=json", *args],                               stdout=subprocess.PIPE, text=True)    warnings = errors = 0    for line in process.stdout:        try:            message = json.loads(line)        except ValueError:            print(line, end="", flush=True)            continue        if message.get("reason") != "compiler-message":            continue        diagnostic = message["message"]        warnings += diagnostic.get("level") == "warning"        errors += diagnostic.get("level") == "error"        print(diagnostic.get("rendered") or diagnostic.get("message", ""), end="", flush=True)    status = process.wait()    summary = f"Clippy: {warnings} warnings, {errors} errors (exit {status})."    print(summary, flush=True)    if os.environ.get("GITHUB_STEP_SUMMARY"):        with Path(os.environ["GITHUB_STEP_SUMMARY"]).open("a") as file:            file.write(summary + "\n")    return statusif __name__ == "__main__":    sys.exit(run(sys.argv[1:]))