Skip to content

Commit

Permalink
Add linter rule for unused type queries (#26116)
Browse files Browse the repository at this point in the history
Adds a chplcheck rule that warns if a user has a type query (e.g.
`aType` in `proc foo(a: ?aType)`), but doesn't use it. This is almost
identical in terms of motivation and implementation to `UnusedFormal`

[Reviewed by @DanilaFe]
  • Loading branch information
jabraham17 authored Oct 22, 2024
2 parents 8337447 + 7965d11 commit 568c873
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 2 deletions.
4 changes: 2 additions & 2 deletions modules/dists/DSIUtil.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ proc densify(subs, wholes, userErrors = true)
return result;
}

proc densify(s: range(?,bounds=?B), w: range(?IT,?), userErrors=true
proc densify(s: range(?,bounds=?), w: range(?IT,?), userErrors=true
) : densiResult(s, w)
{
_densiEnsureBounded(s);
Expand Down Expand Up @@ -425,7 +425,7 @@ proc unDensify(denses, wholes, userErrors = true)
return result;
}

proc unDensify(dense: range(?dIT,bounds=?B), whole: range(?IT,?)
proc unDensify(dense: range(?dIT,bounds=?), whole: range(?IT,?)
) : densiResult(dense, whole)
{
_undensEnsureBounded(dense);
Expand Down
10 changes: 10 additions & 0 deletions test/chplcheck/UnusedTypeQuery.chpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module UnusedTypeQuery {

proc foo(ref a: ?t) {
a = 1;
}
proc bar(ref arr: [?d] ?elt) where elt == int{
arr[1] = 1;
}

}
2 changes: 2 additions & 0 deletions test/chplcheck/UnusedTypeQuery.good
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
UnusedTypeQuery.chpl:3: node violates rule UnusedTypeQuery
UnusedTypeQuery.chpl:6: node violates rule UnusedTypeQuery
1 change: 1 addition & 0 deletions test/chplcheck/activerules.good
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ Active rules:
UnusedLoopIndex Warn for unused index variables in loops.
UnusedTaskIntent Warn for unused task intents in functions.
UnusedTupleUnpack Warn for unused tuple unpacking, such as '(_, _)'.
UnusedTypeQuery Warn for unused type queries in functions.
1 change: 1 addition & 0 deletions test/chplcheck/allrules.good
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ Available rules (default rules marked with *):
* UnusedLoopIndex Warn for unused index variables in loops.
* UnusedTaskIntent Warn for unused task intents in functions.
* UnusedTupleUnpack Warn for unused tuple unpacking, such as '(_, _)'.
* UnusedTypeQuery Warn for unused type queries in functions.
UseExplicitModules Warn for code that relies on auto-inserted implicit modules.
23 changes: 23 additions & 0 deletions tools/chplcheck/src/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,29 @@ def RemoveTaskIntent(context: Context, result: AdvancedRuleResult):
pass
return [fixit] if fixit else []

@driver.advanced_rule
def UnusedTypeQuery(context: Context, root: AstNode):
"""
Warn for unused type queries in functions.
"""
if isinstance(root, Comment):
return

typequeries = dict()
uses = set()

for tq, _ in chapel.each_matching(root, TypeQuery):

typequeries[tq.unique_id()] = tq

for use, _ in chapel.each_matching(root, Identifier):
refersto = use.to_node()
if refersto:
uses.add(refersto.unique_id())

for unused in typequeries.keys() - uses:
yield AdvancedRuleResult(typequeries[unused])

@driver.advanced_rule
def UnusedLoopIndex(context: Context, root: AstNode):
"""
Expand Down

0 comments on commit 568c873

Please sign in to comment.