Skip to content

Commit 9a07087

Browse files
committed
Auto merge of #31288 - GuillaumeGomez:error_code_tidy, r=brson
r? @steveklabnik
2 parents 14f33a5 + c78bf9d commit 9a07087

File tree

3 files changed

+56
-9
lines changed

3 files changed

+56
-9
lines changed

src/etc/errorck.py

+50-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,31 @@
2121

2222
src_dir = sys.argv[1]
2323
errcode_map = {}
24+
errcode_checked = []
25+
errcode_not_found = []
2426
error_re = re.compile("(E\d\d\d\d)")
2527

28+
def check_unused_error_codes(error_codes, check_error_codes, filenames, dirnames, dirpath):
29+
for filename in filenames:
30+
if filename == "diagnostics.rs" or not filename.endswith(".rs"):
31+
continue
32+
path = os.path.join(dirpath, filename)
33+
34+
with open(path, 'r') as f:
35+
for line in f:
36+
match = error_re.search(line)
37+
if match:
38+
errcode = match.group(1)
39+
if errcode in error_codes:
40+
error_codes.remove(errcode)
41+
if errcode not in check_error_codes:
42+
check_error_codes.append(errcode)
43+
for dirname in dirnames:
44+
path = os.path.join(dirpath, dirname)
45+
for (dirpath, dnames, fnames) in os.walk(path):
46+
check_unused_error_codes(error_codes, check_error_codes, fnames, dnames, dirpath)
47+
48+
2649
# In the register_long_diagnostics! macro, entries look like this:
2750
#
2851
# EXXXX: r##"
@@ -35,19 +58,23 @@
3558
long_diag_begin = "r##\""
3659
long_diag_end = "\"##"
3760

61+
errors = False
62+
all_errors = []
63+
3864
for (dirpath, dirnames, filenames) in os.walk(src_dir):
3965
if "src/test" in dirpath or "src/llvm" in dirpath:
4066
# Short circuit for fast
4167
continue
4268

69+
errcode_to_check = []
4370
for filename in filenames:
4471
if filename != "diagnostics.rs":
4572
continue
46-
4773
path = os.path.join(dirpath, filename)
4874

4975
with open(path, 'r') as f:
5076
inside_long_diag = False
77+
errcode_to_check = []
5178
for line_num, line in enumerate(f, start=1):
5279
if inside_long_diag:
5380
# Skip duplicate error code checking for this line
@@ -65,16 +92,36 @@
6592
errcode_map[errcode] = existing + new_record
6693
else:
6794
errcode_map[errcode] = new_record
95+
# we don't check if this is a long error explanation
96+
if (long_diag_begin not in line and not line.strip().startswith("//")
97+
and errcode not in errcode_to_check and errcode not in errcode_checked
98+
and errcode not in errcode_not_found):
99+
errcode_to_check.append(errcode)
68100

69101
if long_diag_begin in line:
70102
inside_long_diag = True
103+
break
104+
check_unused_error_codes(errcode_to_check, errcode_checked, filenames, dirnames, dirpath)
105+
if len(errcode_to_check) > 0:
106+
for errcode in errcode_to_check:
107+
if errcode in errcode_checked:
108+
continue
109+
errcode_not_found.append(errcode)
110+
111+
if len(errcode_not_found) > 0:
112+
errcode_not_found.sort()
113+
for errcode in errcode_not_found:
114+
if errcode in errcode_checked:
115+
continue
116+
all_errors.append(errcode)
117+
print("error: unused error code: " + errcode)
118+
errors = True
71119

72-
errors = False
73-
all_errors = []
74120

75121
for errcode, entries in errcode_map.items():
76122
all_errors.append(entries[0][0])
77123
if len(entries) > 1:
124+
entries.sort()
78125
print("error: duplicate error code " + errcode)
79126
for entry in entries:
80127
print("{1}: {2}\n{3}".format(*entry))

src/librustc/diagnostics.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ const X: i32 = 42 / 0;
226226
```
227227
"##,
228228

229-
E0038: r####"
229+
E0038: r##"
230230
Trait objects like `Box<Trait>` can only be constructed when certain
231231
requirements are satisfied by the trait in question.
232232
@@ -478,7 +478,7 @@ so they are forbidden when specifying supertraits.
478478
479479
There's no easy fix for this, generally code will need to be refactored so that
480480
you no longer need to derive from `Super<Self>`.
481-
"####,
481+
"##,
482482

483483
E0072: r##"
484484
When defining a recursive struct or enum, any use of the type being defined
@@ -1801,14 +1801,14 @@ attribute.
18011801

18021802

18031803
register_diagnostics! {
1804-
// E0006 // merged with E0005
1804+
// E0006 // merged with E0005
18051805
// E0134,
18061806
// E0135,
18071807
E0278, // requirement is not satisfied
18081808
E0279, // requirement is not satisfied
18091809
E0280, // requirement is not satisfied
18101810
E0284, // cannot resolve type
1811-
E0285, // overflow evaluation builtin bounds
1811+
// E0285, // overflow evaluation builtin bounds
18121812
E0298, // mismatched types between arms
18131813
E0299, // mismatched types between arms
18141814
// E0300, // unexpanded macro

src/librustc_resolve/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1046,8 +1046,8 @@ register_diagnostics! {
10461046
// E0153, unused error code
10471047
// E0157, unused error code
10481048
E0254, // import conflicts with imported crate in this module
1049-
E0257,
1050-
E0258,
1049+
// E0257,
1050+
// E0258,
10511051
E0402, // cannot use an outer type parameter in this context
10521052
E0406, // undeclared associated type
10531053
E0408, // variable from pattern #1 is not bound in pattern #

0 commit comments

Comments
 (0)