aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorArthur Zamarin <arthurzam@gentoo.org>2023-09-15 13:16:01 +0300
committerArthur Zamarin <arthurzam@gentoo.org>2023-09-23 18:36:03 +0300
commit9b6446766f42a829f2a07a55989f4e404531fd3e (patch)
tree7d95444ed4a8c66781a0f02926bd080f58bf6c20 /src
parentBannedPhaseCall: detect calls of phase functions directly (diff)
downloadpkgcheck-9b6446766f42a829f2a07a55989f4e404531fd3e.tar.gz
pkgcheck-9b6446766f42a829f2a07a55989f4e404531fd3e.tar.bz2
pkgcheck-9b6446766f42a829f2a07a55989f4e404531fd3e.zip
VariableShadowed: new check to detect shadowed variables
Resolves: https://github.com/pkgcore/pkgcheck/issues/622 Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
Diffstat (limited to 'src')
-rw-r--r--src/pkgcheck/checks/codingstyle.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/pkgcheck/checks/codingstyle.py b/src/pkgcheck/checks/codingstyle.py
index 1838be28..58634515 100644
--- a/src/pkgcheck/checks/codingstyle.py
+++ b/src/pkgcheck/checks/codingstyle.py
@@ -1443,3 +1443,43 @@ class GlobCheck(Check):
lineno, _colno = node.start_point
yield GlobDistdir(line=pkg.node_str(node), lineno=lineno + 1, pkg=pkg)
break
+
+
+class VariableShadowed(results.LinesResult, results.Warning):
+ """Variable is shadowed or repeatedly declared. This is a possible typo."""
+
+ def __init__(self, var_name, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self.var_name = var_name
+
+ @property
+ def desc(self):
+ return f"variable {self.var_name!r} may be shadowed, {self.lines_str}"
+
+
+class DeclarationShadowedCheck(Check):
+ """Scan ebuilds for shadowed variable assignments in global scope."""
+
+ _source = sources.EbuildParseRepoSource
+ known_results = frozenset({VariableShadowed})
+
+ def feed(self, pkg: bash.ParseTree):
+ assigns = defaultdict(list)
+
+ for node in pkg.tree.root_node.children:
+ if node.type == "variable_assignment":
+ used_name = pkg.node_str(node.child_by_field_name("name"))
+ if pkg.node_str(node).startswith(used_name + "+="):
+ continue
+ if value_node := node.child_by_field_name("value"):
+ if any(
+ pkg.node_str(node) == used_name
+ for node, _ in bash.var_query.captures(value_node)
+ ):
+ continue
+ assigns[used_name].append(node)
+
+ for var_name, nodes in assigns.items():
+ if len(nodes) > 1:
+ lines = sorted(node.start_point[0] + 1 for node in nodes)
+ yield VariableShadowed(var_name, lines=lines, pkg=pkg)