Skip to content

Commit

Permalink
Add detection of unused parameters
Browse files Browse the repository at this point in the history
Ref. #874
  • Loading branch information
treiher committed Sep 26, 2022
1 parent fa1f67f commit 0981fa8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ CLI:
- `rflx setup_ide` subcommand for installing IDE integration (#795)
- `rflx` option `--unsafe` (#987)

Model:

- Detection of unused parameters (#874)

### Changed

Specification:
Expand Down
22 changes: 22 additions & 0 deletions rflx/model/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,7 @@ def __init__(

def verify(self) -> None:
if not self.is_null:
self._verify_parameters()
self._verify_expression_types()
self._verify_expressions()
self._verify_checksums()
Expand Down Expand Up @@ -1229,6 +1230,27 @@ def _max_value(self, target: expr.Expr, path: Tuple[Link, ...]) -> expr.Number:
],
)

def _verify_parameters(self) -> None:
variables = [
v.identifier
for l in self.structure
for e in [l.condition, l.size, l.first]
for v in e.findall(lambda x: isinstance(x, expr.Variable))
if isinstance(v, expr.Variable)
]
for p in self.parameters:
if p.identifier not in variables:
self.error.extend(
[
(
f'unused parameter "{p.identifier}"',
Subsystem.MODEL,
Severity.ERROR,
p.identifier.location,
)
]
)

def _verify_expression_types(self) -> None:
types: Dict[Field, mty.Type] = {}

Expand Down
18 changes: 17 additions & 1 deletion tests/unit/model/message_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,17 @@ def test_invalid_message_field_type() -> None:
)


def test_unused_parameter() -> None:
structure = [Link(INITIAL, Field("X")), Link(Field("X"), FINAL)]
types = {Field(ID("P", Location((1, 2)))): MODULAR_INTEGER, Field("X"): MODULAR_INTEGER}

assert_message_model_error(
structure,
types,
'^<stdin>:1:2: model: error: unused parameter "P"$',
)


@pytest.mark.parametrize(
"condition",
[
Expand Down Expand Up @@ -4081,7 +4092,11 @@ def test_message_str() -> None:
"P::M",
[
Link(INITIAL, Field("L")),
Link(Field("L"), Field("O"), condition=Greater(Variable("L"), Number(100))),
Link(
Field("L"),
Field("O"),
condition=And(Greater(Variable("L"), Number(100)), Equal(Variable("A"), TRUE)),
),
Link(Field("L"), Field("P"), condition=LessEqual(Variable("L"), Number(100))),
Link(Field("P"), FINAL),
Link(Field("O"), FINAL),
Expand All @@ -4102,6 +4117,7 @@ def test_message_str() -> None:
L : P::Modular
then O
if L > 100
and A = True
then P
if L <= 100;
O : P::Modular
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/specification/parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2819,7 +2819,9 @@ def test_parse_error_name_conflict_between_parameters() -> None:
type T is mod 2 ** 8;
type M (P : T; P : T) is
message
F : T;
F : T
then null
if P = F;
end message;
end Test;
""",
Expand Down

0 comments on commit 0981fa8

Please sign in to comment.