Coverage for src/castep_linter/tests/allocate_stat_checked.py: 90%

1"""Test that allocate stat is used and checked"""

2 from castep_linter.error_logging import ErrorLogger

3 from castep_linter.fortran import CallExpression

4 from castep_linter.fortran.fortran_node import Fortran, FortranNode, WrongNodeError

5 from castep_linter.fortran.identifier import Identifier

6 from castep_linter.tests import castep_identifiers

7

8

9 def check_allocate_has_stat(node: FortranNode, error_log: ErrorLogger) -> None:

10 """Test that allocate stat is used and checked"""

11

12 if not node.is_type(Fortran.CALL_EXPRESSION):

13 err = "Expected variable declaration node"

14 raise WrongNodeError(err)

15

16 routine = CallExpression(node)

17

18 if routine.name is None: 18 ↛ 19line 18 didn't jump to line 19, because the condition on line 18 was never true

19 return

20

21 # Check this is actually an allocate statement

22 if routine.name != castep_identifiers.ALLOCATE:

23 return

24

25 # First get the stat variable for this allocate statement

26 try:

27 _, stat_variable_node = routine.get_arg(keyword=castep_identifiers.STAT)

28 except KeyError:

29 err = "No stat on allocate statement"

30 error_log.add_msg("Warning", node, err)

31 return

32

33 stat_variable = Identifier.from_node(stat_variable_node)

34

35 # Find the next non-comment line

36 next_node = node.next_named_sibling()

37 while next_node and next_node.is_type(Fortran.COMMENT):

38 next_node = next_node.next_named_sibling()

39

40 # Check if that uses the stat variable

41 if next_node and next_node.is_type(Fortran.IF_STMT):

42 try:

43 relational_expr = next_node.get(Fortran.PAREN_EXPRESSION).get(Fortran.RELATIONAL_EXPR)

44 except KeyError:

45 error_log.add_msg("Error", stat_variable_node, "Allocate status not checked")

46 return

47

48 lhs, rhs = relational_expr.split()

49

50 if lhs.is_type(Fortran.IDENTIFIER) and Identifier.from_node(lhs) == stat_variable:

51 return

52

53 if rhs.is_type(Fortran.IDENTIFIER) and Identifier.from_node(rhs) == stat_variable:

54 return

55

56 error_log.add_msg("Error", stat_variable_node, "Allocate status not checked")