Skip to content
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

Misleading indent var warn #25207

Merged
merged 5 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions test/chplcheck/MisleadingIndentation.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ writeln("second thing");
writeln(i);
writeln("second thing");

proc f() {
for 1..10 do
writeln("Hello, world!");
var unrelated = "hi";
}
}
42 changes: 30 additions & 12 deletions tools/chplcheck/src/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,31 @@ def variables(node: AstNode):
yield from variables(child)


def might_incorrectly_report_location(node: AstNode) -> bool:
"""
Some Dyno AST nodes do not return locations in the way that we expect.
For instance, some NamedDecl nodes currently use the name as the location,
which does not indicate their actual indentation. Rules that depend on
indentation should leave these variables alone.
"""

# some NamedDecl nodes currently use the name as the location, which
# does not indicate their actual indentation.
#
# https://github.com/chapel-lang/chapel/issues/25208
if isinstance(node, (VarLikeDecl, TupleDecl, ForwardingDecl)):
return True

# private function locations are bugged and don't include the 'private'
# keyword.
#
# https://github.com/chapel-lang/chapel/issues/24818
elif isinstance(node, (Function, Use, Import)) and node.visibility() != "":
return True

return False


def fixit_remove_unused_node(
node: AstNode,
lines: Optional[List[str]] = None,
Expand Down Expand Up @@ -403,6 +428,8 @@ def MisleadingIndentation(context: Context, root: AstNode):
for child in root:
if isinstance(child, Comment):
continue
if might_incorrectly_report_location(child):
continue
yield from MisleadingIndentation(context, child)

if prev is not None:
Expand Down Expand Up @@ -433,6 +460,8 @@ def MisleadingIndentation(context: Context, root: AstNode):
for blockchild in reversed(list(grandchildren[-1])):
if isinstance(blockchild, Comment):
continue
if might_incorrectly_report_location(blockchild):
continue
prev = blockchild
prevloop = child
break
Expand Down Expand Up @@ -640,22 +669,11 @@ def unwrap_intermediate_block(node: AstNode) -> Optional[AstNode]:
if isinstance(child, Comment):
continue

# some NamedDecl nodes currently use the name as the location, which
# does not indicate their actual indentation.
if isinstance(child, (VarLikeDecl, TupleDecl, ForwardingDecl)):
if might_incorrectly_report_location(child):
continue
# Empty statements get their own warnings, no need to warn here.
elif isinstance(child, EmptyStmt):
continue
# private function locations are bugged and don't include the 'private'
# keyword.
#
# https://github.com/chapel-lang/chapel/issues/24818
elif (
isinstance(child, (Function, Use, Import))
and child.visibility() != ""
):
continue

line, depth = child.location().start()

Expand Down
Loading