Skip to content

Commit

Permalink
MetOffice#20 Implement LabelledExits rule
Browse files Browse the repository at this point in the history
  • Loading branch information
bilal-chughtai committed Feb 24, 2022
1 parent a8e4d2c commit 74cfa19
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions source/stylist/fortran.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"""
Rules relating to Fortran source.
"""
import re
from abc import ABCMeta, abstractmethod
from collections import defaultdict
import re
from typing import Dict, List, Optional, Pattern, Type, Union

import fparser.two.Fortran2003 as Fortran2003 # type: ignore
Expand All @@ -22,6 +22,7 @@ class FortranRule(Rule, metaclass=ABCMeta):
"""
Parent for style rules pertaining to Fortran source.
"""

def examine(self, subject: FortranSource) -> List[Issue]:
issues = []
if not isinstance(subject, FortranSource):
Expand Down Expand Up @@ -116,6 +117,7 @@ class MissingImplicit(FortranRule):
Catches cases where code blocks which could have an "implicit" statement
don't.
"""

def __init__(self, default='none', require_everywhere=False) -> None:
"""
Constructor taking a default implication.
Expand Down Expand Up @@ -173,8 +175,9 @@ def examine_fortran(self, subject: FortranSource) -> List[Issue]:

class MissingOnly(FortranRule):
"""
Catches cases where a "use" statement is present but has no "only" claus.
Catches cases where a "use" statement is present but has no "only" clause.
"""

def __init__(self, ignore: Optional[List[str]] = None) -> None:
"""
Constructs a "MissingOnly" rule object taking a list of exception
Expand All @@ -200,6 +203,34 @@ def examine_fortran(self, subject: FortranSource) -> List[Issue]:
return issues


class LabelledExits(FortranRule):
"""
Catches cases where a construct is exited but not explicitly named.
"""

def examine_fortran(self, subject: FortranSource) -> List[Issue]:
issues = []

for exit in subject.find_all(Fortran2003.Exit_Stmt):
if exit.items[1] is None:
issues.append(Issue('Usage of "exit" without label '
'indicating ' \
'which do construct is being exited from.',
line=exit.item.span[0]))

# Above doesn't catch exits in inline if statements
for statement in subject.find_all(Fortran2003.If_Stmt):
action = statement.items[1]
if type(action) == Fortran2003.Exit_Stmt and action.items[
1] is None:
issues.append(Issue('Usage of "exit" without label '
'indicating ' \
'which do construct is being exited from.',
line=statement.item.span[0]))

return issues


class MissingPointerInit(FortranRule):
"""
Catches cases where a pointer is declared without being initialised.
Expand Down Expand Up @@ -301,7 +332,7 @@ def examine_fortran(self, subject: FortranSource) -> List[Issue]:

class KindPattern(FortranRule):
_ISSUE_TEMPLATE = "Kind '{kind}' found for {type} variable '{name}' does" \
" not fit the pattern /{pattern}/."
" not fit the pattern /{pattern}/."

def __init__(self, *, # There are no positional arguments.
integer: Union[str, Pattern],
Expand Down

0 comments on commit 74cfa19

Please # to comment.