Skip to content

Add check for unused error codes #31288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 50 additions & 3 deletions src/etc/errorck.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,31 @@

src_dir = sys.argv[1]
errcode_map = {}
errcode_checked = []
errcode_not_found = []
error_re = re.compile("(E\d\d\d\d)")

def check_unused_error_codes(error_codes, check_error_codes, filenames, dirnames, dirpath):
for filename in filenames:
if filename == "diagnostics.rs" or not filename.endswith(".rs"):
continue
path = os.path.join(dirpath, filename)

with open(path, 'r') as f:
for line in f:
match = error_re.search(line)
if match:
errcode = match.group(1)
if errcode in error_codes:
error_codes.remove(errcode)
if errcode not in check_error_codes:
check_error_codes.append(errcode)
for dirname in dirnames:
path = os.path.join(dirpath, dirname)
for (dirpath, dnames, fnames) in os.walk(path):
check_unused_error_codes(error_codes, check_error_codes, fnames, dnames, dirpath)


# In the register_long_diagnostics! macro, entries look like this:
#
# EXXXX: r##"
Expand All @@ -35,19 +58,23 @@
long_diag_begin = "r##\""
long_diag_end = "\"##"

errors = False
all_errors = []

for (dirpath, dirnames, filenames) in os.walk(src_dir):
if "src/test" in dirpath or "src/llvm" in dirpath:
# Short circuit for fast
continue

errcode_to_check = []
for filename in filenames:
if filename != "diagnostics.rs":
continue

path = os.path.join(dirpath, filename)

with open(path, 'r') as f:
inside_long_diag = False
errcode_to_check = []
for line_num, line in enumerate(f, start=1):
if inside_long_diag:
# Skip duplicate error code checking for this line
Expand All @@ -65,16 +92,36 @@
errcode_map[errcode] = existing + new_record
else:
errcode_map[errcode] = new_record
# we don't check if this is a long error explanation
if (long_diag_begin not in line and not line.strip().startswith("//")
and errcode not in errcode_to_check and errcode not in errcode_checked
and errcode not in errcode_not_found):
errcode_to_check.append(errcode)

if long_diag_begin in line:
inside_long_diag = True
break
check_unused_error_codes(errcode_to_check, errcode_checked, filenames, dirnames, dirpath)
if len(errcode_to_check) > 0:
for errcode in errcode_to_check:
if errcode in errcode_checked:
continue
errcode_not_found.append(errcode)

if len(errcode_not_found) > 0:
errcode_not_found.sort()
for errcode in errcode_not_found:
if errcode in errcode_checked:
continue
all_errors.append(errcode)
print("error: unused error code: " + errcode)
errors = True

errors = False
all_errors = []

for errcode, entries in errcode_map.items():
all_errors.append(entries[0][0])
if len(entries) > 1:
entries.sort()
print("error: duplicate error code " + errcode)
for entry in entries:
print("{1}: {2}\n{3}".format(*entry))
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ const X: i32 = 42 / 0;
```
"##,

E0038: r####"
E0038: r##"
Trait objects like `Box<Trait>` can only be constructed when certain
requirements are satisfied by the trait in question.

Expand Down Expand Up @@ -478,7 +478,7 @@ so they are forbidden when specifying supertraits.

There's no easy fix for this, generally code will need to be refactored so that
you no longer need to derive from `Super<Self>`.
"####,
"##,

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


register_diagnostics! {
// E0006 // merged with E0005
// E0006 // merged with E0005
// E0134,
// E0135,
E0278, // requirement is not satisfied
E0279, // requirement is not satisfied
E0280, // requirement is not satisfied
E0284, // cannot resolve type
E0285, // overflow evaluation builtin bounds
// E0285, // overflow evaluation builtin bounds
E0298, // mismatched types between arms
E0299, // mismatched types between arms
// E0300, // unexpanded macro
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_resolve/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,8 +1046,8 @@ register_diagnostics! {
// E0153, unused error code
// E0157, unused error code
E0254, // import conflicts with imported crate in this module
E0257,
E0258,
// E0257,
// E0258,
E0402, // cannot use an outer type parameter in this context
E0406, // undeclared associated type
E0408, // variable from pattern #1 is not bound in pattern #
Expand Down