summaryrefslogtreecommitdiff
path: root/maint/add_warning
blob: d2ceb6a241dfcf6c9893db0be0caaf3271dea83b (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/python

import sys
import os
import re
import shutil

PAT = re.compile(r'^#!\[(allow|deny|warn)')

WANT_LINTS = """
#![deny(missing_docs)]
#![warn(noop_method_call)]
#![deny(unreachable_pub)]
#![warn(clippy::all)]
#![deny(clippy::await_holding_lock)]
#![deny(clippy::cast_lossless)]
#![deny(clippy::checked_conversions)]
#![warn(clippy::cognitive_complexity)]
#![deny(clippy::cargo_common_metadata)]
#![warn(clippy::clone_on_ref_ptr)]
#![deny(clippy::debug_assert_with_mut_call)]
#![deny(clippy::exhaustive_enums)]
#![deny(clippy::exhaustive_structs)]
#![deny(clippy::expl_impl_clone_on_copy)]
#![deny(clippy::fallible_impl_from)]
#![deny(clippy::implicit_clone)]
#![deny(clippy::large_stack_arrays)]
#![warn(clippy::manual_ok_or)]
#![deny(clippy::missing_docs_in_private_items)]
#![deny(clippy::missing_panics_doc)]
#![warn(clippy::needless_borrow)]
#![warn(clippy::rc_buffer)]
#![deny(clippy::ref_option_ref)]
#![warn(clippy::semicolon_if_nothing_returned)]
#![warn(clippy::trait_duplication_in_bounds)]
#![deny(clippy::unnecessary_wraps)]
#![warn(clippy::unseparated_literal_suffix)]
#![deny(clippy::unwrap_used)]
"""
WANT_LINTS = [ "%s\n" % w for w in WANT_LINTS.split() ]

SOON="""
"""

WISH_WE_COULD = """
#![warn(unused_crate_dependencies)]
"""

DECIDED_NOT = """
#![deny(clippy::redundant_pub_crate)]
#![deny(clippy::future_not_send)]
#![deny(clippy::redundant_closure_for_method_calls)]
#![deny(clippy::panic)]
#![deny(clippy::if_then_some_else_none)]
#![deny(clippy::expect_used)]
#![deny(clippy::option_if_let_else)]
#![deny(missing_debug_implementations)]
#![deny(clippy::pub_enum_variant_names)]
"""


PAT2 = re.compile(r'^#!\[(allow|deny|warn)\(((?:clippy::)?)([^\)]*)')
def warning_key(w):
    m = PAT2.match(w)
    return (len(m.group(2)), m.group(3))

def filter_file(lints, inp, outp):
    head,warnings,other = list(),list(),list()
    for line in inp.readlines():
        if line.startswith("//!"):
            head.append(line)
        elif PAT.match(line) :
            warnings.append(line)
        else:
            other.append(line)

    for add_lint in lints:
        if add_lint not in warnings:
            warnings.append(add_lint)
    warnings.sort(key=warning_key)

    while other[0] == '\n':
        del other[0]

    for line in head:
        outp.write(line)
    outp.write("\n")
    for line in warnings:
        outp.write(line)
    outp.write("\n")
    for line in other:
        outp.write(line)

def process(lints, fn):
    print("{}...".format(fn))
    bak_name = fn+".bak"
    outp = open(bak_name,'w')
    inp = open(fn,'r')
    filter_file(lints, inp, outp)
    inp.close()
    outp.close()
    shutil.move(bak_name, fn)

def main(lints,files):
    if not os.path.exists("./crates/tor-proto/src/lib.rs"):
        print("Run this from the top level of an arti repo.")
        sys.exit(1)

    if not files:
        print("No files provided.  Example usage:")
        print("   ./maint/add_warning crates/*/src/{lib,main}.rs")

    for fn in files:
        process(lints, fn)


if __name__ == '__main__':
    main(WANT_LINTS, sys.argv[1:])