Skip to content

Commit 713d3ee

Browse files
authored
O2 linter: Add test for magic numbers (#10616)
1 parent ebfd000 commit 713d3ee

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Scripts/o2_linter.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,24 @@ def test_file(self, path: str, content) -> bool:
592592
return True
593593

594594

595+
class TestMagicNumber(TestSpec):
596+
"""Detect magic numbers."""
597+
598+
name = "magic-number"
599+
message = "Avoid magic numbers in expressions. Assign the value to a clearly named variable or constant."
600+
suffixes = [".h", ".cxx", ".C"]
601+
602+
def test_line(self, line: str) -> bool:
603+
if is_comment_cpp(line):
604+
return True
605+
line = remove_comment_cpp(line)
606+
if not (match := re.search(r" ([<>]=?|[!=]=) [\+-]?([\d\.]+)", line)):
607+
return True
608+
number = match.group(2)
609+
# Accept only 0 or 1 (int or float).
610+
return re.match(r"[01](\.0?)?$", number) is not None
611+
612+
595613
# Documentation
596614
# Reference: https://rawgit.com/AliceO2Group/CodingGuidelines/master/comments_guidelines.html
597615

@@ -1435,6 +1453,7 @@ def main():
14351453
tests.append(TestConstRefInForLoop())
14361454
tests.append(TestConstRefInSubscription())
14371455
tests.append(TestWorkflowOptions())
1456+
tests.append(TestMagicNumber())
14381457

14391458
# Documentation
14401459
enable_documentation = True

0 commit comments

Comments
 (0)