blob: adb2e67069af43d1c1123ca559d2808fcb3ede74 (
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
|
def no_use_as_alias(root_dir)
pattern = root_dir.join('crates', '**', '*.rs').to_s
errors = Dir.glob(pattern).sort.flat_map do |path|
relative = Pathname.new(path).relative_path_from(root_dir).to_s
find_use_aliases(path, relative)
end
return true if errors.empty?
puts 'Found `use ... as Name` aliases.'
puts 'Renaming imports is forbidden; only unnamed imports `as _` (e.g. `use std::io::Write as _;`) are allowed:'
errors.each do |err|
puts " #{err}"
end
false
end
USE_ALIAS_START_RE = /\A(?:pub(?:\([^)]*\))?\s+)?use\b/
def find_use_aliases(path, relative)
errors = []
in_use = false
brace_depth = 0
File.readlines(path).each_with_index do |raw, idx|
code = raw.split('//', 2).first || raw
stripped = code.strip
unless in_use
next unless stripped =~ USE_ALIAS_START_RE
in_use = true
brace_depth = 0
end
code.scan(/\bas\s+([A-Za-z_][A-Za-z0-9_]*)/) do |m|
name = m[0]
next if name == '_'
errors << "#{relative}:#{idx + 1}: `as #{name}` aliasing in `use` statement"
end
brace_depth += code.count('{') - code.count('}')
if brace_depth <= 0 && code.rstrip.end_with?(';')
in_use = false
brace_depth = 0
end
end
errors
end
|